2012-10-17 109 views
1

我使用此處找到的代碼:Integrating Spiderfier JS into markerClusterer V3 to explode multi-markers with exact same long/lat限制縮放級別,當單擊MarkerClusterer時創建的羣集包含位於同一位置的點。在Google Maps API v3中使用MarkerClusterer限制縮放級別時出錯

活生生的例子是在這裏: http://www.adultlearnersfestival.com/newsite/yourarea/map.html

我在Firebug得到一個錯誤,但是:

Error: TypeError: markers is undefined 

,可以不知道是什麼導致了它。具體代碼是:

var minClusterZoom = 14; 
mc.setMaxZoom(minClusterZoom); 
gm.event.addListener(mc, 'clusterclick', function(cluster) { 
    map.fitBounds(cluster.getBounds()); // Fit the bounds of the cluster clicked on 
    if(map.getZoom() > minClusterZoom+1) // If zoomed in past 15 (first level without clustering), zoom out to 15 
    map.setZoom(minClusterZoom+1); 
}); 

任何幫助非常感謝。 - Tom

回答

1

我採取了不同的方法這裏建議:markerClusterer on click zoom和編輯MarkerClusterer源如下

從這個

/** 
* Triggers the clusterclick event and zoom's if the option is set. 
*/ 
ClusterIcon.prototype.triggerClusterClick = function() { 
    var markerClusterer = this.cluster_.getMarkerClusterer(); 

    // Trigger the clusterclick event. 
    google.maps.event.trigger(markerClusterer, 'clusterclick', this.cluster_); 

    if (markerClusterer.isZoomOnClick()) { 
    // Zoom into the cluster. 
    this.map_.fitBounds(this.cluster_.getBounds()); 
    } 
}; 

這個

/** 
* Triggers the clusterclick event and zoom's if the option is set. 
*/ 
ClusterIcon.prototype.triggerClusterClick = function() { 
    var markerClusterer = this.cluster_.getMarkerClusterer(); 

    // Trigger the clusterclick event. 
    google.maps.event.trigger(markerClusterer, 'clusterclick', this.cluster_); 

    if (markerClusterer.isZoomOnClick()) { 
    // Zoom into the cluster. 
    this.map_.fitBounds(this.cluster_.getBounds()); 

    // modified zoom in function 
     if(this.map_.getZoom() > markerClusterer.getMaxZoom()+1) 
      this.map_.setZoom(markerClusterer.getMaxZoom()+1); 
    } 
}; 
0

看起來像MarkerClusterer中的一個錯誤。裏面的for循環這個功能,標記是不確定的,這意味着this.getMarkers()的返回undefined,在我看來就像它是錯誤的:

/** 
* Returns the bounds of the cluster. 
* 
* @return {google.maps.LatLngBounds} the cluster bounds. 
*/ 
Cluster.prototype.getBounds = function() { 
    var bounds = new google.maps.LatLngBounds(this.center_, this.center_); 
    var markers = this.getMarkers(); 
    for (var i = 0, marker; marker = markers[i]; i++) { 
    bounds.extend(marker.getPosition()); 
    } 
    return bounds; 
}; 

大概應該是這樣的(未測試):

/** 
* Returns the bounds of the cluster. 
* 
* @return {google.maps.LatLngBounds} the cluster bounds. 
*/ 
Cluster.prototype.getBounds = function() { 
    var bounds = new google.maps.LatLngBounds(this.center_, this.center_); 
    var markers = this.getMarkers(); 
    if (markers && markers.length) 
    { 
    for (var i = 0; i < markers.length; i++) { 
     bounds.extend(markers[i].getPosition()); 
    } 
    } 
    return bounds; 
}; 

Works using MarkerClustererPlus

+0

嗨 - 感謝尋找這個。我按照建議修正了MarkerClusterer,並且當我不再收到Firebug錯誤消息時,縮放限制現在不起作用:http://www.adultlearnersfestival.com/newsite/yourarea/map.html - Tom –

+0

您的縮放限制代碼取決於防止變焦實際發生的錯誤。 – geocodezip

+0

好的 - 不好依靠我想的錯誤。我嘗試了一個稍微不同的方法,在另一個正在爲我工​​作的答案中概述。再次感謝 - 並非常感謝您的http://geocodezip.com/網站,這非常有幫助。 –

0

我解決了改變這個問題:

Cluster.prototype.getBounds = function() { 
    var bounds = new google.maps.LatLngBounds(this.center_, this.center_); 
    var markers = this.getMarkers(); 
    for (var i = 0, marker; marker = markers[i]; i++) { 
    bounds.extend(marker.getPosition()); 
    } 
    return bounds; 
}; 

這樣:

Cluster.prototype.getBounds = function() { 
    var bounds = new google.maps.LatLngBounds(this.center_, this.center_); 
    var markers = this.getMarkers(); 
    var minZoom =10 
    mc.setMaxZoom(minZoom);//The maximum zoom level that a marker can be part of a cluster 
    for (var i = 0; i < markers.length; i++) { 
    bounds.extend(marker.getPosition());//Extends this bounds to contain the given point. 
    } 
    if(map.getZoom() > minZoom+1){// If zoomed in past 11, the first level without clustering, zoom out to 11. 
    map.setZoom(minZoom+1); 
    } 
    return bounds; 

}; 
相關問題