【问题标题】:Geolocation not working in device ionic3地理位置在设备ionic3中不起作用
【发布时间】:2018-09-17 03:57:09
【问题描述】:

我正在从事基于 ionic 3 位置的工作。我无法在这里获得当前的纬度和经度位置。我提到了我的可用代码。它在浏览器级别可以正常工作,但在移动设备中无法正常工作。

代码

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

【问题讨论】:

  • @Suraj Rao 我听不懂
  • 我尽我所能编辑了你的问题。如果我有什么问题,请告诉我..
  • 不不不正确一切都很好你有任何想法@SurajRao
  • 尝试在this.platform.ready().then(()=>{}) 中设置地理定位调用。也是构造函数内部的调用吗?
  • 同样不工作@SurajRao

标签: ionic-framework ionic2 geolocation ionic3 hybrid-mobile-app


【解决方案1】:

尝试在 ionViewDidLoad() 或 ngAfterViewInit() 方法中调用地理定位函数。

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

希望这能解决您的问题!

【讨论】:

  • 我遇到了这样的错误:只允许安全来源(参见:goo.gl/Y0ZkNV)
  • 不要在网络浏览器上尝试。使用移动设备或模拟器进行测试。
  • 我正在使用移动设备浏览器在设备上运行良好,但出现错误
  • Gunawardhana 有什么想法吗?
  • 不,兄弟..无法识别..如果我能找到任何东西,我会发表评论
【解决方案2】:

试试这个:

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

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

【讨论】:

  • 我们设置超时的目的是什么?@frnac
  • @jose timeout 是您可以传递给 getCurrentPosition() 方法的可选参数之一。它以毫秒为单位。超时很重要,因为它可以让您的请求在您声明的持续时间内运行,然后让它传递到下一行,即控制台错误。超时时间越长,请求越长,但消耗的设备资源也越多。
【解决方案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; 

});

});
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多