【问题标题】:Native IONIC Geolocation not working on Android devices.原生 IONIC 地理位置无法在 Android 设备上运行。
【发布时间】:2018-03-21 06:46:42
【问题描述】:

我将 Ionic 与原生 Geolocation 插件一起使用来检索用户位置并按最接近用户的方式对位置列表进行排序。 Geolocation 插件可以完美地使用ionic serveionic lab 以及iOS 设备,但它不能在Android 设备(或模拟器)上工作。

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

我将在此处附上我使用 Geolocation 插件的类。

我访问的 Location 类有一个公共静态变量,我在其中存储 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');
  }
}

【问题讨论】:

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


    【解决方案1】:

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

    另外,最好有 Success 和 Error 回调来识别实际的错误。如下所示:

            .......... 
            // 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);
              }
    .......
    .......
    

    【讨论】:

      猜你喜欢
      • 2018-01-29
      • 2015-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多