2014-04-06 87 views
0

我想要做的是將markerclusterer添加到集成在網頁中的地圖的實際代碼中。所有的標記都是通過其他地方生成的xml文件創建的。 地圖工作正常,沒有markerclusterer,所有加載都很好。MarkerClusterer xml標記

我找不出我的代碼有什麼問題。也許我沒有得到markerclusterer如何正確工作。我希望你能幫助我。

我添加到我的初始(工作)代碼是什麼3行:

  • 變種markersclust = [];
  • markersclust.push(point);
  • var markerCluster = new MarkerClusterer(map,markersclust);

所有的內部「腳本標籤」,但在一個PHP文件。

echo' 
function load() { 
var map = new google.maps.Map(document.getElementById("map"), { 
center: new google.maps.LatLng(40.8333,14.25), 
zoom: 5, 
mapTypeId: \'roadmap\' 
}); 
var infoWindow = new google.maps.InfoWindow; 

downloadUrl("xml_map_public.php", function(data) { 
var xml = data.responseXML; 
var markersclust = []; 
var markers = xml.documentElement.getElementsByTagName("marker"); 
for (var i = 0; i < markers.length; i++) { 
var name = markers[i].getAttribute("name"); 
var uid = markers[i].getAttribute("uid"); 
var address = markers[i].getAttribute("address"); 
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")), 
parseFloat(markers[i].getAttribute("lng")); 
markersclust.push(point); 
var html = "<a href=\"http://www.facebook.com/"+ uid +"\" target=\"_new\"><b>" + name + "</b></a> <br/>" + address; 
var marker = new google.maps.Marker({ 
map: map, 
position: point 
}); 
bindInfoWindow(marker, map, infoWindow, html); 
} 
}); 
var markerCluster = new MarkerClusterer(map, markersclust); 
} 

function bindInfoWindow(marker, map, infoWindow, html) { 
google.maps.event.addListener(marker, \'click\', function() { 
infoWindow.setContent(html); 
infoWindow.open(map, marker); 
}); 
} 

function downloadUrl(url, callback) { 
var request = window.ActiveXObject ? 
new ActiveXObject(\'Microsoft.XMLHTTP\') : 
new XMLHttpRequest; 

request.onreadystatechange = function() { 
if (request.readyState == 4) { 
request.onreadystatechange = doNothing; 
callback(request, request.status); 
} 
}; 

request.open(\'GET\', url, true); 
request.send(null); 
} 

function doNothing() {} 

'; 
+0

只是可以肯定...你補充一點: <腳本類型= 「文/ JavaScript的」 SRC =「http://google-maps-utility-library-v3.googlecode.com/ svn/tags/markerclusterer/1.0/src/markerclusterer.js「> – smoore4

+0

是的,我在前面的腳本中添加了markerclusterer.js ...我甚至試過你的,但沒有任何改變!無論如何感謝 – user3407913

回答

0

我想你可能想這你的情況:

var marker = new google.maps.Marker({ 
map: map, 
position: point 
}); 

markersclust.push(marker); 

這裏是我作爲基礎用一個例子。

<script type='text/javascript'> 

var mapOpts = {mapTypeId: google.maps.MapTypeId.ROADMAP}; 
var map = new google.maps.Map(document.getElementById("map"), mapOpts); 
var infoWindow = new google.maps.InfoWindow();var markerBounds = new google.maps.LatLngBounds(); 
var markerArray = []; 

function makeMarker(options) 
{var pushPin = new google.maps.Marker({map:map}); 
pushPin.setOptions(options); 
google.maps.event.addListener(pushPin, "click", function(){infoWindow.setOptions(options);infoWindow.open(map, pushPin);}); 
markerBounds.extend(options.position); 
markerArray.push(pushPin); 
return pushPin;} 

google.maps.event.addListener(map, "click", function(){infoWindow.close();}); 

makeMarker({title: "Berlin, Germany",position: new google.maps.LatLng(52.5234051, 13.4113999),content:""}); 
makeMarker({title: "220 E 42nd St, New York, NY 10017, USA",position: new google.maps.LatLng(40.7501762, -73.9731466),content:""}); 
makeMarker({title: "San Francisco, CA, USA",position: new google.maps.LatLng(37.7749295, -122.4194155),content:""}); 
makeMarker({title: "Middletown, OH, USA",position: new google.maps.LatLng(39.5150576, -84.3982763),content:""}); 
makeMarker({title: "Twin Lakes, Salisbury, CT 06068, USA",position: new google.maps.LatLng(42.0264482, -73.3870791),content:""}); 
makeMarker({title: "Dearborn, MI, USA",position: new google.maps.LatLng(42.3222599, -83.1763145),content:""}); 
makeMarker({title: "Essen, Germany",position: new google.maps.LatLng(51.4556432, 7.0115552),content:""}); 

map.fitBounds(markerBounds); 
var mc = new MarkerClusterer(map, markerArray); 

</script> 
+0

感謝您的回答,但這並不能解決我的問題 – user3407913

+0

那麼你是如何解決它的? – smoore4

+0

尚未解決 - 我正在編寫我的Web應用程序的其他部分。當我弄明白時,我會發布解決方案。 – user3407913