ionic3 - 无法读取属性“纬度”未定义
问题描述
我需要帮助,这是问题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);});
}
解决方案
如果要获取当前的经纬度,请使用 getCurrentPosition 方法
this.geolocation.getCurrentPosition().then((position) => {
this.latitude = position.coords.latitude;
this.longitude = position.coords.longitude;
});
推荐阅读
- c++ - 将成员函数传递给 std:function 接口的现代首选方式
- sql-server - SQL 中的年龄计算器
- angular - 如何模拟组件实例事件
- laravel - Laravel Nova Action 停止关闭模式
- python - 如何让 Ctrl+C 导致 SSH 调用的 Python 退出套接字接收?
- c - 在 c 中使用 libpng 编写正弦波
- javascript - 从嵌套数组中删除特定项目并保存结果,如何使这段代码更干净
- python - 在最坏的情况下尝试快速排序。它运行良好,但在最坏的情况下会发生未知错误。我认为
- c# - 通过 pdfsharp 1.32 将文档固定为 pdf
- python-2.7 - 以不同的方式获取和设置 np.array 元素