简体   繁体   English

Google Maps API:分别获取经度/纬度

[英]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.locationgoogle.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM