我需要在地图上使用每个标记,所以我想要的是这样的:
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);
我知道上述形式是不可能的,但无法找到准确的答案。
答案 0 :(得分:2)
脚本修改自Google Maps JS API v3 - Simple Multiple Marker Example \
工作小提琴: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]);