Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.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
Google maps 未能清除Google Map API3中的标记_Google Maps_Google Maps Api 3_Google Maps Markers - Fatal编程技术网

Google maps 未能清除Google Map API3中的标记

Google maps 未能清除Google Map API3中的标记,google-maps,google-maps-api-3,google-maps-markers,Google Maps,Google Maps Api 3,Google Maps Markers,我创建了一个函数,可以在地图上没有标记时添加标记,或者删除原始标记并添加一个新标记来替换它。但是,我可以成功地添加脚本的第一部分,以便在地图上添加标记。但是,当我再次单击地图时,无法删除原始标记,也无法添加新标记。有人能提出什么问题吗 /*Create a listener to record the Click event of the google map and define it as the starting point (i.e. Origin in Google Direction

我创建了一个函数,可以在地图上没有标记时添加标记,或者删除原始标记并添加一个新标记来替换它。但是,我可以成功地添加脚本的第一部分,以便在地图上添加标记。但是,当我再次单击地图时,无法删除原始标记,也无法添加新标记。有人能提出什么问题吗

/*Create a listener to record the Click event of the google map and define it as the starting point (i.e. Origin in Google Direction
service*/
google.maps.event.addListener(map, 'click', function(event) {
  if (origin == null) {
    origin = event.latLng;

    lat = origin.lat();
    lng = origin.lng();
    UpdateLatLng();  

    var startimage = 'images/Start4.png';
    StartMarker = new google.maps.Marker({
         map: map,
         position: origin,
         icon: startimage
     });
  } else {
 //Relocate the Starting point and assign the new position to Textbox
    alert ("The starting point was relocated on screen"); 
    StartMarker.setMap(null);
    directionsDisplay.setMap(null);
    directionsDisplay.setPanel(null);
    directionsDisplay.setDirections({routes: []});
    var origin = event.latLng;  
    lat = origin.lat();
    lng = origin.lng();
    UpdateLatLng(); 
    var startimage = 'images/Start4.png';
    StartMarker = new google.maps.Marker({
         map: map,
         position: origin,
         icon: startimage
     });
    } 
}); 

尝试使用现有的
标记
,如:

// Will change the poisition
StartMarker.setPosition(origin); 
// Will change the Icon
StartMarker.setIcon(startimage);
这对我很有用:

var StartMarker = null;
/*Create a listener to record the Click event of the google map and define it as the starting point (i.e. Origin in Google Direction
service*/
google.maps.event.addListener(map, 'click', function(event) {
  if (!StartMarker) {
    origin = event.latLng;
    lat = origin.lat();
    lng = origin.lng();

    StartMarker = new google.maps.Marker({
         map: map,
         position: origin
     });
  } else {
 //Relocate the Starting point and assign the new position to Textbox
    alert ("The starting point was relocated on screen"); 
    StartMarker.setMap(null);
    var origin = event.latLng;  
    lat = origin.lat();
    lng = origin.lng();
    StartMarker = new google.maps.Marker({
         map: map,
         position: origin
     });
    } 
}); 

为什么不尝试替换原始标记,如?:StartMarker.setPosition(newLatlongPosition);StartMarker.setIcon(新图标);我已经删除了'StartMarker.setMap(null)'和StartMarker=new google.maps.Marker({map:map,position:origin,icon:startimage});并添加一个新代码“StartMarker.setPosition(origin.getposition());”。。。但是,只添加了一个新标记,但原始标记和路线仍然存在感谢您的建议。。。然而,这个问题仍然没有解决。。。无法按预期删除旧标记。。。else{alert(“屏幕上重新定位了起点”);//重新定位起点并将新位置分配给Textbox//StartMarker.setMap(null);directionsDisplay.setMap(null);directionsDisplay.setPanel(null);directionsDisplay.setDirections({routes:[]);var origin=event.latLng;lat=origin.lat();lng=origin.lng();updateLatang();StartMarker.setPosition(origin);您能否在:jsfiddle.netThanks创建一个示例……您的示例解决了我的大部分语法错误……如果我删除以下代码directionsDisplay.setMap(null);directionsDisplay.setPanel(null);directionsDisplay.setDirections({routes:[]),它运行良好;由于在单击事件期间路由可能为空,反之亦然…如果路由存在,我如何删除它???这是另一个问题。您的问题中没有包含足够的代码来回答这个问题(这就是我删除这些行的原因)。您可能存在范围问题。