Google Geocoding - custom marker does not appear

无人久伴 提交于 2019-12-24 13:10:15

问题


I am trying to use the Google Maps API with Geo-coding. The marker's appear in the needed location but only red icon appear, as if the function ignores the icon parameter.

Note, I have the same code without Geo-coding and the marker icon's appear as they should, the issue exists only with Geo-coding.

Here the code:

<!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>

Thanks in advance.


回答1:


geocoding is asynchronous, when the loop ends i = marker.length

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

Is not a valid icon.

You can fix this with function closure, pass the arguments into a function to associate the icon with the geocoder response:

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

You also have a race condition between the page load event and the AJAX load of the markers array.

working example



来源:https://stackoverflow.com/questions/20791188/google-geocoding-custom-marker-does-not-appear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!