0

我有一个谷歌地图 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-->

现在我还想要输入地址的经度和纬度。任何人都知道如何获得输入地址的经度和纬度值。

4

2 回答 2

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();
于 2012-10-08T09:42:16.420 回答
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">
于 2012-10-08T11:09:00.743 回答