Harshal
Harshal

Reputation: 3622

Get longitude and latitude values from Google Map address

I have a Google maps API which gives the Google map address.Here is the code to get the Google map 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-->

Now i also want the longitude And Latitude of entered address.any one have idea about this how can i get The values of longitude & Latitude of the entered address.

Upvotes: 0

Views: 1983

Answers (2)

duncan
duncan

Reputation: 31930

You can use the place's geometry object to get a lat and lng. The following works for me, if I type in a valid destination and select one from the autocomplete options.

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

Or even just:

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

Upvotes: 1

Harshal
Harshal

Reputation: 3622

I got the answer :

        <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">

Upvotes: 1

Related Questions