在Ajax之后将Marker Clusterer设置为异步 [英] Set Marker Clusterer async after Ajax
问题描述
我的网页上有一个Google Maps v3,它将设置很多标记(3000-5000).
I have a Google Maps v3 on my webpage which will be set with a lot of markers (3000 - 5000).
这是我现在缩短的代码:
This is my shortened code for this now:
function initialize(){
var map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
$.ajax({
type: 'POST',
dataType: 'json',
url: Routing.generate('turn_getajaxfirstpoiofallturns'),
async: false
})
.done(function(response){
firstPoiOfAllTurns = response;
for (var i = 0; i < firstPoiOfAllTurns.length; ++i) {
markers.push(addMarker(map, firstPoiOfAllTurns[i].pois[0], firstPoiOfAllTurns[i].id, '' ,infowindow));
}
})
.fail(function(jqXHR, textStatus, errorThrown){
alert('Error : ' + errorThrown);
});
var mcOptions = {gridSize: 50, maxZoom: 15};
var mc = new MarkerClusterer(map, markers, mcOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
我想摆脱此代码的 async:false
部分,以便即使尚未设置标记,该站点也能正常工作.到目前为止,该方法仍然有效,但是如果我将 async:false
移走,则不会设置MarkerClusterer.这样就可以显示标记,但是没有聚类时,地图的速度确实很慢.
I want to get rid of the async: false
part of this code, so that the site will work even if the markers are not set yet. This works so far, but the MarkerClusterer won't be set if i take the async: false
away. So the Markers will be shown, but the map is really slow without the Cluster.
然后我尝试在 .done
函数中也采用了两行代码,但是也没有用.
I then tried to take the 2 lines of code also in the .done
function, but it didn't worked either.
我如何将markerclusterer设置为与Map异步,以便在ajax函数完成后将Markers显示为集群?
How can i set the markerclusterer async to the Map so that the Markers will be shown clustered after the ajax function finished?
推荐答案
您可以在ajax调用之前创建标记聚类器,并将每个创建的标记添加到for循环中的标记聚类器中:
You can create marker clusterer before ajax call and add each created marker to marker clusterer in for loop:
for (var i = 0; i < firstPoiOfAllTurns.length; ++i) {
// markers.push(addMarker(map, firstPoiOfAllTurns[i].pois[0], firstPoiOfAllTurns[i].id, '' ,infowindow));
mc.addMarker(addMarker(map, firstPoiOfAllTurns[i].pois[0], firstPoiOfAllTurns[i].id, '' ,infowindow), true);
}
我假设 addMarker()
创建一个新标记.
I assume that addMarker()
create a new marker.
这篇关于在Ajax之后将Marker Clusterer设置为异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!