本文介绍了cordova-plugin-geolocation watchPosition()方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Ionic2-Angular2-Typescript应用程序。我正在尝试使用Cordova的Geolocation插件,但当我在编辑器向我显示了一些错误:

I'm working on a Ionic2-Angular2-Typescript application. I'm trying to use Cordova's Geolocation plugin, but when I call watchPosition method like in http://ionicframework.com/docs/v2/native/geolocation/ editor shows me some errors:

错误TS2339:属性'coords'在'Geoposition |'类型中不存在PositionError'。

Error TS2339: Property 'coords' does not exist on type 'Geoposition | PositionError'.

这是我的代码:

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs';
import { Observable } from 'rxjs/Observable';
import { Geolocation, PositionError, Geoposition } from 'ionic-native';

export class LocationProvider {

    constructor(private http: Http) {}

    var subscription = Geolocation.watchPosition()
                          .filter((p) => p.code === undefined) //Filter Out Errors
                          .subscribe(position => {
  console.log(position.coords.longitude + ' ' + position.coords.latitude);
});
}


推荐答案

最后,我解决了我的问题如下:

Finally, I solved my problem as follows:

this.subscription = Geolocation.watchPosition().subscribe(position => {
    if ((position as Geoposition).coords != undefined) {
      var geoposition = (position as Geoposition);
      console.log('Latitude: ' + geoposition.coords.latitude + ' - Longitude: ' + geoposition.coords.longitude);
    } else { 
      var positionError = (position as PositionError);
      console.log('Error ' + positionError.code + ': ' + positionError.message);
    }
});

这篇关于cordova-plugin-geolocation watchPosition()方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 07:15