【问题标题】:How to remove individual markers in Google maps api3如何删除谷歌地图 api3 中的单个标记
【发布时间】:2015-06-08 09:10:10
【问题描述】:

我知道还有其他解决方案,但它们都不适用于我的情况,因为我的标记位于函数中,并且标记是从外部 XML 文件创建的,如果更改坐标,则在其中添加标记当前位置。这是我的标记代码

var lastCoordinates={};
var polyline = new google.maps.Polyline({map:map})
var path = [];
function gotdata(){

    if (xmlhttp.readyState == 4){

        var d = xmlhttp.responseXML.documentElement 
            //innerHTML shouldn't work for XML-Nodes
            y = d.getElementsByTagName("y")[0].textContent,
            x = d.getElementsByTagName("x")[0].textContent,
            h = [x,y].join('_');
        if(lastCoordinates[h]){
          return;
        } 

        lastCoordinates[h]= new google.maps.Marker({
                              position: new google.maps.LatLng(x,y),
                              map: map,
                              title: 'YAY'
                            });
         path.push(lastCoordinates[h].getPosition());
         if (path.length >= 2) {
           // display the polyline once it has more than one point
           polyline.setMap(map);
           polyline.setPath(path);
         }

    }
}

【问题讨论】:

  • 您的问题是什么?如何删除知道其坐标的标记?如果不是从它的坐标(从您的代码中看起来很明显),您如何识别要删除的坐标?或者您是否也想从折线上删除标记的“点”?
  • 我想删除的标记最好通过右键单击来选择,但如果可能的话,我希望保留折线
  • 你尝试了什么?如果您有坐标,则删除标记应该很简单。您的代码中没有“右键单击”处理程序。
  • 如果右键单击不可用,我将如何使用坐标删除标记,同时将折线留在那里

标签: javascript google-maps google-maps-api-3 coordinates google-maps-markers


【解决方案1】:
function gotdata() {
  if (xmlhttp.readyState == 4){
    count++;
    // var d = xmlhttp.responseXML.documentElement 
    //innerHTML shouldn't work for XML-Nodes
    y = count * 0.01; // d.getElementsByTagName("y")[0].textContent,
    x = count * 0.01; //d.getElementsByTagName("x")[0].textContent,
    h = [x, y].join('_');
    if (lastCoordinates[h]) {
        return;
    }

    lastCoordinates[h] = new google.maps.Marker({
        position: new google.maps.LatLng(x, y),
        map: map,
        title: 'YAY'
    });
    rightclickableMarker(lastCoordinates[h],h);    
    map.panTo(lastCoordinates[h].getPosition());
    path.push(lastCoordinates[h].getPosition());
    if (path.length >= 2) {
        // display the polyline once it has more than one point
        polyline.setMap(map);
        polyline.setPath(path);
    }
  }
}
function rightclickableMarker(marker, h) {
    google.maps.event.addListener(marker, 'rightclick', function(evt) {
        if(lastCoordinates[h] && lastCoordinates[h].setMap){
          lastCoordinates[h].setMap(null);
          delete marker;
        } 
    });
}

proof of concept fiddle

【讨论】:

  • 如果我把那个地方说'function gotdata'没有标记出现并且控制台说get data is not defined
  • 小提琴适合你吗?请提供一个Minimal, Complete, Tested and Readable example 来说明问题在您的问题本身
猜你喜欢
  • 2013-12-04
  • 1970-01-01
  • 2012-10-17
  • 1970-01-01
  • 2020-04-02
  • 2013-12-09
  • 2011-01-28
  • 1970-01-01
  • 2013-05-19
相关资源
最近更新 更多