why setMap(null) is not working google maps api v3?

大城市里の小女人 提交于 2019-12-03 07:29:25

From the documentation-

To remove an overlay from a map, call the overlay's setMap() method, passing null. Note that calling this method does not delete the overlay; it simply removes the overlay from the map. If instead you wish to delete the overlay, you should remove it from the map, and then set the overlay itself to null.

so after the marker.setMap(null) you should also write marker=null

Update1-

function deleteMarker(id,rev) {
  var x = confirm("are you sure to delete marker?");
  if(x)
  {
    deleteLocations(id,rev);//removes marker details from DB
    if(markerObj)
    {
       console.log(markerObj);
       markerObj.setMap(null);
       markerObj=null;
    }
  }
}

Update 2-

Here is a simple demo that works. See the code and check where your code is wrong. Probably some variable scope issue exists in your code.

WORKING DEMO

I had same problem. You should call these methods before markers[index].setMapp(null) :

map.setCenter(desMarker[index].getPosition());
desMarker[index].setPosition(null);

after these call:

markers[index].setMapp(null)

marker.setMap(null) does not delete the object, it only hides it. To delete it do marker = null;

In the map click event you assign this to the markerObj. Though this refers to the map object and not the marker object.

Change it to

markerObj = point;

and it should work as expected.

I had a similar error, I'm not sure if my solution applies to your case, nevertheless.. I set up my code so that when my page loaded the map would be filled up with any markers from coordinates denoted in my database. Then I allowed the user to add more points to the database and then added a marker to the user's selected location on the map.

What I didn't realize is that any time a coordinate was changed or created in my database my code was re-adding markers to all the coordinates on my map. So anytime I created a point I was both manually adding a marker to the coordinates and my database was adding a marker to the coordinates. So when I thought my code was broken what was really happening is I was deleting one of two points in the same location.

So I don't know exactly how you're pulling in coordinates and markers from you database but it's worth looking into.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!