我在Google地图网络应用中遇到了我的标记问题。我可以添加标记,但我无法删除它们。我一直在寻找解决方案的几天,但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);
}
}
答案 0 :(得分:2)
我认为问题的根本原因是你实际上并没有使用.setMap(null)调用来解决你认为对象的对象。
尝试从placeMarker()返回标记,然后将其分配给var并在其上调用setMap(null)。
如果在初始化google.maps.marker()后声明window.myPositionMarker = marker,它将按预期工作。
答案 1 :(得分:1)
似乎你的答案已经解决了,但是对于那些在这个问题上挣扎的人,要预先警告 -
在我设置自定义属性后,我的标记不会删除。
例如
// can create a marker and add it to map OK
marker = new new google.maps.Marker({...})
marker.setMap(map)
// But don't set a custom property on the marker
marker.foo = "bar"
marker.setMap(null) // wont remove the marker
答案 2 :(得分:0)
创建新标记时,无法通过其全局名称myPositionMarker引用它。但是在创建标记的过程中,它放在一个数组中。如果我将标记称为
window.markers[0].setMap(null);
按预期将其从地图中删除。非常感谢你的帮助!