简体   繁体   English

如何使用 Fused Location API 与电容器? - 离子 5

[英]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.

相关问题 如何在 ModalPage Ionic 6/Capacitor 中使用自定义组件? - How to use a custom component in a ModalPage Ionic 6/Capacitor? 如何在 Ionic 6/Capacitor 中使用不同的 admob 方法? - How to use different admob methods in Ionic 6/Capacitor? 离子/电容器反应应用程序 API 请求在 iOS 上返回 HTML - Ionic/Capacitor React App API Requests returning HTML on iOS 如何使用浏览器插件隐藏 Ionic 5 Capacitor 中的 URL 栏? - How to hide URL Bar in Ionic 5 Capacitor using Browser Plugin? 如何使用打算在 vanilla js 应用程序中导入的电容器插件 - How to use a capacitor plugin meant to be imported in a vanilla js app 如何在 iOS 的 Ionic 电容器中打开 webview 自动完成登录表单? - How do I turn on the webview autocomplete for an login form in Ionic's capacitor in iOS? 如何将文本字段中的填充值保存到 Ionic 6/Capacitor 中的 SQL 精简版数据库? - How do I save a filled value in a textfield to a SQL lite database in Ionic 6/Capacitor? 如何在 Android-studio 或移动设备上运行 ionic-capacitor 应用程序? - How can i run ionic-capacitor app on Android-studio or mobile device? Firebase 认证棒电容 Ionic on iOS - Firebase Authentication Sticks Capacitor Ionic on iOS 如何使用Google Maps API在用户位置显示地图和标记 - How to use google maps api to display map and marker at users location
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM