首页 > 解决方案 > 无法读取属性“纬度”未定义

问题描述

我需要帮助,这是问题watchPosition()还是我做错了什么?

这是代码:

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

 private lat: any;
 private lng: any;
 constructor( private geolocation: Geolocation ) {

 let watch = this.geolocation.watchPosition();
watch.subscribe((data) => {

 // data can be a set of coordinates, or an error (if an error occurred).
 this.lat= data.coords.latitude;
 this.lng= data.coords.longitude;
},(error)=>{console.log(error);});

  }

标签: ionic3

解决方案


如果要获取当前的经纬度,请使用 getCurrentPosition 方法

this.geolocation.getCurrentPosition().then((position) => {

      this.latitude = position.coords.latitude;
      this.longitude = position.coords.longitude;
     });

推荐阅读