从Google地图地址获取经度和纬度值

时间:2012-10-08 07:59:48

标签: google-maps geolocation location

我有一个谷歌地图API,它提供谷歌地图地址。这是获取谷歌地图API的代码:

<input name="add" type="text" id="address" class="required" style="width: 500px;">
            <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=places&language=en-AU"></script>
            <script>
                var autocomplete = new google.maps.places.Autocomplete($("#address")[0], {});

                google.maps.event.addListener(autocomplete, 'place_changed', function() {
                var place = autocomplete.getPlace();
                console.log(place.address_components);
                });
            </script>
        <!--code for Google address API-->

现在我也想要输入地址的经度和纬度。任何人都有这个想法我怎样才能得到经度值和经度值输入地址的纬度。

2 个答案:

答案 0 :(得分:1)

您可以使用场所的几何对象来获得lat和lng。如果我输入有效目的地并从自动填充选项中选择一个,则以下内容适用于我。

<input name="add" type="text" id="address" class="required" style="width: 500px;">
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=places&language=en-AU"></script>
<script>
    var autocomplete = new google.maps.places.Autocomplete(document.getElementById('address'));

    google.maps.event.addListener(autocomplete, 'place_changed', function() {
        var place = autocomplete.getPlace();
        var geometry = place.geometry;
        var location = geometry.location;
        var latitude = location.lat();
        var longitude = location.lng();
        console.log(latitude);
        console.log(longitude);
    });
</script>

甚至只是:

var place = autocomplete.getPlace();
var latitude = place.geometry.location.lat();
var longitude = place.geometry.location.lng();

答案 1 :(得分:1)

我得到了答案:

        <script>

        function cordinate()
        {
            geocoder = new google.maps.Geocoder(); // creating a new geocode object
            // getting the address value
            address1 = document.getElementById("address").value;
            //alert(address1);
                if (geocoder)
            {
            geocoder.geocode( { 'address': address1}, function(results, status)
            {
            if (status == google.maps.GeocoderStatus.OK)
            {
            //location of address (latitude + longitude)
            var location1 = results[0].geometry.location;
            document.getElementById("cor").value = location1;

            //alert(location1);
            } else
            {
            alert("Geocode was not successful for the following reason: " + status);
            }
            });

            }//end of If

        }
        </script>
        <!--code for google address cordinates-->
        <input type="hidden" name="cor" id="cor">