我正在使用以下代码绑定起点和终点-纬度和经度。获取起点(当前位置)和终点(我从标记click获得值)之后,我将参数传递给url并尝试打开新标签页。问题是参数未传递到Google地图网址。
getDirection(dirLat: any, dirLng: any) {
let srcLat, srcLng;
if ('geolocation' in navigator) {
navigator.geolocation.getCurrentPosition((position) => {
srcLat = position.coords.latitude;
srcLng = position.coords.longitude;
});
}
this.dir = {
origin: { latitude: 29.9809683, longitude: 31.3377553 },
destination: { latitude: dirLat, longitude: dirLng }
};
// prints the values correctly for origin and destination{latitude: 29.9809683, longitude: 31.3377553}
console.log('originDest', this.dir.origin, this.dir.destination);
//in the below url, dir.origin and dir.destination does not get the values
window.open('https://www.google.com/maps/dir/?api=1&origin={{dir.origin}}&destination={{dir.destination}}&travelmode=driving', '_blank');
}
答案 0 :(得分:1)
var p ='origin';
var d = 'destination';
window.open('https://www.google.com/maps/dir/?api=1&origin='+p+'&destination='+d+'&travelmode=driving','_blank' );
您可以通过这种方式绑定起点和终点
答案 1 :(得分:0)
getCurrentPosition异步尝试获取设备的当前位置。
由于异步性质,控制台日志显示正确的值。在这里详细阅读说明 Weird behavior with objects & console.log
在此处阅读有关地理信息API的更多详细信息 https://w3c.github.io/geolocation-api/#geolocation_interface