42

How do I get the longitude and latitude from the searched location with the google maps place search box api.

Im using the same code as the google demo - https://developers.google.com/maps/documentation/javascript/examples/places-searchbox

Programit
  • 555
  • 1
  • 4
  • 10
  • 3
    possible duplicate of [Google Maps get LatLng value](http://stackoverflow.com/questions/15315017/google-maps-get-latlng-value/15315483#15315483) – geocodezip Jan 04 '14 at 03:31
  • Possible duplicate of [Google Places get LatLng](https://stackoverflow.com/questions/15315017/google-places-get-latlng) – Chris Moschini Jun 21 '18 at 16:53

5 Answers5

64
function GetLatlong() {
  var geocoder = new google.maps.Geocoder();
  var address = document.getElementById('textboxid').value;

  geocoder.geocode({
    'address': address
  }, function(results, status) {

    if (status == google.maps.GeocoderStatus.OK) {
      var latitude = results[0].geometry.location.lat();
      var longitude = results[0].geometry.location.lng();
    }
  });
}

You can use the above function which will give you the latitude and longitude for the area entered by user.

Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
Piyush Sahu
  • 802
  • 7
  • 10
  • Been searching around for days on how to do this with people constantly pointing me towards using the googe.maps.places.placesservice.getPlaceDetails() function. Now I know its the Geocoder I needed all this time! If only I found this answer a few days ago! You can use the autocomplete and add an eventlistener to it for the change event and then get the places from that list but this is a much easier solution especially when working with an angular app. This should be marked as the answer in my eyes – James Blackburn Sep 26 '19 at 09:22
26

The code on the link you provide shows a function to do when a search is entered. First, it creates an empty array of markers (the pins you see on the map once you perform a search).

So, check the function starting with:

google.maps.event.addListener(searchBox, 'places_changed', function() {

You'll see later on that a marker has been created (there's even a comment):

// Create a marker for each place.
var marker = new google.maps.Marker({
    map: map,
    icon: image,
    title: place.name,
    position: place.geometry.location
});

So, on place.geometry.location you have a Google Maps Location object. You could use place.geometry.location.lat() and place.geometry.location.lng().

Check here, too: https://stackoverflow.com/a/15315483/1012139

Community
  • 1
  • 1
Patrick D'appollonio
  • 2,722
  • 1
  • 18
  • 34
9

From the docs:

// Autocomplete Options
var defaultBounds = new google.maps.LatLngBounds();
var options = {
  types: ['(cities)'],
  bounds: defaultBounds
};

// get DOM's input element
var input = document.getElementById('location_address');

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

// Listener for whenever input value changes            
autocomplete.addListener('place_changed', function() {

  // Get place info
  var place = autocomplete.getPlace();

  // Do whatever with the value!
  console.log(place.geometry.location.lat());
});
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
Seif
  • 701
  • 4
  • 13
  • 32
1

HTML:

<input type="text" id="address" name="address" value=""> //Autocomplete input address

<input type="hidden" name="s_latitude" id="s_latitude" value="" /> //get latitude
<input type="hidden" name="s_longitude" id="s_longitude" value="" /> //get longitude

Javascript:

<script src="https://maps.googleapis.com/maps/api/js?key=API_KEY&libraries=places&callback=initMap"
async defer></script>

<script>
var input = document.getElementById('address');
var originLatitude = document.getElementById('s_latitude');
var originLongitude = document.getElementById('s_longitude');

var originAutocomplete = new google.maps.places.Autocomplete(input);

    
originAutocomplete.addListener('place_changed', function(event) {
    var place = originAutocomplete.getPlace();

    if (place.hasOwnProperty('place_id')) {
        if (!place.geometry) {
                // window.alert("Autocomplete's returned place contains no geometry");
                return;
        }
        originLatitude.value = place.geometry.location.lat();
        originLongitude.value = place.geometry.location.lng();
    } else {
        service.textSearch({
                query: place.name
        }, function(results, status) {
            if (status == google.maps.places.PlacesServiceStatus.OK) {
                originLatitude.value = results[0].geometry.location.lat();
                originLongitude.value = results[0].geometry.location.lng();
            }
        });
    }
});
</script>
jai
  • 71
  • 12
-3
navigator.geolocation.getCurrentPosition(success => {

  console.log(success) // `have the lat and long`

}, failure =>{

//`enter code here if failed`

});
Dharman
  • 30,962
  • 25
  • 85
  • 135