如何从谷歌地图api获得lat long

时间:2016-10-24 08:59:29

标签: javascript google-maps

如何从下面的功能中获取经度和纬度。

<script>        
    getPlace_dynamic();  
    function getPlace_dynamic() {
        var defaultBounds = new google.maps.LatLngBounds(
            new google.maps.LatLng(-33.8902, 151.1759),
            new google.maps.LatLng(-33.8474, 151.2631)
        );
        var input = document.getElementsByClassName('formcontrol');
        var options = {
            bounds: defaultBounds,
            types: ['establishment']
        };
        for (i = 0; i < input.length; i++) {
            autocomplete = new google.maps.places.Autocomplete(input[i], options);
        }

    }
</script>

我想从下面的行中获取lat和long

 document.getElementById('tolat').value = place.geometry.location.lat();
            document.getElementById('tolong').value = place.geometry.location.lng();

1 个答案:

答案 0 :(得分:0)

您是否需要从自动填充中获取地点信息?

Autocomplete for Addresses and Search Terms

  

“当用户从附加的预测中选择一个地点时   自动填充文本字段,该服务触发place_changed事件。您   可以在Autocomplete对象上调用getPlace()来检索   PlaceResult对象“。

所以你的代码看起来像这样

autocomplete.addListener('place_changed', function() {
    var place = this.getPlace();
    document.getElementById('tolat').value = place.geometry.location.lat();
    document.getElementById('tolong').value = place.geometry.location.lng();
    ...