gpt4 book ai didi

javascript - MarkerCluster Google map 未对我的标记进行分组

转载 作者:行者123 更新时间:2023-12-03 11:21:47 26 4
gpt4 key购买 nike

首先我使用 JSON//Work 获取车站位置

JSON 结构:

[{"number":31705,"name":"31705 - CHAMPEAUX (BAGNOLET)","address":"RUE DES CHAMPEAUX (PRES DE LA GARE ROUTIERE) - 93170 BAGNOLET","position":{"lat":48.8645278209514,"lng":2.416170724425901},"banking":true,"bonus":true,"status":"OPEN","contrat _name":"Paris","bike_stands":50,"available_bike_stands":45,"available_bikes":5,"last_update":1416677110000}]

第二,我想要使用 MarkerCluster 对标记进行分组,但 MarkerCluster 位于我的所有标记中
为什么不分组?

function initialize() {

var mapOptions = {
center: new google.maps.LatLng(48.859875, 2.342253),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true
};

var infoWindow = new google.maps.InfoWindow();
var mcOptions = {gridSize: 50, maxZoom: 15, minimumClusterSize:10}

map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
$.getJSON("https://api.jcdecaux.com/vls/v1/stations?contract=Paris&apiKey=d98a9b59f1d1999cf1ab909051cc63bd46c66a46", function (data) {
$.each(data, function (key, data) {

var markers = [];

for (var i = 0, length = data.address.length; i < length; i++) {
var myLatlng = new google.maps.LatLng(data.position.lat, data.position.lng);
var marker = new google.maps.Marker({
position: myLatlng
});

markers.push(marker);
}

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

});
});
google.maps.event.addDomListener(window, 'load', initialize);
}

最佳答案

您正在为每个标记创建一个新的 MarkerClusterer(如果您缩进代码,则更容易看到)

  • 将 MarkerCluster 的创建移出 $.each。

还有:

  • google.maps.event.addDomListener(window,'load',initialize); 移出初始化函数
  • 删除 data.address.length 上的内部循环(除非您的数据需要,否则不会查看您的所有数据)
function initialize() {

var mapOptions = {
center: new google.maps.LatLng(48.859875, 2.342253),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true
};

var infoWindow = new google.maps.InfoWindow();
var mcOptions = {
gridSize: 50,
maxZoom: 15,
minimumClusterSize: 10
};

map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
$.getJSON("https://api.jcdecaux.com/vls/v1/stations?contract=Paris&apiKey=d98a9b59f1d1999cf1ab909051cc63bd46c66a46", function (data) {
$.each(data, function (key, data) {
var myLatlng = new google.maps.LatLng(data.position.lat, data.position.lng);
var marker = new google.maps.Marker({
position: myLatlng
});
markers.push(marker);
});
var markerCluster = new MarkerClusterer(map, markers, mcOptions);
});
}
google.maps.event.addDomListener(window, 'load', initialize);

working fiddle (使用修改后的数据子集)

关于javascript - MarkerCluster Google map 未对我的标记进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27078976/

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