Google地图(geocoder.geocode)使用地址而不是LatLng时会中断标记列表

新浪咏叹调

小伙子,请看看我的Google地图。当我使用LatLng显示位置时,它可以工作。问题是使用地理编码将地址转换为LatLng。

尽管标记仅显示第一个地址,但Infowindow不会附加到该地址。同样,在循环中到达第一个地址后,它会中断,并且不再显示其他位置或地址。它显示所有带有latLng的位置,但是到达地址时会错过行为(使用地址解析)

链接到完整的代码,位于: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);

您的代码有两个问题。

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

其次,在地址解析回调中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
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

避免在Google地图中使用geocoder.geocode,并使用变量传递给google.maps.Marker

来自分类Dev

Android Geocoder找不到地址,谷歌地图可以找到

来自分类Dev

Geocoder自动将Google地图向左平移

来自分类Dev

无法使用企业帐户在 Google Geocoder API 上搜索地址

来自分类Dev

Google map api geocoder.geocode 返回相同的值

来自分类Dev

Google地图-在地图中显示许多标记

来自分类Dev

Android Google地图。按地址添加标记

来自分类Dev

Android Google地图。按地址添加标记

来自分类Dev

Google地图上的多个标记

来自分类Dev

Google地图上的多个标记

来自分类Dev

清除Google地图上的标记?

来自分类Dev

在Google地图中添加标记

来自分类Dev

标记消失在Google地图中

来自分类Dev

Google CSE:当我使用“é”等特殊字符进行搜索时会中断

来自分类Dev

Google CSE:当我使用“é”之类的特殊字符进行搜索时会中断

来自分类Dev

在HTML中使用地图标记时,Google Chrome会绘制多边形

来自分类Dev

GeoCoder使用地址获取经度和纬度

来自分类Dev

在Google地图上使用MarkerWithLabel隐藏标记

来自分类Dev

使用按钮在Google地图上添加标记

来自分类Dev

Google地图突然无法使用

来自分类Dev

使用php的简单Google地图

来自分类Dev

Google地图突然无法使用

来自分类Dev

使用php的简单Google地图

来自分类Dev

Google地图在LatLng边界之间放大

来自分类Dev

Google地图地址和地图显示(超出限制图片)

来自分类Dev

Google地图未呈现,但标记/集群工作(灰色地图区域)

来自分类Dev

Firebase强制使用地图而不是数组

来自分类Dev

如何在Google标记地图中添加地址?

来自分类Dev

通过在 Google 地图上拖放标记获取用户地址

Related 相关文章

热门标签

归档