gpt4 book ai didi

javascript - getClusters() 总是返回一个空数组(使用 Google Maps V3 的 MarkerClustererPlus)

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:04:13 27 4
gpt4 key购买 nike

我有以下加载 map 并对标记进行聚类的代码:

$scope.getMapHistory = function(locations){

var map = new google.maps.Map(document.getElementById('map-canvas-history'), {
zoom: 17,
center: google.maps.LatLng(0, 0)
});

var markers = [];
locations.forEach(function(deviceLocation){
//var dataPhoto = data.photos[i];
var devLat = deviceLocation._source.data.location[1];
var devLng = deviceLocation._source.data.location[0];
var latLng = new google.maps.LatLng(devLat, devLng);
var marker = new google.maps.Marker({position: latLng });
markers.push(marker);
});

var markerCluster = new MarkerClusterer(map, markers);

//Test to get the clusters
console.log(markerCluster);
console.log(markerCluster.getClusters());
console.log(markerCluster.getTotalClusters());

markerCluster.fitMapToMarkers();

$scope.hideLoading('loadingMap'); //Hides the loading gif

}

直到一切正常。问题是我想访问簇数组,当我尝试记录 markerCluster.getClusters() 的返回时,它返回一个空数组。此外,.getTotalClusters() 返回 0。

enter image description here

然而,当我记录整个 markerCluster 并在控制台上打开它时,我可以看到 clusters_ 数组:

enter image description here

标记聚类工作正常,但我无法访问聚类数组或聚类数量。有人知道会发生什么吗?(如果我的解释不够,我可以提供更多细节)

最佳答案

您似乎在 MarkerClusterer 实例完成对标记的分区之前询问集群和总集群计数,这非常快但不是即时的。

一个简单的测试是在查询属性之前留出几秒钟:

var markerCluster = new MarkerClusterer(map, markers);

window.SetTimeout(function() {
//Test to get the clusters
console.log(markerCluster);
console.log(markerCluster.getClusters());
console.log(markerCluster.getTotalClusters());
},2000);

顺便说一句,您将实例视为包含标记,因为控制台中的对象是对原始对象的引用,这意味着它在您检查时确实包含标记,但在您记录它时不一定。

关于javascript - getClusters() 总是返回一个空数组(使用 Google Maps V3 的 MarkerClustererPlus),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28245542/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com