原生IONIC Geolocation无法在Android设备上运行。

时间:2017-10-10 00:30:50

标签: android ionic-framework geolocation geocoding ionic-native

我正在使用Ionic和本地 Geolocation 插件来检索用户位置并按最接近用户的位置列表进行排序。 Geolocation插件使用ionic serveionic lab以及iOS设备完美运行,但它不适用于Android设备(也不适用于模拟器)。

我可以使用哪种其他解决方案来检索用户的经度和纬度?

我会在这里附上我使用Geolocation插件的类。

我访问的位置类有一个公共静态变量,我存储userLocation,因为将在更多类中修改。

this.Location.load只是使用用户位置来调用Location类中的方法来对地点列表进行排序。

import { Component } from '@angular/core';
import { ModalController, NavController, NavParams } from 'ionic-angular';
import { SharePopup } from '../share-popup/share-popup';
import { InAppBrowser } from 'ionic-native';
import { CamhsPage } from '../camhs-page/camhs-page';

import { Locations } from '../../providers/locations';
import { Platform } from 'ionic-angular';
import { Geolocation } from 'ionic-native';

@Component({
  selector: 'contact',
  templateUrl: 'contact.html'
})
export class Contact {
  userPosition = [0, 0];


  constructor(public navCtrl: NavController, public navParams: NavParams,
   public modalCtrl: ModalController, public locations: Locations,public platform: Platform) {
  }

  openCamhsPage(){
    this.platform.ready().then(() => {
      let options = {
        timeout: 10000,
      enableHighAccuracy: true
      };
            Geolocation.getCurrentPosition(options).then((data) => {
                Locations.userPosition[0] = Math.round(data.coords.latitude * 100)/100;
                Locations.userPosition[1] = Math.round(data.coords.longitude * 100)/100;
                // console.log("CONTACT "+Locations.userPosition);
            });
        });
    this.locations.load();
    this.navCtrl.push(CamhsPage);
    console.log("CONTACT "+Locations.userPosition);
  }

  //Open WebPage
  openPage(url) {
    new InAppBrowser(url, '_system');
  }
}

1 个答案:

答案 0 :(得分:0)

先决条件:检查您是否在Android中启用了GPS服务。

还有成功和错误回调来识别实际错误。如下所示:

        .......... 
        // Success callback for get geo coordinates

        var onSuccess = function (data) {

            Locations.userPosition[0] = Math.round(data.coords.latitude * 100)/100;
            Locations.userPosition[1] = Math.round(data.coords.longitude * 100)/100;
        }

        var onError = function (data) {
            console.log("Not Able to get Geolocation");
        }

        openCamhsPage(){
            this.platform.ready().then(() => {
               Geolocation.getCurrentPosition(onSuccess, onError, { enableHighAccuracy: true });

            this.locations.load();
            this.navCtrl.push(CamhsPage);
            console.log("CONTACT "+Locations.userPosition);
          }
.......
.......