7

我在向地圖添加標記羣集器功能時遇到問題。我想要的是對我的標記使用自定義圖標,每個標記都有自己的信息窗口,我希望能夠編輯。向Google地圖添加簡單標記羣集器

我做到了,但現在我添加標記羣集庫功能時遇到問題。我讀了一些關於向數組添加標記的內容,但我不確定它到底意味着什麼。此外,所有與數組相關的示例都找到了,沒有信息窗口,也沒有找到合適的方法來添加它們。

這裏是我的代碼(主要來自Geocodezip.com):

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
    <script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/src/markerclusterer.js"></script> 
    <style type="text/css"> 
    html, body { height: 100%; } 
    </style> 
<script type="text/javascript"> 
//<![CDATA[ 
var map = null; 
function initialize() { 
    var myOptions = { 
    zoom: 8, 
    center: new google.maps.LatLng(43.907787,-79.359741), 
    mapTypeControl: true, 
    mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}, 
    navigationControl: true, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    map = new google.maps.Map(document.getElementById("map_canvas"), 
           myOptions); 

var mcOptions = {gridSize: 50, maxZoom: 15}; 
var mc = new MarkerClusterer(map, [], mcOptions); 

     google.maps.event.addListener(map, 'click', function() { 
      infowindow.close(); 
      }); 

     // Add markers to the map 
     // Set up three markers with info windows 

      var point = new google.maps.LatLng(43.65654,-79.90138); 
      var marker1 = createMarker(point,'Abc'); 

      var point = new google.maps.LatLng(43.91892,-78.89231); 
      var marker2 = createMarker(point,'Abc'); 

      var point = new google.maps.LatLng(43.82589,-79.10040); 
      var marker3 = createMarker(point,'Abc'); 

      var markerArray = new Array(marker1, marker2, marker3); 
      mc.addMarkers(markerArray, true); 


} 

var infowindow = new google.maps.InfoWindow(
    { 
    size: new google.maps.Size(150,50) 
    }); 

function createMarker(latlng, html) { 
    var image = '/321.png'; 
    var contentString = html; 
    var marker = new google.maps.Marker({ 
     position: latlng, 
     map: map, 
     icon: image, 
     zIndex: Math.round(latlng.lat()*-100000)<<5 
     }); 

    google.maps.event.addListener(marker, 'click', function() { 
     infowindow.setContent(contentString); 
     infowindow.open(map,marker); 
     }); 
} 

//]]> 
</script> 

回答

12

這裏是工作jsfiddle demo

一旦你創建一個標記集羣,你將要標記添加到它。 MarkerClusterer支持添加使用addMarker()addMarkers()方法標記或通過提供標記的數組構造函數:

當他們說加標記通過提供標記的陣列它類似於這樣做是爲了構造器:

var markers = []; //create a global array where you store your markers 
for (var i = 0; i < 100; i++) { 
    var latLng = new google.maps.LatLng(data.photos[i].latitude, 
     data.photos[i].longitude); 
    var marker = new google.maps.Marker({'position': latLng}); 
    markers.push(marker); //push individual marker onto global array 
} 
var markerCluster = new MarkerClusterer(map, markers); //create clusterer and push the global array of markers into it. 

要使用它,你addMarker()基本上把它添加到集羣像下面添加:

var markerCluster //cluster object created on global scope 

//do your marker creation and add it like this: 
markerCluster.addMarker(newMarker, true); //if specify true it'll redraw the map 

,或者如果你想添加的數組:

var markerCLuster //cluster object created on global scope 

//do your marker creation and push onto array of markers: 
markerCluster.addMarkers(newMarkers, true); //if specify true it'll redraw the map 

下面是引用MarkerClustererSimple Examples

基於您的代碼片段的,你會想要做這樣的事情:

var mcOptions = {gridSize: 50, maxZoom: 15}; 
    var mc = new MarkerClusterer(map, [], mcOptions); 

     google.maps.event.addListener(map, 'click', function() { 
      infowindow.close(); 
      }); 

     // Add markers to the map 
     // Set up three markers with info windows 

      var point = new google.maps.LatLng(43.65654,-79.90138); 
      var marker1 = createMarker(point,'Abc'); 

      var point = new google.maps.LatLng(43.91892,-78.89231); 
      var marker2 = createMarker(point,'Abc'); 

      var point = new google.maps.LatLng(43.82589,-79.10040); 
      var marker3 = createMarker(point,'Abc'); 

      var markerArray = new Array(marker1, marker2, marker3); 
      mc.addMarkers(markerArray, true); 

你不是被命名正確創建制造商所有的標記到相同的var ,所以你實際上正在創建三個標記,並且每次將它存儲在var標記中時都會被寫入。所以我繼續並重新命名你的標記。然後我創建了一個數組來存儲它們並傳遞給MarkerClusterer。

UPDATE:您createMarker功能,你沒有返回標記,然後,沒有標記集羣:

function createMarker(latlng, html) { 
    var contentString = html; 
    var marker = new google.maps.Marker({ 
     position: latlng, 
     map: map, 
     zIndex: Math.round(latlng.lat() * -100000) << 5 
    }); 

    google.maps.event.addListener(marker, 'click', function() { 
     infowindow.setContent(contentString); 
     infowindow.open(map, marker); 
    }); 

    return marker; 
} 
+0

@ kjy112謝謝您的回答。我按照您的建議更改了我的代碼,但仍然只有3個自定義圖標可見。當我縮小時,沒有任何變化,沒有集羣圖標。我用當前的代碼更新了我的問題。 – take2 2011-03-10 15:23:38

+0

@ take2你如何設置羣集? – kjy112 2011-03-10 15:24:13

+0

@ kjy112我不確定你的意思。使用var mcOptions = {gridSize:50,maxZoom:15}; var mc = new MarkerClusterer(map,[],mcOptions); var markerArray = new Array(marker1,marker2,marker3); mc.addMarkers(markerArray,true); – take2 2011-03-10 15:27:58