I want to remove one marker from my Google Map, but I can't seem to get it to work. I find various answers, all telling me to use .setMap(null)
on the marker, but I can't seem to get it to work.
我想从我的谷歌地图中删除一个标记,但我似乎无法让它工作。我找到了各种答案,都告诉我在标记上使用.setMap(null),但我似乎无法让它工作。
$map_canvas = $('#map_canvas');
var youreHere_Marker;
function centerMapToAddress( address ) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if( typeof youreHere_Marker!=="undefined"){
youreHere_Marker.setMap(null);
}
youreHere_Marker = $map_canvas.gmap('addMarker', {'position': results[0].geometry.location.lat()+','+results[0].geometry.location.lng(), 'bounds': true});
}
});
}
I get TypeError: youreHere_Marker.setMap is not a function
. To my knowledge this means that the variable youreHere_Marker
doesn't have the method .setMap()
, but if I do console.log(youreHere_Marker)
and inspect the object, I can see the method.
我得到TypeError:youreHere_Marker.setMap不是一个函数。据我所知,这意味着变量youreHere_Marker没有方法.setMap(),但如果我执行console.log(youreHere_Marker)并检查对象,我可以看到该方法。
I have more markers on my map, via the MarkerClusterer
. Those should remain untouched
我的地图上有更多标记,通过MarkerClusterer。那些应该保持不变
I have the feeling I'm close, could someone point me in the right direction?
我感觉我很亲近,有人能指出我正确的方向吗?
Edit: I've also tried .setPosition()
, same error. I'm assuming I'm using the variable incorrect, but I don't know how to refer to it properly.
编辑:我也尝试过.setPosition(),同样的错误。我假设我使用的变量不正确,但我不知道如何正确引用它。
1 个解决方案
#1
0
Well, i was working with google maps without jQuery, but i think (i'm not sure, but you may try) that you should get your marker with the following code:
好吧,我正在使用没有jQuery的谷歌地图,但我认为(我不确定,但你可以尝试),你应该得到你的标记与以下代码:
youreHere_Marker = $map_canvas.gmap('get', 'markers')[0];
youreHere_Marker.setMap(null);
I'm really not sure that it will do what you want, but there is a possibility that this will work : )
I hope you'll solve you problems.
Thanks. : )
我真的不确定它会做你想要的,但有可能这样做:)我希望你能解决你的问题。谢谢。 :)
#1
0
Well, i was working with google maps without jQuery, but i think (i'm not sure, but you may try) that you should get your marker with the following code:
好吧,我正在使用没有jQuery的谷歌地图,但我认为(我不确定,但你可以尝试),你应该得到你的标记与以下代码:
youreHere_Marker = $map_canvas.gmap('get', 'markers')[0];
youreHere_Marker.setMap(null);
I'm really not sure that it will do what you want, but there is a possibility that this will work : )
I hope you'll solve you problems.
Thanks. : )
我真的不确定它会做你想要的,但有可能这样做:)我希望你能解决你的问题。谢谢。 :)