Google地理编码 - 不会显示自定义标记

时间:2013-12-26 21:00:16

标签: google-maps google-maps-api-3 geocoding

我正在尝试将Google Maps API与地理编码结合使用。 标记显示在所需位置,但只显示红色图标,就好像该功能忽略了图标参数一样。

注意,我有相同的代码而没有地理编码,标记图标显示为应该的,只有地理编码存在问题。

这里是代码:

<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="jquery.xml2json.js" type="text/javascript" language="javascript"></script>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>
var markers = [];
  $.get('Customers.xml', function(xml) {
      var jsonObj = $.xml2json(xml);
        $.each(jsonObj.Marker, function(){
            var stat = this.site_status == "Critical" ? "redgoogle.png" : "green_marker.png";
                 var mark = {
                        title: this.title,
                        location: this.site_location,
                        lati: this.latitude,
                        longi: this.longitude,
                        icon: stat
                        }
                markers.push(mark);
        });
});     

function initialize() {
    var chicago = new google.maps.LatLng(35.442579,-40.895920);
    var mapOptions = {
        zoom: 4,
        center: chicago,
        mapTypeId: google.maps.MapTypeId.ROADMAP
  }

  var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  for(var i=0; i< markers.length; i++){
    var maddress = markers[i].location;
    var image = markers[i].icon;
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': maddress}, function(results, status) { 

        if (status == google.maps.GeocoderStatus.OK)
        {   
            var myLatlng = new google.maps.LatLng(results[0].geometry.location.lat(),results[0].geometry.location.lng());
            var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
            var marker = new google.maps.Marker({ position: results[0].geometry.location,icon: image,map:map });
        }
    else
    {
        alert("Geocode was not successful for the following reason: " + status);
    }
    });
  } 
}
google.maps.event.addDomListener(window, 'load', initialize);
debugger;
    </script>
  </head>
  <body>
     <div id="map-canvas"></div>
  </body>
</html>

提前致谢。

1 个答案:

答案 0 :(得分:2)

地理编码是异步的,当循环结束时i = marker.length

var image = markers[markers.length].icon;

不是有效的图标。

您可以通过函数闭包来解决此问题,将参数传递给函数以将图标与地理编码器响应相关联:

  for(var i=0; i< markers.length; i++){
    var maddress = markers[i].location;
    var image = markers[i].icon;
    geocodeAddress(maddress, image, map); 
  } 


function geocodeAddress(maddress, image, map) {
  var geocoder = new google.maps.Geocoder();
  geocoder.geocode( { 'address': maddress}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) {   
      var myLatlng = new google.maps.LatLng(results[0].geometry.location.lat(),results[0].geometry.location.lng());
      var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
      var marker = new google.maps.Marker({ position: results[0].geometry.location,icon: image,map:map });
    } else {
      alert("Geocode was not successful for the following reason: " + status);
    }
  });
}

页面加载事件和markers数组的AJAX加载之间也存在竞争条件。

working example