微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

离子框架 – 地理位置不适用于离子3

我正在使用离子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);
});

解决方法

试试这个:

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐