根据文档,我们按以下方式检索一组源和目标之间的距离(服务器API)。
https://maps.googleapis.com/maps/api/distancematrix/json?origins=Washington%20DC&destinations=New%20York&mode=walking&key=
如果我们将place_id作为原点或目的地传递,那么最好不要传递地名。这样可以得到更准确的结果。有可能吗?
例如。是否可以执行API调用,如:
https://maps.googleapis.com/maps/api/distancematrix/json?origins=<place_id>&destinations=<place_id>&mode=walking&key=
???
三江源
答案 0 :(得分:6)
没有办法让google使用place_id作为原点,或谷歌地图方向API中的目的地。
因此,您这样做的唯一方法是首先从place_id获取lat / lng或地址。你可以这样做:
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
<强>更新强>
现在,您可以在Distance Matrix API中将地点ID用作原点和目的地。
如果您提供地点ID,则必须在其前面加上place_id:。如果请求包含API密钥或Google Maps API Premium Plan客户端ID,则您只能指定地点ID。
有关详细信息,请参阅文档:
https://developers.google.com/maps/documentation/distance-matrix/intro#RequestParameters
答案 1 :(得分:2)
您可以使用origin=place_id:<place id>
代替origin="<address>"
。有关详细信息,请参阅link。
答案 2 :(得分:0)
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);
}
});