我在谷歌地图上的网页应用程序中我的标记有问题。我可以添加标记,但我不能删除它们。我已经寻找了几天的解决方案,但是对于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);
}
}
发布于 2014-06-16 22:57:16
我认为问题的根本原因在于您实际上没有使用.setMap(null)调用来寻址您认为是的对象。
尝试从placeMarker()返回标记,然后将其赋值给var,并对其调用setMap(null)。
如果在初始化window.myPositionMarker ()之后声明google.maps.marker=标记,那么它将按预期工作。
发布于 2016-06-08 18:11:31
看来你的答案已经解决了,但对于那些为这个问题而挣扎的人来说,请预先警告-
在设置自定义属性后,我的标记将不会删除。
例如
// 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
发布于 2014-06-16 23:17:46
当创建一个新标记时,它的全局名称myPositionMarker无法引用它。然而,在创建标记的过程中,它被放置在一个数组中。如果我把标记称为
window.markers[0].setMap(null);
它将按预期从地图中移除。非常感谢你的帮助!
https://stackoverflow.com/questions/24253092
复制相似问题