发送geocode
请求并收到状态为确定的回复后,您可以使用results[0].geometry.location
检索纬度和经度
但是你如何分别得到纬度/长度 ?我无法在Google maps API reference
中找到正确的电话答案 0 :(得分:2)
location
字段是LatLng类型的对象。要获得纬度,请调用它的lat()
方法。要获得经度,请将其命名为lng()
方法。
例如:
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
答案 1 :(得分:1)
results[0].geometry.location
是一个google.maps.LatLng
对象,它有一个返回纬度的.lat()
方法和一个返回经度的.lng()
方法。
答案 2 :(得分:0)
<input id="pac-input" type="text" placeholder="Enter a location">
<div id="map"></div>
<script>
function initMap(){
var input = document.getElementById('pac-input');
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);
autocomplete.addListener('place_changed', function() {
var place = autocomplete.getPlace();
var lat = place.geometry.location.lat();
var lng = place.geometry.location.lng();
});
});
</script>