【发布时间】:2018-10-21 17:44:12
【问题描述】:
我正在使用谷歌地图路线服务来计算行程时间。
this.mapsAPILoader.load().then(() => {
const p1 = new google.maps.LatLng(50.926217, 5.342043);
const p2 = new google.maps.LatLng(50.940525, 5.353626);
const directionsService = new google.maps.DirectionsService();
const request = {
origin: p1,
destination: p2,
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
};
directionsService.route(request, (response, status) => {
if (status === google.maps.DirectionsStatus.OK) {
const point = response.routes[0].legs[0];
// console.log(point.duration.text);
this.travelTimeDriving = point.duration.text;
}
});
});
控制台记录了正确的驾驶时间,但我的变量 this.travelTimeDriving 保持为空。
我猜它与回调函数和范围有关,但我无法修复它。
路由函数也返回 void,没有承诺,所以我不能使用 .then()
【问题讨论】:
-
你能解释一下你为什么投反对票吗?
标签: javascript angular google-maps typescript directions