简体   繁体   English

本机IONIC地理位置不适用于Android设备。

[英]Native IONIC Geolocation not working on Android devices.

I am using Ionic together with the native Geolocation plugin to retrieve user position and sort a list of position by closest to the user. 我将Ionic与本机Geolocation插件一起使用,以检索用户位置并按距离用户最近的位置对位置列表进行排序。 The Geolocation plugin works perfectly using ionic serve or ionic lab as well as iOS devices but it does not work on Android devices (nor simulator). Geolocation插件可与ionic serveionic lab以及iOS设备完美配合使用,但不适用于Android设备(也不适用于模拟器)。

What other solution can I use to retrieve longitude and latitude of the user? 我还可以使用什么其他解决方案来检索用户的经度和纬度?

I'll attach the class where I use the Geolocation plugin here. 我将在此处使用Geolocation插件的地方附加该类。

The Location class I access has a public static variable where I store the userLocation since will be modified in more classes. 我访问的Location类有一个公共静态变量,我在其中存储userLocation,因为它将在更多类中进行修改。

this.Location.load just uses the user position to call a method in Location class to sort the list of places. this.Location.load仅使用用户位置来调用Location类中的方法以对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');
  }
}

Prerequisite : Check whether you have switch ON your GPS service in Android. 先决条件:检查您是否已在Android中打开GPS服务。

Also it is good to have Success and Error Callbacks to identify the actual Error. 具有成功和错误回调以识别实际错误也是很好的。 Something like below : 如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM