Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PlaceResult object returns latitude/longitude as object, not sure how to get them individually

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.

like image 882
dallen Avatar asked May 18 '12 05:05

dallen


3 Answers

You can use like this.

var latitude = place.geometry.location.lat();
var longitude = place.geometry.location.lng();  
like image 53
JDev Avatar answered Oct 17 '22 23:10

JDev


You can use:

var LatLng = place.geometry.location.toJSON();
like image 27
Sabbir Avatar answered Oct 17 '22 23:10

Sabbir


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);
like image 1
Muthu Avatar answered Oct 17 '22 22:10

Muthu