使用geoCoder的群集标记Google地图 [英] Cluster Marker Google Maps using geoCoder

查看:92
本文介绍了使用geoCoder的群集标记Google地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用这些数据

Using this data

   var data = {
  'address': [
    {
      'address': '7970 GLENTIES Ln'
    },
    {
      'address': '8022 Caminito Mallorca'
    },
    {
      'address': '2750 Wheatstone St # 26'
    },
    {
      'address': '335 49th St'
    }  
  ]
};

我设法标记它们,但我无法对它们进行聚类。低于我使用的代码。

i manage to marker them but i was unable to cluster them. below the code that I've use.

var markerArray = [];
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var center = new google.maps.LatLng(37.4419, -122.1419);

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 3,
      center: center,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    var address;
    var markers = [];
    for (var level in data) {
        for (var i = 0; i < data[level].length; i++) {
       //   var dataPhoto = data.photos[i];
          var dataAdd =  data[level][i];
        //  alert(dataAdd.address);
        geocoder.geocode({ 'address': dataAdd.address}, function(results){            
          var marker  = new google.maps.Marker({
              map: map, 
              position: results[0].geometry.location
          });
         markerArray[i] = marker;         
        });
        }
    }
    var markerCluster = new MarkerClusterer(map, markerArray);
  }

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

我如何群集它们?它似乎是MarkerClusterer不工作,或者我不知道。

How can i cluster them? it seem that the MarkerClusterer is not working or i dont know.

推荐答案

因为地理编码功能是异步的,你需要创建标记在创建标记之前使用clusterer。

Because the geocodeing function is async you need to create the marker clusterer before the markers have been created.

你应该做的是在开始地理编码之前创建MarkerClusterer,然后添加到标记数组中,而不是添加到标记数组中到MarkerClusterer。

what you should do is create the MarkerClusterer before you start geocoding and then instead of adding to a marker array you can instead just add it to the MarkerClusterer.

这篇关于使用geoCoder的群集标记Google地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆