如何从google Map中的经度和纬度获取place_id?

Nee*_*ngh 8 javascript google-maps dictionary google-maps-api-3

我可以使用此链接获取地点ID .但问题是这是基于位置搜索.我想使用draggeble标记获取place_id,所以我不能使用上面的链接.我正在谷歌地图上移动标记,所以我如何在地图上获得place_id.

通过marker.getPosition()我们可以得到纬度和经度.所以我想通过纬度和经度知道place_id.我怎么能得到,请告诉我

latitude=marker.getPosition().lat();               
longitude=marker.getPosition().lng();
Run Code Online (Sandbox Code Playgroud)

Iva*_*vić 19

使用google.maps.GeocoderReverse Geocoding如下解释:https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse

您将收到包含的数据results[1].place_id.

var geocoder = new google.maps.Geocoder;

latitude=marker.getPosition().lat();               
longitude=marker.getPosition().lng();
var latlng = {lat: parseFloat(latitude), lng: parseFloat(longitude)};

geocoder.geocode({'location': latlng}, function(results, status) {
    if (status === google.maps.GeocoderStatus.OK) {
      if (results[1]) {
        console.log(results[1].place_id);
      } else {
        window.alert('No results found');
      }
    } else {
      window.alert('Geocoder failed due to: ' + status);
    }
  });
Run Code Online (Sandbox Code Playgroud)

  • 我想知道:为什么要使用第二个结果而不是“results[0]”? (3认同)

Man*_*ing 5

您可以使用Geocoder API发送请求,也可以直接将HTTP请求发送到后面的Web服务。

您的样品要求:

https://maps.googleapis.com/maps/api/geocode/json?latlng= latlng&key = YOUR_API_KEY

然后,您可以通过Javascript JSON.parse()轻松解析JSON对象。