geolocation.getCurrentPosition()在android手机中不起作用

时间:2019-03-06 04:47:45

标签: ionic4

geolocation.getCurrentPosition()在android手机中不起作用,这是离子错误吗?还是有更好的解决方案来实现这一目标,请问有人可以帮助我吗

  ngOnInit() {
            this.geolocation.getCurrentPosition().then((resp) => {
            }).catch((error) => {
                console.log('Error getting location', error);
            });
            let watch = this.geolocation.watchPosition();
            watch.subscribe((data) => {
                this.data = data;
                this.currentLat = data.coords.latitude;
                this.currentLng = data.coords.longitude;
                this.accuracy = data.coords.accuracy;
            });
        }

2 个答案:

答案 0 :(得分:1)

我曾经遇到过这个问题,并且能够通过在移动设备上启用高精度GPS来解决它。

您可以使用位置准确度插件 https://ionicframework.com/docs/native/location-accuracy 在加载应用程序时激活高精度模式

import { LocationAccuracy } from '@ionic-native/location-accuracy/ngx';

constructor(private locationAccuracy: LocationAccuracy) { }

...

this.locationAccuracy.canRequest().then((canRequest: boolean) => {

    if(canRequest) { 
       this.locationAccuracy.request(this.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY).then(
       () => console.log('Request successful'),
       error => console.log('Error requesting location permissions', error)
       );
    }

});

或者您可以简单地通过

 this.geolocation.getCurrentPosition({ enableHighAccuracy : true, timeout: 10000 } )

答案 1 :(得分:0)

尝试在变量选项

中设置下一个值
import { Geolocation, Geoposition } from '../../node_modules/@ionic-native/geolocation';

constructor(private geolocation: Geolocation)

...

 var options = {
    enableHighAccuracy: true,
    timeout: 5000,
    maximumAge: 0
    };

this.geolocation.getCurrentPosition(options)
  .then((result: Geoposition) => {
    console.log(result)
  })
  .catch(error => {
    console.error(error)
  })
});