Javascript 如何更新谷歌地图中的一组标记?
我正在尝试从客户端的表单输出更新我在映射中显示的标记集 我遇到的问题是ShowUser函数,最后一条语句是setMarkersmap,stations,我希望标记会被更改,但它似乎不起作用。有什么帮助吗Javascript 如何更新谷歌地图中的一组标记?,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我正在尝试从客户端的表单输出更新我在映射中显示的标记集 我遇到的问题是ShowUser函数,最后一条语句是setMarkersmap,stations,我希望标记会被更改,但它似乎不起作用。有什么帮助吗 function initialize() { var mapOptions = { zoom: 5, center: new google.maps.LatLng(34.7291, 0.3379) } var map = new google.maps.Map(
function initialize() {
var mapOptions = {
zoom: 5,
center: new google.maps.LatLng(34.7291, 0.3379)
}
var map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
setMarkers(map, stations);
}
var stations = [
['Rafa',40.0,-6.0,0],
['Josh',38.5,-1.0,1]
];
function setMarkers(map, locations) {
var image = ['circle_orange.png','circle_blue .png'];
for (var i = 0; i < locations.length; i++) {
var stations = locations[i];
var myLatLng = new google.maps.LatLng(stations[1], stations[2]);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
icon: image[stations[3]],
title: stations[0],
zIndex: stations[3],
optimized: false
});
var infowindow = new google.maps.InfoWindow({
content: "No data available"
});
google.maps.event.addListener(marker, 'mouseover', function() {
infowindow.setContent("We can include any station information, for example: Lat/Long: "+ stations[1]+" , "+stations[2]);
infowindow.open(map, this);
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
function showUser(str) {
if (str != ""){
var stations = [
['Rafa',50.0,-6.0,0],
['Josh',58.5,-1.0,1]
];
document.getElementById("txtHint").innerHTML=stations;
setMarkers(map, stations);
}
}
正如Molle博士指出的,问题在于map变量。它在initialize函数中初始化,这意味着它不是一个全局变量。Chrome调试器工具是我用来查找问题的方法。以下是一个参考工作示例的链接:
也许你需要手动调用这样的重画:google.maps.event.triggermap,“resize”;删除此行图标:图像[站[3]],当标记出现时,图标的路径不正确。但是,您的代码不会更改标记,它会创建新的标记。谢谢Molle博士,我看不出这样的行有任何问题。实际上,在执行ShowUser之前,标记已完全加载。map是一个局部变量,请删除var关键字