Geolocation not working in device ionic3

☆樱花仙子☆ 提交于 2020-01-03 08:42:10

问题


I am working with ionic 3 location-based work. I am not able to get current location of latitude and longitude here. I mentioned my usable code. It's working fine in browser level but not working in a mobile device.

code

$ ionic cordova plugin add cordova-plugin-geolocation --variable GEOLOCATION_USAGE_DESCRIPTION="To locate you"
$ npm install --save @ionic-native/geolocation
import { Geolocation } from '@ionic-native/geolocation';

constructor(private geolocation: Geolocation) {}

this.geolocation.getCurrentPosition().then((resp) => {
  console.log( resp.coords.latitude)
 console.log( resp.coords.longitude)
}).catch((error) => {
  console.log('Error getting location', error);
});

回答1:


Try this:

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);
   });
});
}

Let this be in the constructor. Don't forget to agree to the location privacy permission, also enable location option on your Android device(this is probable though).




回答2:


Try to call the geolocation function inside ionViewDidLoad() or ngAfterViewInit() method.

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

constructor(private geolocation: Geolocation) {}

ngAfterViewInit(){
  this.geolocation.getCurrentPosition().then((resp) => {
    console.log( resp.coords.latitude)
    console.log( resp.coords.longitude)
  }).catch((error) => {
    console.log('Error getting location', error);
  });
}

I hope this will solve your problem!




回答3:


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 = {
        enableHighAccuracy: true, timeout: 60000, maximumAge: 0
    };
//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);
   });

 let watch = this.geolocation.watchPosition(options);
          watch.subscribe((data) => {
            let lat_lng = data.coords.latitude+","+data.coords.longitude; 

});

});
}


来源:https://stackoverflow.com/questions/49704357/geolocation-not-working-in-device-ionic3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!