Google Map API3中的清晰标记失败

时间:2013-05-12 08:06:21

标签: 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
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
     });
    } 
}); 

2 个答案:

答案 0 :(得分:0)

尝试使用现有的Marker,如:

// Will change the poisition
StartMarker.setPosition(origin); 
// Will change the Icon
StartMarker.setIcon(startimage);

答案 1 :(得分:0)

这对我有用:

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
     });
    } 
}); 

working example