我在使用Google地图web应用时遇到了标记问题。我可以添加标记,但无法删除它们。我已经寻找了几天的解决方案,但是v3的标准建议似乎是:
marker.setMap(null);
问题在于这似乎对我的代码完全无效。下面是一个启动时运行的函数示例。它可以快速以低精度获取当前位置。稍后需要更长时间才能完成的另一个函数应该删除标记并在更准确的位置放置新的标记。问题是,我无法在放置后移除标记。
function geoLocCheckAndFastLatLng(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
//get current position
pos = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
//place low accuracy marker
placeMarker(pos,window.meIconMediumAccuracy,window.myPositionMarker);
//and center map
window.map.setCenter(pos);
window.map.setZoom(14);
window.myPositionMarker.setMap(null);
});
}else{
alert("Sorry - You're Browser Doesn't Support Geolocation.\nChange To Google-Chrome Or Dolphin To Use This App");
}
}
在理论上,上述函数应该获取位置、放置标记,然后删除同一标记。但是该标记仍然存在!有人能帮忙吗?我只剩下几天时间来完成,但我不知道出了什么问题。
以下是放置标记的函数:
function placeMarker(location, iconType, marker) {
//alert(window.markers.length);
//if the marker is undefined it means it needs to be created form scratch
//using the iconType and location provided in the function call
if(marker === undefined){
marker = new google.maps.Marker(
{
position: location,
map: window.map,
icon: iconType,
});
//and add a click listener
google.maps.event.addListener(marker, 'click', function()
{
alert("Marker location:\n" + pos);
});
//add to markers array
window.markers.push(marker);
}else{
marker.setPosition(location);
}
}
window.myPositionMarker.setMap(null);
是指什么?我觉得应该是window.markers[i].setMap(null);
,其中 "i" 是你想要隐藏的特定标记的索引(对于你的原始示例来说,它将是window.markers.length-1
,即最后添加的标记)。 - geocodezip