使用 W3C GeoLocation API 难以显示地图并将其与用户位置居中

发布于 2024-11-27 00:01:29 字数 613 浏览 1 评论 0原文

我正在使用以下脚本,但很难传递我使用用户位置创建的纬度和经度变量。当我删除我创建的纬度和经度变量并手动输入 0,0 时,它可以很好地进行调试...地图应该显示在 id 为 map_canvas 的 div 中。

$(document).ready(function() {
navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors);  
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var latlng = new google.maps.LatLng(lat, lon);
    var myOptions = {
    zoom: 1,
    center: latlng,
    disableDefaultUI: true,
    mapTypeId: google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
    myOptions); map.setTilt(45); 
});  

I'm using the following script and having difficulty passing in the lat and lon variables I created with user's location. When I remove the lat and lon variables I created and manually put in 0,0 it works fine for debugging's sake...The map should be displayed in a div with id of map_canvas.

$(document).ready(function() {
navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors);  
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var latlng = new google.maps.LatLng(lat, lon);
    var myOptions = {
    zoom: 1,
    center: latlng,
    disableDefaultUI: true,
    mapTypeId: google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
    myOptions); map.setTilt(45); 
});  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝天白云 2024-12-04 00:01:29

您的问题可能就在这里:

navigator.geolocation.getCurrentPosition(handle_geolocation_query, handle_errors);

大概,handle_geolocation_query看起来像这样:

var position;
function handle_geolocation_query(pos) {
    position = pos;
}

但是navigator.geolocation.getCurrentPosition 是一个异步函数调用:

getCurrentPosition() 方法采用一个、两个或三个参数。调用时,它必须立即返回,然后异步尝试获取设备的当前位置。如果尝试成功,则必须调用 successCallback [...]

强调我的。

因此,您的 handle_geolocation_query 回调将在 getCurrentPosition 拥有位置时调用,而不是在您调用 getCurrentPosition 时调用。因此,当您到达此处时:

var lat = position.coords.latitude;
var lon = position.coords.longitude;

您的 position 不一定包含任何有用的内容,并且您可能会收到一条错误消息,告诉您 position.coords 未定义或不是对象,或者类似的东西。

您需要将 new google.maps.LatLngnew google.maps.Map 内容移至 handle_geolocation_query 回调中,您将在其中获得有用的位置值。

Your problem is probably right here:

navigator.geolocation.getCurrentPosition(handle_geolocation_query, handle_errors);

Presumably, handle_geolocation_query looks something like this:

var position;
function handle_geolocation_query(pos) {
    position = pos;
}

But navigator.geolocation.getCurrentPosition is an asynchronous function call:

The getCurrentPosition() method takes one, two or three arguments. When called, it must immediately return and then asynchronously attempt to obtain the current location of the device. If the attempt is successful, the successCallback must be invoked [...]

Emphasis mine.

So, your handle_geolocation_query callback will be called by getCurrentPosition when it has the position, not when you call getCurrentPosition. So, when you get to here:

var lat = position.coords.latitude;
var lon = position.coords.longitude;

Your position won't necessarily contain anything useful and you probably get an error telling you that position.coords is undefined or not an object or something similar.

You need to move your new google.maps.LatLng and new google.maps.Map stuff to inside the handle_geolocation_query callback where you'll have a useful position value.

江心雾 2024-12-04 00:01:29

http://j.maxmind.com/app/geoip.js

http://maps.google.com/maps/api/js?sensor=true

 var ulat;
 var ulon;
 var map;

         if (navigator.geolocation)  {



       navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors);

            }

            else if (!navigator.geolocation)

            {  
             max();  
            } 
             else
            { 
             alert("Sorry user location can't be detected");
              initialize();   
            }   



//maxmind api for fallback
        function max()  
        { 
              ulat=geoip_latitude();
              ulon=geoip_longitude();

            initialize();
        }  


        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;  
            } 
        initialize();    
        } 

        function handle_geolocation_query(position){ 

        ulat=position.coords.latitude;
        ulon=position.coords.longitude;
      /* alert('Lat: ' + position.coords.latitude +  
                  ' Lon: ' + position.coords.longitude); */ 

        initialize();
        } 


     function initialize(){
     loc = new Array();
     geocoder = new google.maps.Geocoder();
      var latlng = new google.maps.LatLng(ulat,ulon);
       var myOptions = {
       zoom: 3,
       center: latlng,
       mapTypeId: google.maps.MapTypeId.ROADMAP
        };
     map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);



}//end of initialize

您好,地理定位本质上是异步的,因此不要将其与同步函数一起使用,否则新的 google.maps.LatLng(lat, lon) 函数可能会得到空值。根据您的要求尝试上面的代码。您可以在 jquery 中进行类似的工作。

http://j.maxmind.com/app/geoip.js

http://maps.google.com/maps/api/js?sensor=true

 var ulat;
 var ulon;
 var map;

         if (navigator.geolocation)  {



       navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors);

            }

            else if (!navigator.geolocation)

            {  
             max();  
            } 
             else
            { 
             alert("Sorry user location can't be detected");
              initialize();   
            }   



//maxmind api for fallback
        function max()  
        { 
              ulat=geoip_latitude();
              ulon=geoip_longitude();

            initialize();
        }  


        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;  
            } 
        initialize();    
        } 

        function handle_geolocation_query(position){ 

        ulat=position.coords.latitude;
        ulon=position.coords.longitude;
      /* alert('Lat: ' + position.coords.latitude +  
                  ' Lon: ' + position.coords.longitude); */ 

        initialize();
        } 


     function initialize(){
     loc = new Array();
     geocoder = new google.maps.Geocoder();
      var latlng = new google.maps.LatLng(ulat,ulon);
       var myOptions = {
       zoom: 3,
       center: latlng,
       mapTypeId: google.maps.MapTypeId.ROADMAP
        };
     map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);



}//end of initialize

Hi, geolocation is asynchronus by nature hence don't use it with synchronus function ,otherwise u may get null value for new google.maps.LatLng(lat, lon) function. try the above code according to your requirement.you can work similarly in jquery.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文