2014-07-03 67 views
0

我真的很希望你能幫上忙,因爲我整天都把頭髮拉出來! 我們正在開發一個房地產代理網站,作爲其中的一部分,我期待爲主頁開發一個集羣地圖,它將鏈接到所有的房產。爲什麼我在自定義Google地圖中看不到任何標記羣?

我已經從我添加的地址對幾個虛擬位置進行了地理編碼,但是當我添加了代碼以創建羣集時,它看起來並不工作。 我開始認爲我沒有爲此添加足夠的位置來點擊,但我所看到的演示只有2個標記。

下面是我到目前爲止的代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
<script src="http://maps.google.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript" src="https://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/src/markerclusterer.js"></script> 
<script type="text/javascript" src="https://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/src/data.json"></script> 
<script type="text/javascript"> 

    function initialize() { 

     var myOptions = {center: new google.maps.LatLng(54, -2), zoom: 2, mapTypeId: google.maps.MapTypeId.ROADMAP}; 
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
var addressArray = new Array("3 Ard Na Cluana, Warrenpoint, BT34 3GY","19 Great Georges Street, Warrenpoint","Warrenpoint","BT34 3FS","15 Ard Na Cluana, Warrenpoint, BT34 3GY","10 Carrowmenagh Lane, Maghera","Belfast","49.286955, -123.118479"); 
var geocoder = new google.maps.Geocoder(); 
var markerBounds = new google.maps.LatLngBounds(); 

     var markers = []; 

     for (var i = 0; i < 100; i++) { 

      geocoder.geocode({ "address": addressArray[i]}, function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location}); 
       markers.push(marker); 
       markerBounds.extend(results[0].geometry.location); 
map.fitBounds(markerBounds); 
      } 
}); 
} 

     var markerCluster = new MarkerClusterer(map, markers); 
     } 

</script> 

</head> 

<body> 
<div id="map_canvas" style="width:600px; height: 400px;">&nbsp;</div> 
<script type="text/javascript"> 
initialize(); 
</script> 
</body> 
</html> 

我真的很感激,如果你能測試和任何幫助或建議!

在此先感謝!

回答

1

geocode()是異步方法,需要一些時間來完成。在填充markers之前創建了markerCluster。一種選擇是創建markerCluster之前for循環和geocode()方法使用

markerCluster.addMarker(marker, true); 

。例如:

 var markerCluster = new MarkerClusterer(map, markers); 

     for (var i = 0; i < 100; i++) { 

      geocoder.geocode({ "address": addressArray[i]}, function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location}); 
       markers.push(marker); 
       markerBounds.extend(results[0].geometry.location); 
map.fitBounds(markerBounds); 

       markerCluster.addMarker(marker, true); 
      } 
}); 
} 

     //var markerCluster = new MarkerClusterer(map, markers); 
     } 
相關問題