[英]Google maps API: get lat / long separately
After sending a geocode
request and receiving a response with status OK , you can use results[0].geometry.location
to retrieve both latitude and longitude . 在发送
geocode
请求并收到状态为OK的响应后,您可以使用results[0].geometry.location
检索纬度和经度 。
But how do you get the lat / long separately ? 但你如何让经/纬分开 ? I can't find the right call in the Google maps API reference
我在Google Maps API参考中找不到正确的调用
That location
field is an object of type LatLng . 该
location
字段是LatLng类型的对象。 To get the latitude, call it's lat()
method. 要获取纬度,请调用
lat()
方法。 To get the longitude, call it's lng()
method. 要获取经度,请调用它的
lng()
方法。
For example: 例如:
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.