Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何删除谷歌地图api3中的单个标记_Javascript_Google Maps_Google Maps Api 3_Coordinates_Google Maps Markers - Fatal编程技术网

Javascript 如何删除谷歌地图api3中的单个标记

Javascript 如何删除谷歌地图api3中的单个标记,javascript,google-maps,google-maps-api-3,coordinates,google-maps-markers,Javascript,Google Maps,Google Maps Api 3,Coordinates,Google Maps Markers,我知道还有其他的解决方案,但没有一个能那么容易地适用于我的情况,因为我的标记位于函数中,并且标记是从外部XML文件创建的,如果更改坐标,则在当前位置添加标记。这是我的标记代码 var lastCoordinates={}; var polyline = new google.maps.Polyline({map:map}) var path = []; function gotdata(){ if (xmlhttp.readyState == 4){ var d =

我知道还有其他的解决方案,但没有一个能那么容易地适用于我的情况,因为我的标记位于函数中,并且标记是从外部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);
         }

    }
}



你的问题是什么?如何移除已知坐标的标记?如果不是从它的坐标(这在代码中似乎很明显),如何识别要删除的那个?或者,是否也要从多段线中删除标记的“点”?我要删除的标记最好使用鼠标右键单击来选择,但如果可能,我希望保留多段线。您尝试了什么?如果您有坐标,那么删除标记应该很简单。您的代码中没有“右键单击”处理程序。如果右键单击不可用,如何在保留多段线的同时使用坐标删除标记?您的问题是什么?如何移除已知坐标的标记?如果不是从它的坐标(这在代码中似乎很明显),如何识别要删除的那个?或者,是否也要从多段线中删除标记的“点”?我要删除的标记最好使用鼠标右键单击来选择,但如果可能,我希望保留多段线。您尝试了什么?如果您有坐标,那么删除标记应该很简单。您的代码中没有“右键单击”处理程序。如果右键单击不可用,如何在保留多段线的同时使用坐标删除标记?您的问题是什么?如何移除已知坐标的标记?如果不是从它的坐标(这在代码中似乎很明显),如何识别要删除的那个?或者,是否也要从多段线中删除标记的“点”?我要删除的标记最好使用鼠标右键单击来选择,但如果可能,我希望保留多段线。您尝试了什么?如果您有坐标,那么删除标记应该很简单。您的代码中没有“右键单击”处理程序。如果右键单击不可用,我将如何使用坐标删除标记,同时将多段线保留在那里如果我放置的位置显示“function gotdata”,则不会显示标记,并且控制台显示“get data not Defined”小提琴是否适用于您?请提供一个例子,说明问题本身的问题。如果我把它放在何处,表示“function gotdata”,则不显示任何标记,并且控制台表示“get data未定义”,那么小提琴对您有用吗?请提供一个例子,说明问题本身的问题。如果我把它放在何处,表示“function gotdata”,则不显示任何标记,并且控制台表示“get data未定义”,那么小提琴对您有用吗?请在你的问题中提供一个说明问题本身的例子。
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;
        } 
    });
}