gpt4 book ai didi

android - Ionic 5 Capacitor Android 不显示地理定位结果

转载 作者:行者123 更新时间:2023-12-04 23:59:56 25 4
gpt4 key购买 nike

我有一个 Ionic 5/Capacitor 应用程序并且我正在使用地理定位。

代码如下:

import { Component } from '@angular/core';
import { Geolocation } from '@capacitor/core';


@Component({
selector: 'app-tab1',
templateUrl: 'tab1.page.html',
styleUrls: ['tab1.page.scss']
})
export class Tab1Page {

latitude: number;
longitude: number;

constructor() {
this.getLocation();
}

async getLocation() {
const position = await Geolocation.getCurrentPosition({ enableHighAccuracy: true, timeout: 5000, maximumAge: 0 });
this.latitude = position.coords.latitude;
this.longitude = position.coords.longitude;
}

}

当我在浏览器上运行它时,this.latitude 和 this.longitude 显示结果,但是当我在 Android Studio 模拟器或手机上运行它时,它根本不显示任何结果。

我该如何解决这个问题,因为它没有给我任何错误,它只是没有在我的 Android 手机或模拟器上返回任何 gps 坐标。

最佳答案

试试这个:

constructor(public geoLoc: GeoLocation){}

async getLocation() {
const position = await this.geoLoc.getCurrentPosition({enableHighAccuracy: true, timeout: 5000, maximumAge: 0}).then(res => {
this.latitude = res.coords.latitude;
this.longitude = res.coords.longitude;
}
});

我认为发生的事情是您的浏览器已经有了位置,但在手机上您必须等待它获得位置,这就是为什么您应该使用从 返回的 promise getCurrentPosition() 方法。此外,根据您使用 getLocation() 的方式,您可能还希望它在 promise 返回时返回 promise getCurrentPosition() 已经完成,否则 this.latitudethis.longitude 可能仍然是 null

关于android - Ionic 5 Capacitor Android 不显示地理定位结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60737475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com