我有一个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坐标。
发布于 2020-03-18 21:10:23
试试这个:
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;
}
});我认为发生的情况是浏览器已经有了位置,但在手机上你必须等待它才能获得位置,这就是为什么你应该使用从getCurrentPosition()方法返回的promise。此外,根据您使用getLocation()的方式,您可能还希望它在满足从getCurrentPosition()返回的promise时返回一个promise,否则this.latitude和this.longitude很可能仍然是null。
https://stackoverflow.com/questions/60737475
复制相似问题