首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ionic 5 Capacitor Android未显示地理位置结果

Ionic 5 Capacitor Android未显示地理位置结果
EN

Stack Overflow用户
提问于 2020-03-18 18:20:41
回答 1查看 2.1K关注 0票数 3

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

代码如下:

代码语言:javascript
复制
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坐标。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-18 21:10:23

试试这个:

代码语言:javascript
复制
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.latitudethis.longitude很可能仍然是null

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60737475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档