简体   繁体   中英

Google Maps JavaScript API3 marker.setMap(null) Not Hiding the Marker

The marker.setMap(null) call doesn't remove the marker from the map.

I have confirmed the map and marker vars point to the right place but the setMap(null) call simply leaves the marker visible on the map. The setVisible(false) also has no effect.

if(sch_ovr_google_markers.length > 0){//remove from map
    for(var i=0;i < sch_ovr_google_markers.length;i++){
        sch_ovr_google_markers[i].setMap(null);//no effect
    }
}

The markers are created here:

var latlon = new google.maps.LatLng(o.lat, o.lon);
var marker = new google.maps.Marker(
{
    map:sch_google_map,
    position: latlon, 
    title: gpn[4],
    icon: pinImage,
    shadow: pinShadow,
    animation: google.maps.Animation.DROP,
    zIndex:10000,
    index: c
});
sch_ovr_google_markers[lidx] = marker;
sch_ovr_google_iws[lidx] = new google.maps.InfoWindow({ content:h, position:latlon });
sch_ovr_google_cbs[lidx] = sch_ovr_google_marker_init(marker,lidx);

The whole map works great except for this one problem. The surrounding code tests good and too involved to list completely.

I am wondering if there is some hidden preconditions for it to work. I have tried deleting other references to it before setMap(null), but still no luck.

The documentation makes it sound like the method will simply remove it, but obviously, to met at least, there is something more required. Any ideas?

Hopefully the original poster still isn't needing this late answer, but I had similar symptoms and couldn't find any help in the Internets.

For me, there was a problem in populating the data which caused duplicate markers to appear. Therefore, calling setMap(null) didn't seem to do anything, though it still actually did work ok, but of course it only worked on the other marker, leaving the other still on the map which looked like the setMap(null) isn't working.

So, if you're sure that your marker's map is set to null (you can check from javascript console/debugger) but the marker is still visible, try setting draggable: true parameter for the marker and move it around to see if another marker is lurking behind it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM