加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 13:11:05 所属栏目:百科 来源:网络整理
导读:我正在使用离子3基于位置的工作.我无法在这里获得纬度和经度的当前位置.我提到了我可用的代码.它在浏览器级别上运行良好,但在移动设备中无法正常工作. 码 $ionic cordova plugin add cordova-plugin-geolocation --variable GEOLOCATION_USAGE_DESCRIPTION="
我正在使用离子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设备上启用位置选项(尽管这很可能).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读