地理位置在设备ionic3中不起作用 [英] Geolocation not working in device ionic3

查看:33
本文介绍了地理位置在设备ionic3中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

代码

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

推荐答案

试试这个:

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 设备上启用位置选项(虽然这是可能的).

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).

这篇关于地理位置在设备ionic3中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆