gpt4 book ai didi

javascript - 从数组中在谷歌地图上放置多个标记

转载 作者:搜寻专家 更新时间:2023-11-01 04:10:43 25 4
gpt4 key购买 nike

我正在尝试在 map 上放置多个标记,这些标记由数组提供。现在只有我的初始点加载(纽约市)。

var geocoder;
var map;
var markersArray = [];

//plot initial point using geocode instead of coordinates (works just fine)
function initialize() {
geocoder = new google.maps.Geocoder();
latlang = geocoder.geocode( { 'address': 'New York City'}, function(results, status) { //use latlang to enter city instead of coordinates
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
markersArray.push(marker);
}
else{
alert("Geocode was not successful for the following reason: " + status);
}
});
var myOptions = {
center: latlang, zoom: 5, mapTypeId: google.maps.MapTypeId.SATELLITE,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
}
};
map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);

}

///////////////////////////////////////////////////////////
//Everything below this line is for attempting to plot the markers

var locationsArray = ['Pittsburgh','Chicago', 'Atlanta'];

function plotMarkers(){
for(var i = 0; i < locationsArray.length; i++){
codeAddresses(locationsArray[i]);
}
}

function codeAddresses(address){
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
//markersArray.push(marker);
}
else{
alert("Geocode was not successful for the following reason: " + status);
}
});
}

最佳答案

您实际上并没有在上面代码段的任何地方调用 plotMarkers!当我添加到初始化的末尾时(定义 map 后),效果很好! http://jsfiddle.net/T5aKE/

       ...
map = new google.maps.Map...
plotMarkers();
...

关于javascript - 从数组中在谷歌地图上放置多个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10688509/

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