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

时间:2015-10-15 10:46:40

标签: javascript google-maps dictionary google-maps-api-3

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

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

latitude=marker.getPosition().lat();               
longitude=marker.getPosition().lng();

3 个答案:

答案 0 :(得分:17)

使用google.maps.Geocoder Reverse 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);
    }
  });

答案 1 :(得分:3)

我有了解决这个问题的新方法。在这里,我使用谷歌http服务获取基于经度和纬度的位置的总信息。您只需要在url和api密钥中传递纬度和经度(例如:latlng = 21.1497409,79.08747970000002& key =您的API密钥)。这是我在ExampleService类中的get服务

 getService(url) {

    return this.http.get(url).map((data: any) => data.json())

}

这可以放在你想要的任何地方,只需从需要位置数据的组件中调用以下服务

this._exampleService.getService("https://maps.googleapis.com/maps/api/geocode/json?latlng=21.1497409, 79.08747970000002&key=YOUR API KEY").subscribe(getplaceData => {
            var placeDataDest: any;
            placeDataDest = getplaceData;
            console.log("i got place id yeee " + placeDataDest['results'][0]['place_id']);
            console.log("i got place details yeee " + placeDataDest['results']);
        });

希望你能找到这个有用的

答案 2 :(得分:3)

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

您的样品申请:

https://maps.googleapis.com/maps/api/geocode/json?latlng= LAT ,<强> LNG &安培;键= <强> YOUR_API_KEY

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