地理位置在设备ionic3中不起作用

jos*_*ose 7 geolocation hybrid-mobile-app ionic-framework ionic2 ionic3

我正在与ionic 3基于位置的工作。我无法在此处获取当前的纬度和经度位置。我提到了我的可用代码。在浏览器级别上可以正常工作,但不能在移动设备上工作。

$ ionic cordova plugin add cordova-plugin-geolocation --variable GEOLOCATION_USAGE_DESCRIPTION="To locate you"
$ npm install --save @ionic-native/geolocation
Run Code Online (Sandbox Code Playgroud)
$ ionic cordova plugin add cordova-plugin-geolocation --variable GEOLOCATION_USAGE_DESCRIPTION="To locate you"
$ npm install --save @ionic-native/geolocation
Run Code Online (Sandbox Code Playgroud)

fra*_*anc 5

尝试这个:

import { Geolocation } from '@ionic-native/geolocation';
import { Platform } from 'ionic-angular';

//Set the properties in this class
long: any; //longitude
lati: any; //latitude

constructor(private platform: Platform, private geolocation: Geolocation) {
this.platform.ready().then(()=>{

//set options.. 
var options = {
           timeout: 20000 //sorry I use this much milliseconds
       }
//use the geolocation 
this.geolocation.getCurrentPosition(options).then(data=>{
  this.long = data.coords.longitude;
  this.lati = data.coords.latitude;
 }).catch((err)=>{
     console.log("Error", err);
   });
});
}
Run Code Online (Sandbox Code Playgroud)

让它在构造函数中。不要忘记同意位置隐私权限,还要在您的 Android 设备上启用位置选项(虽然这很可能)。