Javascript 谷歌地图api MarkerClusterer问题
我尝试使用map api MarkerClusterer功能时运气不佳:Javascript 谷歌地图api MarkerClusterer问题,javascript,google-maps,google-maps-api-3,markerclusterer,Javascript,Google Maps,Google Maps Api 3,Markerclusterer,我尝试使用map api MarkerClusterer功能时运气不佳: var markersArray = []; function getMarkers(hours) {//5 if (markersArray) { for (i in markersArray) { markersArray[i].setMap(null); } markersArray.length = 0; } im
var markersArray = [];
function getMarkers(hours) {//5
if (markersArray) {
for (i in markersArray) {
markersArray[i].setMap(null);
}
markersArray.length = 0;
}
image = '/images/site/tw.png';
$.ajax({
url: "updateMarkers",
type:"POST",
data:{"hours": hours},
success: function(data){
data = $.parseJSON( data );
if (data.Locations.length>0) {//2
for (i=0; i<data.Locations.length; i++) {//1
loc = new google.maps.LatLng(data.Locations[i].lat, data.Locations[i].lng);
marker = new google.maps.Marker({
position: loc,
map: map,
icon: image,
html: content
});
markersArray.push(marker);
infowindow = new google.maps.InfoWindow({
content: "holding..."
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, this);
infowindow.setContent(this.html);
});
}//1
}//2
}
});
var markerCluster = new MarkerClusterer(map, markersArray);
}//5
getMarkers(24);
var-markersArray=[];
函数getMarkers(小时){//5
if(markersArray){
for(markersArray中的i){
markersArray[i].setMap(空);
}
markersArray.length=0;
}
image='/images/site/tw.png';
$.ajax({
url:“更新标记”,
类型:“POST”,
数据:{“小时”:小时数},
成功:功能(数据){
data=$.parseJSON(数据);
如果(data.Locations.length>0){//2
对于(i=0;iAjax是异步的。所发生的事情是在回调函数完成之前创建了MarkerClusterer,因此没有将任何标记推到全局数组MarkerArray上。这只是我的想法,没有进行任何测试,但看看它是否解决了问题
var markersArray = [], markerCluster;
function getMarkers(hours) {//5
if (markersArray) {
for (i in markersArray) {
markersArray[i].setMap(null);
}
markersArray.length = 0;
}
image = '/images/site/tw.png';
$.ajax({
url: "updateMarkers",
type:"POST",
data:{"hours": hours},
success: function(data){
data = $.parseJSON( data );
if (data.Locations.length>0) {//2
for (i=0; i<data.Locations.length; i++) {//1
loc = new google.maps.LatLng(data.Locations[i].lat, data.Locations[i].lng);
marker = new google.maps.Marker({
position: loc,
map: map,
icon: image,
html: content
});
markersArray.push(marker);
infowindow = new google.maps.InfoWindow({
content: "holding..."
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, this);
infowindow.setContent(this.html);
});
}//1
//Create the Cluster AFTER all markers are pushed into the markersArray, and make sure it's called within the callback function
markerCluster = new MarkerClusterer(map, markersArray);
}//2
}
});
}//5
getMarkers(24);
var markersArray=[],markerCluster;
函数getMarkers(小时){//5
if(markersArray){
for(markersArray中的i){
markersArray[i].setMap(空);
}
markersArray.length=0;
}
image='/images/site/tw.png';
$.ajax({
url:“更新标记”,
类型:“POST”,
数据:{“小时”:小时数},
成功:功能(数据){
data=$.parseJSON(数据);
如果(data.Locations.length>0){//2
对于(i=0;isorry用于代码格式设置-不知道那里发生了什么:)