簡體   English   中英

如何從google Map中的經度和緯度獲取place_id?

[英]How to get place_id from longitude and latitude in google Map?

我可以使用此鏈接獲取地點ID。 但問題是這是基於位置搜索。 我想使用draggeble標記獲取place_id,所以我不能使用上面的鏈接。 我正在谷歌地圖上移動標記,所以我如何在地圖上獲得place_id。

通過marker.getPosition()我們可以得到緯度和經度。 所以我想通過緯度和經度知道place_id 我怎么能得到,請告訴我

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

使用google.maps.Geocoder進行Reverse Geocoding如下所述: https//developers.google.com/maps/documentation/javascript/examples/geocoding-reverse

您將收到包含results[1].place_id數據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);
    }
  });

您可以使用Geocoder API發送請求,也可以只將HTTP請求發送到后面的Web服務。

您的樣品申請:

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

然后,您可以通過Javascript JSON.parse()輕松解析JSON對象。

我有了解決這個問題的新方法。 在這里,我使用谷歌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']);
        });

希望你會發現這很有用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM