获取当前位置的角度坐标 [英] Get coordinates of current location in angular
本文介绍了获取当前位置的角度坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在服务中创建了以下函数,以获取当前位置的坐标并将其存储在2个变量中:
I created the following function in a service, to get the coordinates of the current location and store them in 2 variables:
getCurrentLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
this.currLat = position.coords.latitude;
this.currLng = position.coords.longitude;
});
}
else {
alert("Geolocation is not supported by this browser.");
}
}
如何通过此函数返回坐标以便在组件中使用它们?
how can I return the coordinates through this function in order to use them in a component?
推荐答案
您可以返回承诺.
locationService.ts
getPosition(): Promise<any>
{
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(resp => {
resolve({lng: resp.coords.longitude, lat: resp.coords.latitude});
},
err => {
reject(err);
});
});
}
component.ts
this.locationService.getPosition().then(pos=>
{
console.log(`Positon: ${pos.lng} ${pos.lat}`);
});
这篇关于获取当前位置的角度坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文