Google地图标记错误位置

问题描述:

我在地图上有多个标记。但它不会将相应的infoWindows附加到标记。他们总是在左上角。不知道这里干扰什么。Google地图标记错误位置

http://jsfiddle.net/kcr4zd88/

var geocoder = new google.maps.Geocoder(); 

    var addresses = [ 
     'Marienstr. 37a 27472 Cuxhaven', 
     'Bahnhofstr. 15 21745 Hemmoor', 
     'Richtpfad 20 26506 Norden', 
     'Eulenbusch 4 21391 Reppenstedt' 
    ]; 

    var bounds = new google.maps.LatLngBounds(); 

    var map = new google.maps.Map(document.getElementById('map'), { 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }); 

    var infowindow = new google.maps.InfoWindow(), marker, i; 

    for (i=0; i < addresses.length; i++) { 
     geocoder.geocode({ 'address': addresses[i]}, function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
      // map.setCenter(results[0].geometry.location); 
      marker = new google.maps.Marker({ 
       map: map, 
       position: results[0].geometry.location 
      }); 

      google.maps.event.addListener(marker, 'click', (function(marker, i) { 
       return function() { 
        infowindow.setContent(addresses[i]); 
        infowindow.open(map, marker); 
       }; 
      })(marker, i)); 

      bounds.extend(results[0].geometry.location); 
      map.setCenter(bounds.getCenter()); 
      map.fitBounds(bounds); 
      } else { 
      alert("Geocode was not successful for the following reason: " + status); 
      } 
     }); 
    } 

它未能这里(如MrUpsidown观察到的)infowindow.setContent(addresses[i]);因为当点击监听运行i = 4的地址和[4]中不存在。

解决您的问题的一种方法是对地理编码功能以及标记(下面)使用函数关闭。但正如Upsidown先生所观察到的,如果你有超过10个地址,它将无法工作。

function geocodeAddress(addresses, i) { 
    geocoder.geocode({ 'address' : addresses[i]}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     // map.setCenter(results[0].geometry.location); 
     marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location 
     }); 

     google.maps.event.addListener(marker, 'click', (function(marker, i) { 
      return function() { 
       infowindow.setContent(addresses[i]); 
       infowindow.open(map, marker); 
      }; 
     })(marker, i)); 

     bounds.extend(results[0].geometry.location); 
     map.setCenter(bounds.getCenter()); 
     map.fitBounds(bounds); 
     } else { 
     alert("Geocode was not successful for the following reason: " + status); 
     } 
    }); 
} 

工作代码片段:

function geocodeAddress(addresses, i) { 
 
     geocoder.geocode({ 'address' : addresses[i]}, function(results, status) { 
 
      if (status == google.maps.GeocoderStatus.OK) { 
 
      // map.setCenter(results[0].geometry.location); 
 
      marker = new google.maps.Marker({ 
 
       map: map, 
 
       position: results[0].geometry.location 
 
      }); 
 

 
      google.maps.event.addListener(marker, 'click', (function(marker, i) { 
 
       return function() { 
 
        infowindow.setContent(addresses[i]); 
 
        infowindow.open(map, marker); 
 
       }; 
 
      })(marker, i)); 
 

 
      bounds.extend(results[0].geometry.location); 
 
      map.setCenter(bounds.getCenter()); 
 
      map.fitBounds(bounds); 
 
      } else { 
 
      alert("Geocode was not successful for the following reason: " + status); 
 
      } 
 
     }); 
 
    } 
 

 
    var geocoder = new google.maps.Geocoder(); 
 
    
 
    var addresses = [ 
 
     'Marienstr. 37a 27472 Cuxhaven', 
 
     'Bahnhofstr. 15 21745 Hemmoor', 
 
     'Richtpfad 20 26506 Norden', 
 
     'Eulenbusch 4 21391 Reppenstedt' 
 
    ]; 
 
    
 
    var bounds = new google.maps.LatLngBounds(); 
 
    
 
    var map = new google.maps.Map(document.getElementById('map'), { 
 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }); 
 
    
 
    var infowindow = new google.maps.InfoWindow(), marker, i; 
 
    
 
    for (i=0; i < addresses.length; i++) { 
 
     geocodeAddress(addresses,i); 
 
    }
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> 
 
<div id="map" style="width: 500px; height: 400px;"></div>

地理编码器是异步的。您的for循环在第一个地理编码请求获得响应之前结束。所以你不能使用i,因为它永远是4addresses[4]不存在。

的谷歌地图API提供了地理编码地理编码器类和反向从用户输入动态天气预报

无论如何,因为你不应该做这种方式。当你第一次加载API时,你将被分配一个地理编码请求的初始配额。一旦你使用了这个配额,额外的请求将以每秒为基础进行速率限制。如果您希望对静态的已知地址进行地理编码,请参阅地理编码Web服务文档。

https://developers.google.com/maps/documentation/javascript/geocoding

如果地理编码大量的地址,特别是在循环(每秒限制),您可能会碰到的限制。

更多内容:https://developers.google.com/maps/articles/geocodestrat