3

我目前有一張地圖在用戶地理位置上顯示一個標記。我有一個文本輸入字段設置爲自動完成位置。當用戶搜索城市名稱時,會在該位置放置一個新標記。但是,舊的凝膠定位標記仍然存在。我想刪除舊的標記或移動它,因此地圖上只有一個標記。我怎樣才能做到這一點?在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

回答

11

讓你的標記全球性的,無論是隱藏它或將它移到新位置

工作代碼片段:

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