谷歌地图 - 聚类标记

时间:2013-01-03 10:34:31

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

我有一个Google Map(使用API​​ V3),我需要在其中设置标记聚类。基本上标记位置是通过JSON加载的,它返回问题(1或2),纬度和经度。

我尝试设置群集,但即使JSON调用实际返回点,地图上也没有显示任何内容。我想出了以下内容:

var map;

function myMap(){   
  var latlng = new google.maps.LatLng(35.94, 14.37);

var mapOptions = {
zoom: 10,
center: latlng,
panControl: false,
mapTypeControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scaleControl: false,
streetViewControl: false,
overviewMapControl: false,
zoomControl: true};

map = new google.maps.Map(document.getElementById("map"), mapOptions);  
loadMarkers();
}

function loadMarkers() {    

var mcOptions = {gridSize: 50, maxZoom: 15};
var issueSevereMarkers = [];
var issueSlightMarkers = [];

 markerIssueHigh = new google.maps.MarkerImage("img/icn_issue_high.png",
new google.maps.Size(10.0, 10.0),
new google.maps.Point(0, 0),
new google.maps.Point(5.0, 5.0)
);

markerIssueMedium = new google.maps.MarkerImage("img/icn_issue_medium.png",
new google.maps.Size(10.0, 10.0),
new google.maps.Point(0, 0),
new google.maps.Point(5.0, 5.0)
);

var issuePoints = $.ajax({
  type: 'GET',
  url: 'http://www.mydomain.com/php/markers.php?&jsoncallback=?',
  dataType: 'JSONp',
  timeout: 5000,
  success: function(data) {         
        $.each(data, function(i,item){

        if (item.Issue == 1) {  
            // If severely congested                
              var latLng = new google.maps.LatLng(item.Latitude, item.Longitude);
              var issueSevereMarker = new google.maps.Marker({'position': latLng});
              issueSevereMarkers.push(issueSevereMarker);

        } else if (item.Issue == 2) {
            // If slightly congested
              var latLng = new google.maps.LatLng(item.Latitude, item.Longitude);
              var issueSlightMarker = new google.maps.Marker({'position': latLng});
              issueSlightMarkers.push(issueSlightMarker);
        }   

    });

    var markerClusterSevere = new MarkerClusterer(map, issueSevereMarkers);
    var issueSlightMarkerCluster = new MarkerClusterer(map, issueSlightMarkers);
  },
  error: function(data) {

  }
});     
}

基本上我正在做的是从JSON获得结果,如果问题是类型1,我将一个引脚添加到issueSevereMarkers数组,否则我将它添加到issueSlightMarkers数组。这应该使用标记的点加载数组。我不确定我在那里做错了什么。

我还有一个问题是,当标记被分组时,群集器基本上会返回一个自定义图标。当我放大并查看单个标记时,如何设置标记以使用markerIssueHigh和markerIssueMedium图像?

我正在使用Clustere for the V3(http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/docs/reference.html)

1 个答案:

答案 0 :(得分:1)

要实际使用自定义图标,您需要在google.maps.Marker构造函数

中引用它们
    if (item.Issue == 1) {  
        // If severely congested                
          var latLng = new google.maps.LatLng(item.Latitude, item.Longitude);
          var issueSevereMarker = new google.maps.Marker({position: latLng, 
                                                          icon:markerIssueHigh});
          issueSevereMarkers.push(issueSevereMarker);

    } else if (item.Issue == 2) {
        // If slightly congested
          var latLng = new google.maps.LatLng(item.Latitude, item.Longitude);
          var issueSlightMarker = new google.maps.Marker({position: latLng,
                                                          icon:markerIssueMedium});

          issueSlightMarkers.push(issueSlightMarker);
    }   

没有看到任何显而易见的事情,这里有一个link到一个带有clusterer的工作地图(从XML而不是JSON获取数据)。

similar question会向群集添加自定义标记。