谷歌地图(geocoder.geocode)在使用地址而不是LatLng时打破了标记列表

时间:2014-09-03 07:36:09

标签: javascript jquery google-maps google-maps-api-3 google-geocoder

CHAPS, 请看看我的谷歌地图。它在我使用LatLng显示位置时有效。问题是使用Geocode将地址转换为LatLng。

虽然标记仅显示第一个地址,但Infowindow不会附加到它。此外,在循环中到达第一个地址后,它会中断并且不再显示任何位置或地址。它显示了latLng的所有位置,但是当到达地址时使用miss(使用地理编码)

完整代码的链接已开启:http://jsfiddle.net/6cs2uLrL/8/

  var clients_details;
  var infowindow =[];
  var content =[
                 'QLD marker','SA marker','WA marker','VIC marker','random marker'
                  ];
  var geocoder;
  var map;
  var clients_details =
       [
         {location:"-26.6719304,153.0889225,17z",address: null},
         {location:"-34.927883,138.610688,17",address: null},                                             
         {location: null ,address:"Perth, WA"},
         {location: null ,address:"Melbourne, VIC"},
         {location:"-39.927883,150.610688,17",address: null}
       ];



   /**
   *
   * Draw google map with markers
   */
  function initialize()
  {

    geocoder = new google.maps.Geocoder();
    // Sydney
    var defaultLatlng = new google.maps.LatLng(-34.397, 150.644);

    var mapOptions =
    {
      center: defaultLatlng ,
      //disableDefaultUI:true,
      mapTypeControl: false,
      zoomControl: true,
      zoomControlOptions: {
      style: google.maps.ZoomControlStyle.SMALL
      },
      zoom: 3
    };

    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    // Makes sure clients_details is populated
   if (clients_details != null)
      showMarkers(clients_details);
 } // initialize
 /**
  * Asigning info to the  markers on the map
  * @param {list} clients_details
  *
  */
 function showMarkers(clients_details)
 {

   var marker = [];
   for (var i = 0; i < clients_details.length; i++)
   {

       infowindow[i] = new google.maps.InfoWindow({ content: content[i] });           
       var client_location='';
       if (clients_details[i]['location'] !== null)
       {
       // Geting Lat and Lng from the database string
       LatLng = clients_details[i]['location'].split(',');
       // Making number from string
       Lat = Number (LatLng[0]);
       Lng = Number(LatLng[1]);
       client_location = new google.maps.LatLng (Lat,Lng);
       marker[i] = new google.maps.Marker({
         position: client_location,
         map: map,
         title:  'LatLng'
           });

       }
       else
       {

         client_address = clients_details[i]['address'];
         geocoder.geocode(
           { 'address': client_address},
           function(results, status)
           {
             debugger;
             if (status == google.maps.GeocoderStatus.OK)
             {
               client_location = results[0].geometry.location;
               marker[i] = new google.maps.Marker({
                 position: client_location,
                 map: map,
                 title: 'address' 
               });
             }
             else
               alert('Geocode was not successful for the following\n\
                      reason: ' + clients_details[i]['name']+'\n' + status);
           });
       }
       // Add 'click' event listener to the marker
       addListenerMarkerList(infowindow[i], map, marker[i]);
   }// for
 }// function

 /*
  * Adds a click listner to the marker object
  *
  *   */
 function addListenerMarkerList(infowindow, map, marker )
 {       
   google.maps.event.addListener(marker, 'click', function() {
     infowindow.open(map,marker);
      });
 }     

google.maps.event.addDomListener(window, 'load', initialize);

1 个答案:

答案 0 :(得分:0)

您的代码存在两个问题。

首先,在for循环结束时,您正在调用addListenerMarkerList(infowindow[i], map, marker[i]);。但是,如果您要对地理位置进行地理编码,则只会在地理编码器返回后(在marker[i]回调中)创建function(results, status)。要解决此问题,您需要在addListenerMarkerList分支以及地理编码回调中调用if

其次,在地理编码回调function(results, status)中,您正在访问循环变量i。但是,for循环不会创建新范围。这意味着i的值不会包含在您的回调中。执行回调时,for循环将结束,因此i将等于clients_details.length。解决方法是使用函数闭包。

从概念上讲,固定代码看起来像这样(JSFiddle)。

for (var i = 0; i < clients_details.length; i++) {
    ...
    if (clients_details[i]['location'] !== null) {
        ...
        marker[i] = new google.maps.Marker(...);
        addListenerMarkerList(infowindow[i], map, marker[i]);
    } else {
        ....
        geocoder.geocode(..., (function(i) {
            return function(results, status) {
                ...
                marker[i] = new google.maps.Marker(...);
                addListenerMarkerList(infowindow[i], map, marker[i]);
            }
        })(i));
    }

    //addListenerMarkerList(infowindow[i], map, marker[i]); // Will not work
}