35

I'm testing out the Google Places autocomplete feature here:

https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete

I want to get the latitude and longitude of the Place, but I'm having some troubles. When I use the following code:

var place = autocomplete.getPlace();
console.log(place.geometry.location);

I get this returned:

enter image description here

When I use it in an infoWindow like this:

infowindow.setContent('
<div><strong>' + place.name + '</strong><br>' + place.geometry.location);

place.geometry.location is then displayed like this:

(53.539834, -113.49402099999998)

All I want to do is get the lat and lng separately. place.geometry.location.lat doesn't work. Not sure what else to do.

dallen
  • 2,621
  • 7
  • 37
  • 47

5 Answers5

103

You can use like this.

var latitude = place.geometry.location.lat();
var longitude = place.geometry.location.lng();  
Sunil Dodiya
  • 2,605
  • 2
  • 18
  • 21
  • 1
    The Place Details JSON object looks like this: https://developers.google.com/places/webservice/details – ruhong Jul 07 '15 at 03:26
  • It didn't work for me if you search for 'The Shard, London' :| – Aamir Afridi Dec 24 '15 at 01:57
  • 7
    This is ridiculous. The JSON response is documented here: https://developers.google.com/places/web-service/details#PlaceDetailsResponses Why on earth would they replace fields `.lat` and `.lng` from the JSON response with methods `.lat()` and `.lng()`? (these are the only fields on the JSON that are replaced with the methods) – jakub.g Nov 21 '16 at 13:18
2

You can use:

var LatLng = place.geometry.location.toJSON();
The F
  • 3,647
  • 1
  • 20
  • 28
Sabbir
  • 359
  • 2
  • 10
  • I was unsure if this is a public API but it seems to be: https://developers.google.com/maps/documentation/javascript/reference#LatLng – jakub.g Nov 21 '16 at 15:15
1

It's working fine for me.

marker.setIcon(image);

marker.setPosition(place.geometry.location);

var address = '';
if (place.address_components) {
    address = [
        (place.address_components[0] && place.address_components[0].short_name || ''),
        (place.address_components[1] && place.address_components[1].short_name || ''),
        (place.address_components[2] && place.address_components[2].short_name || '')
    ].join(' ');
}
alert(place.geometry.location.lat());
alert(place.geometry.location.lng());
infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
infowindow.open(map, marker);
Himanshu
  • 31,810
  • 31
  • 111
  • 133
Muthu
  • 11
  • 1
1

You can get the LAT and LONG from the PLACES object.

Like this :

const autocomplete = new google.maps.places.Autocomplete(this.inputNativeElement.nativeElement as HTMLInputElement);

autocomplete.addListener('place_changed', () => {
    const place = autocomplete.getPlace();

    let cus_location = {
        lat: place.geometry.location.lat(),
        long: place.geometry.location.lng()
    }

}
Sushil
  • 2,324
  • 1
  • 27
  • 26
0

you can bring the values to desired html object as well

var place = autocomplete.getPlace(); $('#latitude').val(place.geometry['location'].lat()); $('#longitude').val(place.geometry['location'].lng());

N.K
  • 38
  • 5