Getting Latitude and Longitude from Google Places search api using Javascript

前端 未结 5 646
小鲜肉
小鲜肉 2020-12-30 22:22

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://de

5条回答
  •  囚心锁ツ
    2020-12-30 23:10

    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());
    });
    

提交回复
热议问题