无法获取javascript中的当前位置 [英] Unable to get current location in javascript

查看:109
本文介绍了无法获取javascript中的当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
    $(document).ready(function () {
        debugger;
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        }
        else {
            alert("Geolocation is not supported by this browser.");
        }


        function showPosition(position) {
            debugger;
            var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            var geocoder = geocoder = new google.maps.Geocoder();
            geocoder.geocode({ 'latLng': latlng }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if (results[1]) {
                        document.getElementById("txtaddress").value = results[1].formatted_address;
                    }
                }
            });
        }
    });
</script>





我检查了javascript代码showPosition函数未被调用,这就是为什么我无法获取位置。



I have checked javascript code showPosition function is not getting called that's why i am unable to get location.

推荐答案

(document).ready(function(){
debugger;
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition) );
}
else {
alert(此浏览器不支持地理定位。);
}


函数showPosition (位置){
调试器;
var latlng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
var geocoder = geocoder = new google.maps .Geocoder();
geocoder.geocode({'latLng':latlng},function(results,status){
if(status) == google.maps.GeocoderStatus.OK){
if(results [1]){
document.getElementById(txtaddress)。value = results [1] .formatted_address;
}
}
});
}
});
< / script >
(document).ready(function () { debugger; if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } else { alert("Geolocation is not supported by this browser."); } function showPosition(position) { debugger; var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); var geocoder = geocoder = new google.maps.Geocoder(); geocoder.geocode({ 'latLng': latlng }, function (results, status) { if (status == google.maps.GeocoderStatus.OK) { if (results[1]) { document.getElementById("txtaddress").value = results[1].formatted_address; } } }); } }); </script>





我检查了javascript代码showPosition函数未被调用,这就是为什么我无法获取位置。



I have checked javascript code showPosition function is not getting called that's why i am unable to get location.


<!DOCTYPE html>
<html>
  <head>

    <script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js"></script>
    <script>
        jQuery(window).ready(function(){
            jQuery("#btnInit").click(initiate_geolocation);
        });

        function initiate_geolocation() {
            navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors);
        }

        function handle_errors(error)
        {
            switch(error.code)
            {
                case error.PERMISSION_DENIED: alert("user did not share geolocation data");
                break;

                case error.POSITION_UNAVAILABLE: alert("could not detect current position");
                break;

                case error.TIMEOUT: alert("retrieving position timed out");
                break;

                default: alert("unknown error");
                break;
            }
        }

        function handle_geolocation_query(position){
            alert('Lat: ' + position.coords.latitude +
                  ' Lon: ' + position.coords.longitude);
        }
    </script>
  </head>
  <body>
    <div>
      <button id="btnInit" >Find my location</button>
    </div>
  </body>
</html>






你试过这个解决方案吗?

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_nav_geolocation [ ^ ]



我遇到地理位置的两个问题,不知道你是不是这样:



1.如果你不允许地理位置比以后它不适用于同一页面(不要记住它是哪个浏览器...很久以前)



2.如果你在光盘上打开html页面作为文件,geolocation对我不起作用一个浏览器,例如c:/mypage.html



希望它会有所帮助
Hi,

have you tried this solution?
http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_nav_geolocation[^]

Two problems that I had with geolocation, don't know if that is you case:

1. If you don't allow geolocation than in future it won't work for the same page (don't remember which browser it was... it was long time ago)

2. geolocation doesn't worked for me if you open html page on disc as a file in a browser for example c:/mypage.html

hope it will help


这篇关于无法获取javascript中的当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆