Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Places get LatLng

I am using the Autocomplete method to get Place Suggestions and when I click the Place I want to choose, I want to extract the Lat and Lng which is placed under place.geometry.location as below.

Firebug Screenshot

As per my observation, the keys ib and jb keep changing with every session. Is there any way I can extract the Lat and Lng predictably?

$(document).ready(function() {

    var mapOptions = {
        center : new google.maps.LatLng(-33.8688, 151.2195),
        zoom : 13,
        mapTypeId : google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById('map'), mapOptions);

    $('#searchTextField').bind('keydown keypress', function() {
        setTimeout(function() {
            var inputQuery = $('#searchTextField').val();

            if (inputQuery.length >= 2) {
                //console.log(inputQuery);

                /*
                var service = new google.maps.places.AutocompleteService();

                service.getPlacePredictions({
                    input : inputQuery
                }, callback);
                */

                var input = document.getElementById('searchTextField');
                var options = {
                    types : ['geocode']
                };

                var autocomplete = new google.maps.places.Autocomplete(input, options);

                // Acting on Selecting a place
                google.maps.event.addListener(autocomplete, 'place_changed', function() {
                    //infowindow.close();
                    var place = autocomplete.getPlace();

                    console.log(place);
                    console.log(place.formatted_address);
                    console.log(place.name);
                    console.log(place.geometry.location);
                    console.log(place.geometry.location[0]);

                    // Show the map to the current location selected
                    if (place.geometry.viewport) {
                        map.fitBounds(place.geometry.viewport);
                    } else {
                        map.setCenter(place.geometry.location);
                        map.setZoom(17);
                        // Why 17? Because it looks good.
                    }

                    var marker = new google.maps.Marker({
                        position : place.geometry.location,
                        map : map,
                        draggable : true,
                    });

                    $.each(place.geometry.location, function(key, value) {
                        console.log(key + ": " + value);
                    }); 
                });
            }

        }, 0);

    });
});
like image 318
Harsha M V Avatar asked Mar 09 '13 19:03

Harsha M V


People also ask

What is LatLng in Google map?

A LatLng is a point in geographical coordinates: latitude and longitude. Latitude ranges between -90 and 90 degrees, inclusive.

How do I find LatLng address?

The short story is you need to do: Geocoder geocoder = new Geocoder(this, Locale. getDefault()); List<Address> addresses = geocoder. getFromLocation(lat, lng, 1);

How do I get my Google places API zip code?

ZIP code lookup The request format is: http://maps.googleapis.com/maps/api/geocode/json?address=ZIP_CODE&key=YOUR_API_KEY will, and in the response you get a lot of information, including for our purposes, that 92101 is the postal code for San Diego, California.


1 Answers

place.geometry.location is a LatLng, so you can call its .lat() and .lng() methods.

var location = place.geometry.location;
var lat = location.lat();
var lng = location.lng();

You'll see that these methods are present in your inspection of this object.

Never use undocumented properties such as the ib and jb you discovered. Google compiles the Maps API using the Closure Compiler or a similar tool, which generates random names for private properties and variables.

like image 112
Michael Geary Avatar answered Sep 21 '22 12:09

Michael Geary