[英]How to use Fused Location API with capacitor ? - Ionic 5
I'm building an ionic app with capacitor.我正在用电容器构建一个离子应用程序。 Currently i'm running in a use case where I need to use GPS to get user location.
目前我正在一个用例中运行,我需要使用 GPS 来获取用户位置。
After digging out for almost 2hrs, I found out that fused location is the way to go.挖了将近2小时后,我发现融合位置是通往go的路。
From capacitor official documents I couldn't find relevant info on how to call fused location to get geo location on android.从电容器官方文档中,我找不到有关如何调用融合位置以获取 android 上的地理位置的相关信息。 Below is the tested code that runs on browsers but fails on device.
下面是在浏览器上运行但在设备上失败的测试代码。
import { Geolocation } from '@capacitor/core';
export class HomePage implements OnInit {
latitude: number;
longitude: number;
constructor() {}
async getCurrentPosition() {
const coordinates = await Geolocation.getCurrentPosition();
console.log('Current', coordinates);
}
watchPosition() {
Geolocation.watchPosition({}, (position, err) => {
this.latitude = position.coords.latitude;
this.longitude = position.coords.longitude
console.log(position);
})
}
}
If you don't see the position in the template you probably need to run the callback inside the NgZone:如果您在模板中没有看到 position,您可能需要在 NgZone 中运行回调:
import { Component, NgZone } from '@angular/core';
// ...
constructor(
private ngZone: NgZone,
) { }
watchPosition() {
this.locationWatchId = Geolocation.watchPosition(this.geolocationOptions, (position, err) => {
this.ngZone.run(() => {
if (err) {
console.error('Failed to watch position.', err);
}
this.latitude = position.coords.latitude;
this.longitude = position.coords.longitude
console.log(position);
});
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.