使用Javascript從Google Places搜索api獲取緯度和經度

如何使用谷歌地圖搜索框api從搜索到的位置獲取經度和緯度.javascript

 我使用與谷歌演示相同的代碼 – https://developers.google.com/maps/documentation/javascript/examples/places-searchboxjava

 

搜索代碼:git

<script type="text/javascript">
    //自動搜索
    function initMap() {
        var input = document.getElementById('address_search');
        var places = new google.maps.places.Autocomplete(input);
        google.maps.event.addListener(places, 'place_changed', function () {
                var place = places.getPlace();
                var address = place.formatted_address;
                var latitude = place.geometry.location.A;
                var longitude = place.geometry.location.F;
                var mesg = "Address: " + address;
                mesg += "nLatitude: " + latitude;
                mesg += "nLongitude: " + longitude;
                console.log(place);
                GetLatlong();
        });
    }
    //根據google places搜索api獲取經緯度
    function GetLatlong()
    {
        var geocoder = new google.maps.Geocoder();
        var address = document.getElementById('address_search').value;

        geocoder.geocode({ 'address': address }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var latitude = results[0].geometry.location.lat();
                var longitude = results[0].geometry.location.lng();
                alert(latitude+","+longitude);
            }
        })
    }
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=xxxxx&libraries=places&callback=initMap">
</script>
相關文章
相關標籤/搜索