gpt4 book ai didi

javascript - GMap 中的标记数组

转载 作者:行者123 更新时间:2023-11-30 15:29:54 29 4
gpt4 key购买 nike

我需要处理 map 上的每个标记,所以我想要的是这样的:

var marker[5] = new google.maps.Marker({
position: new google.maps.LatLng(31.0962,36.2738),
map: map, //declared before ..
optimized: false,
label:'FRED'
});

所以我可以像这样处理每个标记:(还有许多与服务器端的交互)

marker[5].setPosition(NewPosition);

我知道上面的表格是不可能的,但找不到准确的答案。

最佳答案

脚本修改自 Google Maps JS API v3 - Simple Multiple Marker Example\

工作 fiddle :https://jsfiddle.net/6tyavn93/

var locations = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.923036, 151.259052, 5],
['Cronulla Beach', -34.028249, 151.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.950198, 151.259302, 1]
];

var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(-33.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow();

var marker, i;
var markers = [];

for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});

markers.push(marker);

google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}

console.log(markers[0]);

关于javascript - GMap 中的标记数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42397951/

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