简体   繁体   English

Google Maps javascript API + HTML5地理位置怪癖

[英]Google Maps javascript API + HTML5 geolocation quirk

I'm having a rather strange issue with the Google Maps javascript API and HTML5 geolocation. 我在使用Google Maps javascript API和HTML5地理位置时遇到了一个很奇怪的问题。

I have the following executed onload: 我执行了以下onload:

  var geocoder;
  var map;
  var latlng;
  function initialize() {
 if(navigator.geolocation){
  navigator.geolocation.getCurrentPosition(function(position){
   latlng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
  });
 }else{
  latlng = new google.maps.LatLng(geoip_latitude(),geoip_longitude());
 }

 geocoder = new google.maps.Geocoder();
 var myOptions = {
   zoom: 8,
   center: latlng,
   streetViewControl: false,
   mapTypeId: google.maps.MapTypeId.HYBRID 
 };
 map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
   google.maps.event.addListener(map, 'click', function(event) {
  placeMarker(event.latLng);
  var latLong = String(event.latLng).replace('(', '');
  latLong = latLong.replace(')', '');
  $('#latlng').attr('value', latLong);
   });

   function placeMarker(location) {
  if(undefined != initialize.marker){
   initialize.marker.setMap(null);
  }
  initialize.marker = new google.maps.Marker({
   position: location, 
   map: map
  });
  initialize.marker.setMap(map);
  map.setCenter(location);
 }
  }

Now, what this should do is if the browser supports geolocation, get the lat+long, create a map, and display the map centered on the lat+long. 现在,这是什么应该做的是浏览器是否支持地理定位,得到了纬度+长,创建地图,并显示集中在纬度+长的地图。

It works just fine if I use the geoip_ functions (maxmind), but the geolocation has a strange issue: 如果我使用geoip_函数(maxmind),它可以正常工作,但是地理位置存在一个奇怪的问题:

If the code is run as is, the google maps display shows up as a gray box with no map and no controls. 如果代码按原样运行,则Google地图显示将显示为没有地图且没有控件的灰色框。

If I add an alert(); 如果我添加一个alert(); immediately after 之后立马

if(navigator.geolocation){
    navigator.geolocation.getCurrentPosition(function(position){
        latlng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
    });
}else{
    latlng = new google.maps.LatLng(geoip_latitude(),geoip_longitude());
}

the map displays just fine. 地图显示得很好。

What's up with that? 那是怎么回事?

The W3C Geolocation API is asynchronous ; W3C Geolocation API是异步的 the success callback function you have (which sets the variable latlng ) won't be called immediately -- the browser requires some time to actually do the geolocating; 您拥有的成功回调函数(设置变量latlng )不会立即被调用-浏览器需要一些时间才能真正进行地理位置定位; in fact, if the user takes some time to read the privacy prompt and decide whether or not to give permission, this process could take many seconds. 实际上,如果用户花一些时间阅读隐私提示并决定是否授予许可,则此过程可能需要花费几秒钟的时间。 In the meantime though, your map is loaded immediately. 但是,与此同时,您的地图会立即加载。

You're probably finding that it works with an alert after it because the alert gives the browser some time to finish the geolocation process and call the callback before getting to the code that loads the map. 您可能会发现它在警报之后起作用,因为警报使浏览器有一些时间来完成地理定位过程并在获取加载地图的代码之前调用回调。 (Just waiting a second or two isn't a good solution though: some users and browsers will take much longer than that before revealing location.) (不过,等待一两秒钟并不是一个好的解决方案:某些用户和浏览器所花的时间比显示位置之前要长得多。)

To fix: call the function that creates the map (you'll want to encapsulate practically everything after the geolocation call) and call it both in the success callback for getCurrentPosition and in the geoip else branch. 要修复:调用创建地图的函数(您将希望在地理位置调用之后封装几乎所有内容),并在getCurrentPosition的成功回调和geoip else分支中都对其进行调用。 That way you'll only try to load the map after you're guaranteed that the latlng variable has been appropriately filled. 这样,只有在确保正确填充latlng变量后,您才尝试加载地图。

I worked through this mess for the first time this morning (honestly I am surprised the maps API doesn't have a function to deal with this). 我今天早上第一次解决了这个烂摊子(老实说,我很惊讶maps API没有处理该问题的功能)。 Here is My Solution: JSFIddle 这是我的解决方案: JSFIddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM