2015-07-01 54 views
2

根據文檔,我們按照以下方式(服務器API)檢索一組起始地和目的地之間的距離。使用地點ID查找距離

https://maps.googleapis.com/maps/api/distancematrix/json?origins=Washington%20DC&destinations=New%20York&mode=walking&key= 

而不是傳遞地名,如果我們通過place_id作爲原點或目的地,會更好。這會得到更準確的結果。可能嗎 ?

例如,是否可以執行如下API調用:

???

三江源

+0

只是地址或座標。您可以使用座標作爲請求參數 –

+0

@BrunoCésar獲取[Places](https://developers.google.com/maps/documentation/javascript/places#place_details)中的詳細座標和座標,因爲place_id是唯一標識符,它不應該是每個地方的普遍查找。 – user3506

回答

5

有沒有辦法讓谷歌使用place_id作爲起始點的,或者在谷歌地圖的目的地方向API。

所以你做這件事的唯一方法就是首先從place_id獲取緯度/經度或地址。
1)反向地理編碼:您可以通過做https://developers.google.com/maps/documentation/geocoding/

https://maps.googleapis.com/maps/api/geocode/json?place_id=ChIJ2eUgeAK6j4ARbn5u_wAGqWA&key=MYKEY

2)地方詳情:https://developers.google.com/places/webservice/details

https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJ2eUgeAK6j4ARbn5u_wAGqWA&key=MYKEY

更新

現在您可以在距離矩陣API中應用地點ID作爲起點和目的地。

如果您提供場所ID,則必須在它前面加上place_id :.如果請求中包含API密鑰或Google Maps API Premium計劃客戶ID,則只能指定地點ID。

請參考文檔的詳細信息:

https://developers.google.com/maps/documentation/distance-matrix/intro#RequestParameters

2

可以使用origin=place_id:<place id>,而不是origin="<address>"欲瞭解更多信息,請參閱該link

0

是的,你可以這樣做。看看這個:https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-directions

this.directionsService.route({ 
     origin: {'placeId': this.originPlaceId}, 
     destination: {'placeId': this.destinationPlaceId}, 
     travelMode: this.travelMode 
    }, function(response, status) { 
     if (status === 'OK') { 
     me.directionsDisplay.setDirections(response); 
     } else { 
     window.alert('Directions request failed due to ' + status); 
     } 
    }); 
相關問題