Semicolon
Semicolon

Reputation: 1914

Google maps API: get lat / long separately

After sending a geocode request and receiving a response with status OK, you can use results[0].geometry.location to retrieve both latitude and longitude.

But how do you get the lat / long separately? I can't find the right call in the Google maps API reference

Upvotes: 0

Views: 2035

Answers (3)

SMHussain
SMHussain

Reputation: 31

<input id="pac-input" type="text" placeholder="Enter a location">
<div id="map"></div>

<script>

function initMap(){

var input = document.getElementById('pac-input');
var autocomplete = new google.maps.places.Autocomplete(input);

autocomplete.bindTo('bounds', map);
autocomplete.addListener('place_changed', function() {

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

});

});

</script>

Upvotes: 0

geocodezip
geocodezip

Reputation: 161404

results[0].geometry.location is a google.maps.LatLng object, it has a .lat() method that returns the latitude and a .lng() method that returns the longitude.

Upvotes: 1

David
David

Reputation: 34573

That location field is an object of type LatLng. To get the latitude, call it's lat() method. To get the longitude, call it's lng() method.

For example:

var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();

Upvotes: 2

Related Questions