Ionic 2地理位置无法在Android设备上运行

时间:2017-08-21 13:47:55

标签: android cordova typescript ionic2 geolocation

我正在努力让Ionic 2中的当前地理位置在Android设备上运行。在浏览器中它运行良好,但是当我运行ionic cordova run android命令在设备上部署时,地理位置根本不执行,我收到以下错误:

Angular 2 is running in the development mode. Call enableProdMode() to enable the production mode. main.js:48746 

Native: deviceready did not fire within 2000ms. This can happen when plugins are in an inconsistent state. Try removing plugins from plugins/ and reinstalling them. cordova.js:1223 (anonymous) @ main.js:48746

deviceready has not fired after 5 seconds. main.js:48741 

DEVICE READY FIRED AFTER 3656 ms main.js:119892 

Ionic Native: deviceready event fired after 3519 ms main.js:122839 

Ionic Storage driver: asyncStorage main.js:50230 

navigator.geolocation works well main.js:8291 

PlacesPage ionViewDidLoad error: this.getGeolocation is not a function

主要是,我不明白的是我得到this.getGeolocation is not a function,因为这是如何从浏览器更改为设备的?

import { Geolocation } from '@ionic-native/geolocation';
...
constructor(private geolocation: Geolocation) {}
...
ionViewDidLoad() {
    if(this.platform.is('cordova') === true){
        document.addEventListener("deviceready", onDeviceReady, false);
    }else{
        console.log('Browser geolocation')
        this.getGeolocation();
    }

    function onDeviceReady() {
        console.log("navigator.geolocation works well");
        this.getGeolocation();
    }
}

getGeolocation(){
    console.log('Starting Geolocation');

    var options = {
        enableHighAccuracy: true
    };

    this.geolocation.getCurrentPosition(options)
    .then((position) => {
        console.log('Geolocation successful');

        this.currentLocation = {
            lat: position.coords.latitude,
            lng: position.coords.longitude
        };

        let query = '?lat=' + position.coords.latitude + '&lng=' + position.coords.longitude;

        this.updatePlaces(query);

    }).catch((error) => {
        console.log('Error getting location', error);
    });
}

我尝试删除所有插件并重新安装它们。我已向index.html添加了Content-Security-Policy。

有人可以告诉我什么是错的或指导我朝正确的方向发展吗?感谢。

2 个答案:

答案 0 :(得分:0)

你的代码对我来说似乎不对。将您的代码更改为:

import { Geolocation } from '@ionic-native/geolocation';
...
constructor(private geolocation: Geolocation) {}
...
ionViewDidLoad() {
    if(this.platform.is('cordova') === true){
        document.addEventListener("deviceready", onDeviceReady, false);
    }else{
        console.log('Browser geolocation')
        this.getGeolocation();
    }
}

function onDeviceReady() {
        console.log("navigator.geolocation works well");
        this.getGeolocation();
    }

getGeolocation(){
    console.log('Starting Geolocation');

    var options = {
        enableHighAccuracy: true
    };

    this.geolocation.getCurrentPosition(options)
    .then((position) => {
        console.log('Geolocation successful');

        this.currentLocation = {
            lat: position.coords.latitude,
            lng: position.coords.longitude
        };

        let query = '?lat=' + position.coords.latitude + '&lng=' + position.coords.longitude;

        this.updatePlaces(query);

    }).catch((error) => {
        console.log('Error getting location', error);
    });
}

答案 1 :(得分:-1)

我似乎已经解决了这个问题。问题在于我一直在混合Cordova地理定位的实施和Ionic的实施。在Ionic中,不需要添加事件监听器(我想它处理引擎盖)as seen in the docs,所以正确的实现应该是这样的:

import { Geolocation } from '@ionic-native/geolocation';
...
constructor(private geolocation: Geolocation) {}
...
ionViewDidLoad() {
    this.getGeolocation();
}

getGeolocation(){
    console.log('Starting Geolocation');

    var options = {
        enableHighAccuracy: true
    };

    this.geolocation.getCurrentPosition(options)
    .then((position) => {
        console.log('Geolocation successful');

        this.currentLocation = {
            lat: position.coords.latitude,
            lng: position.coords.longitude
        };

        let query = '?lat=' + position.coords.latitude + '&lng=' + position.coords.longitude;

        this.updatePlaces(query);

    }).catch((error) => {
        console.log('Error getting location', error);
    });
}

在我实现事件监听器之前,我已经有了这个代码,但当时插件失败了,所以它试图修复它,它需要实现事件监听器。感谢@Prera​​k Tiwari让我思考正确的方向。