为什么setMap(null)无法使用Google Maps API v3? [英] why setMap(null) is not working google maps api v3?

查看:125
本文介绍了为什么setMap(null)无法使用Google Maps API v3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用google maps api 3.9.在应用程序中,用户可以添加标记或删除标记.当用户单击地图时,将显示一个信息窗口.在该窗口中,用户可以输入名称,纬度,长度并单击保存图像,如下所示:

I am using google maps api 3.9 .In app user can add marker or delete marker.when user click on map an Infowindow will be displayed.in which user can enter name,lat,long and click the save image as follows:

google.maps.event.addListener(map, 'click', function(event) {

  point = new google.maps.Marker({
    position: event.latLng
    , map: map
    , icon: 'resource/image/mapIcons/point.png'
    , id: id
    , type:"point"
  });

  type = point.type;
  newPoint = true;
  existingPoint = false;
  markerObj = this;

  inputInfowindow.setContent("<table style='width:92%;' id='inputTable'>" +
                             "<tr> <td>point</td> </tr>" +
                             "<tr> <td><input class='infoInput' type='text' id='name' placeholder='name'/> </td> </tr>" +
                             "<tr> <td><input class='infoInput'type='text' id='lat' placeholder='latitude'/></td> </tr>" +
                             "<tr> <td><input class='infoInput'type='text' id='lon' placeholder='longitude'/></td> </tr>" +
                             "<tr><td><input type='image' src='resource/image/mapIcons/save.png' onclick='save()' class='saveImage' alt='save'/> </td></tr>");

  event1 = event.latLng;
  currentMarker = point;
  inputInfowindow.open(map,point);

});

标记已保存在数据库中. 当用户点击删除"按钮的以下方法时:

marker saved in DB. when user cliclks on delete button follwing method ll be called:

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

但在marker.setMap(null);我从console.log(marker)进行了检查;标记对象正确到达,控制台上没有错误.我经过大量的谷歌搜索,但没有结果.请对此提供帮助.

but at marker.setMap(null); marker is removed from map still its on map.I checked with console.log(marker); marker object coming properly,no errors on console.i went through lot of googling but no result.Please help about this.

推荐答案

摘自文档-

要从地图上删除叠加层,请调用叠加层的setMap()方法, 传递null.请注意,调用此方法不会删除 覆盖;只需从地图上删除叠加层即可.相反,如果你 希望删除该叠加层,则应将其从地图上删除,然后 然后将叠加层本身设置为null.

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.

因此,在marker.setMap(null)之后,您还应该写marker=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;
    }
  }
}

更新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.

工作演示

这篇关于为什么setMap(null)无法使用Google Maps API v3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆