Reputation: 1914
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
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
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