Google maps API:分别获取lat / long

时间:2016-11-11 19:01:08

标签: javascript google-maps

发送geocode请求并收到状态为确定的回复后,您可以使用results[0].geometry.location检索纬度经度

但是你如何分别得到纬度/长度 ?我无法在Google maps API reference

中找到正确的电话

3 个答案:

答案 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>