簡體   English   中英

Ionic 5 Capacitor Android 不顯示地理定位結果

[英]Ionic 5 Capacitor Android not showing geolocation results

我有一個 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; 
    }   
});

我認為正在發生的事情是您的瀏覽器已經有了位置,但是在電話上您必須等待它獲取位置,這就是為什么您應該使用從getCurrentPosition()方法返回的promise 此外,這取決於你是如何使用getLocation()您可能還希望它返回一個promisepromise從返回getCurrentPosition()已經完成,否則this.latitudethis.longitude將可能仍然是null

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM