在Google地图上移动Google地图标记自动完成

问题描述:

我目前有一张地图在用户地理位置上显示一个标记。我有一个文本输入字段设置为自动完成位置。当用户搜索城市名称时,会在该位置放置一个新标记。但是,旧的凝胶定位标记仍然存在。我想删除旧的标记或移动它,因此地图上只有一个标记。我怎样才能做到这一点?在Google地图上移动Google地图标记自动完成

这里是我的代码:

<html> 
<head> 
    <script> 
    var map; 

    function initialize() { 
     var mapOptions = { 
      zoom: 12 
    }; 
    map = new google.maps.Map(document.getElementById('map-canvas'), 
    mapOptions); 

    // Get GEOLOCATION 
    if(navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(function(position) { 
     var pos = new google.maps.LatLng(position.coords.latitude, 
             position.coords.longitude); 

     map.setCenter(pos); 
     var marker = new google.maps.Marker({ 
      position: pos, 
      map: map, 
      draggable:true 
     }); 
     }, function() { 
     handleNoGeolocation(true); 
     }); 
    } else { 
     // Browser doesn't support Geolocation 
     handleNoGeolocation(false); 
    } 
    function handleNoGeolocation(errorFlag) { 
     if (errorFlag) { 
     var content = 'Error: The Geolocation service failed.'; 
     } else { 
     var content = 'Error: Your browser doesn\'t support geolocation.'; 
     } 

     var options = { 
     map: map, 
     position: new google.maps.LatLng(60, 105), 
     content: content 
     }; 

     map.setCenter(options.position); 

    } 

// get places auto-complete when user type in location-text-box 
    var input = /** @type {HTMLInputElement} */(
    document.getElementById('location-text-box')); 


    var autocomplete = new google.maps.places.Autocomplete(input); 
    autocomplete.bindTo('bounds', map); 

    var infowindow = new google.maps.InfoWindow(); 
    var marker = new google.maps.Marker({ 
     map: map, 
     anchorPoint: new google.maps.Point(0, -29), 
     draggable:true 
    }); 

    google.maps.event.addListener(autocomplete, 'place_changed', function() { 
     infowindow.close(); 
     marker.setVisible(false); 
     var place = autocomplete.getPlace(); 
     if (!place.geometry) { 
     return; 
     } 

// If the place has a geometry, then present it on a map. 
    if (place.geometry.viewport) { 
     map.fitBounds(place.geometry.viewport); 
    } else { 
     map.setCenter(place.geometry.location); 
     map.setZoom(17); // Why 17? Because it looks good. 
    } 
    marker.setIcon(/** @type {google.maps.Icon} */({ 
     url: place.icon, 
     size: new google.maps.Size(71, 71), 
     origin: new google.maps.Point(0, 0), 
     anchor: new google.maps.Point(17, 34), 
     scaledSize: new google.maps.Size(35, 35) 
    })); 
    marker.setPosition(place.geometry.location); 
    marker.setVisible(true); 

    var address = ''; 
    if (place.address_components) { 
     address = [ 
     (place.address_components[0] && place.address_components[0].short_name || ''), 
     (place.address_components[1] && place.address_components[1].short_name || ''), 
     (place.address_components[2] && place.address_components[2].short_name || '') 
     ].join(' '); 
    } 

    }); 



} 

google.maps.event.addDomListener(window, 'load', initialize); 
     </script> 
    </head> 
    <body> 
     <div> 
       <input type="text" id="location-text-box"> 
       <div id="map-canvas"></div> 
     </div> 
    </body> 
    </html> 
+0

类似的问题:[使用Google Maps API进行地理编码 - updat现有的标记,而不是添加另一个](http://stackoverflow.com/questions/12628994/geocoding-with-google-maps-api-updating-existing-marker-rather-than-adding-ano) – geocodezip 2015-02-08 19:32:57

让你的标记全球性的,无论是隐藏它或将它移到新位置

工作代码片段:

var map; 
 
var marker; 
 

 
function initialize() { 
 
    var mapOptions = { 
 
    zoom: 12 
 
    }; 
 
    map = new google.maps.Map(document.getElementById('map-canvas'), 
 
    mapOptions); 
 

 
    // Get GEOLOCATION 
 
    if (navigator.geolocation) { 
 
    navigator.geolocation.getCurrentPosition(function(position) { 
 
     var pos = new google.maps.LatLng(position.coords.latitude, 
 
     position.coords.longitude); 
 

 
     map.setCenter(pos); 
 
     marker = new google.maps.Marker({ 
 
     position: pos, 
 
     map: map, 
 
     draggable: true 
 
     }); 
 
    }, function() { 
 
     handleNoGeolocation(true); 
 
    }); 
 
    } else { 
 
    // Browser doesn't support Geolocation 
 
    handleNoGeolocation(false); 
 
    } 
 

 
    function handleNoGeolocation(errorFlag) { 
 
    if (errorFlag) { 
 
     var content = 'Error: The Geolocation service failed.'; 
 
    } else { 
 
     var content = 'Error: Your browser doesn\'t support geolocation.'; 
 
    } 
 

 
    var options = { 
 
     map: map, 
 
     position: new google.maps.LatLng(60, 105), 
 
     content: content 
 
    }; 
 

 
    map.setCenter(options.position); 
 
    marker = new google.maps.Marker({ 
 
     position: options.position, 
 
     map: map, 
 
     draggable: true 
 
    }); 
 

 
    } 
 

 
    // get places auto-complete when user type in location-text-box 
 
    var input = /** @type {HTMLInputElement} */ 
 
    (
 
     document.getElementById('location-text-box')); 
 

 

 
    var autocomplete = new google.maps.places.Autocomplete(input); 
 
    autocomplete.bindTo('bounds', map); 
 

 
    var infowindow = new google.maps.InfoWindow(); 
 
    marker = new google.maps.Marker({ 
 
    map: map, 
 
    anchorPoint: new google.maps.Point(0, -29), 
 
    draggable: true 
 
    }); 
 

 
    google.maps.event.addListener(autocomplete, 'place_changed', function() { 
 
    infowindow.close(); 
 
    marker.setVisible(false); 
 
    var place = autocomplete.getPlace(); 
 
    if (!place.geometry) { 
 
     return; 
 
    } 
 

 
    // If the place has a geometry, then present it on a map. 
 
    if (place.geometry.viewport) { 
 
     map.fitBounds(place.geometry.viewport); 
 
    } else { 
 
     map.setCenter(place.geometry.location); 
 
     map.setZoom(17); // Why 17? Because it looks good. 
 
    } 
 
    marker.setIcon(/** @type {google.maps.Icon} */ ({ 
 
     url: place.icon, 
 
     size: new google.maps.Size(71, 71), 
 
     origin: new google.maps.Point(0, 0), 
 
     anchor: new google.maps.Point(17, 34), 
 
     scaledSize: new google.maps.Size(35, 35) 
 
    })); 
 
    marker.setPosition(place.geometry.location); 
 
    marker.setVisible(true); 
 

 
    var address = ''; 
 
    if (place.address_components) { 
 
     address = [ 
 
     (place.address_components[0] && place.address_components[0].short_name || ''), (place.address_components[1] && place.address_components[1].short_name || ''), (place.address_components[2] && place.address_components[2].short_name || '') 
 
     ].join(' '); 
 
    } 
 

 
    }); 
 

 

 

 
} 
 

 
google.maps.event.addDomListener(window, 'load', initialize);
html, 
 
body, 
 
#map-canvas { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places"></script> 
 
<div style="height:100%; width:100%"> 
 
    <input type="text" id="location-text-box" /> 
 
    <div id="map-canvas"></div> 
 
</div>

+0

我该怎么办这个?对不起,我对JavaScript很陌生。在类似问题中使用的示例与我的代码无关,我不知道如何合并它。谢谢 – xslibx 2015-02-08 20:37:37

+0

这就是我正在寻找的...有点补充.......我怎样才能访问和保存坐标,无论何时用户移动标记? – 2015-08-04 05:42:57

+0

这是另一个问题。你找过重复的东西吗? – geocodezip 2015-08-04 09:25:30