【发布时间】:2016-11-11 19:01:07
【问题描述】:
发送geocode 请求并收到状态为OK 的响应后,您可以使用results[0].geometry.location 检索纬度 和经度.
但是您如何分别获得纬度/经度?我在Google maps API reference 中找不到合适的电话
【问题讨论】:
发送geocode 请求并收到状态为OK 的响应后,您可以使用results[0].geometry.location 检索纬度 和经度.
但是您如何分别获得纬度/经度?我在Google maps API reference 中找不到合适的电话
【问题讨论】:
location 字段是LatLng 类型的对象。要获得纬度,请将其称为lat() 方法。要获取经度,请将其称为 lng() 方法。
例如:
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
【讨论】:
results[0].geometry.location 是一个google.maps.LatLng 对象,它有一个返回纬度的.lat() 方法和一个返回经度的.lng() 方法。
【讨论】:
<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>
【讨论】: