简体   繁体   English

使用 Javascript 从 Google Places 搜索 API 获取纬度和经度

[英]Getting Latitude and Longitude from Google Places search api using Javascript

How do I get the longitude and latitude from the searched location with the google maps place search box api.如何使用谷歌地图位置搜索框 api 从搜索位置获取经度和纬度。

Im using the same code as the google demo - https://developers.google.com/maps/documentation/javascript/examples/places-searchbox我使用与谷歌演示相同的代码 - https://developers.google.com/maps/documentation/javascript/examples/places-searchbox

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.您可以使用上述功能,该功能将为您提供用户输入区域的纬度和经度。

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.因此,在place.geometry.location您有一个 Google Maps Location 对象。 You could use place.geometry.location.lat() and place.geometry.location.lng() .您可以使用place.geometry.location.lat()place.geometry.location.lng()

Check here, too: https://stackoverflow.com/a/15315483/1012139也可以在这里查看: https : //stackoverflow.com/a/15315483/1012139

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

我想您可能想要查看Google地图地理编码服务

HTML: 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: 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>
navigator.geolocation.getCurrentPosition(success => {

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

}, failure =>{

//`enter code here if failed`

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM