2017-10-10 133 views
0

我使用Ionic与原生地理定位插件来检索用户位置并按距离用户最近的顺序对列表进行排序。 地理位置插件可以完美地使用ionic serveionic lab以及iOS设备,但它不适用于Android设备(也不适用于模拟器)。本机IONIC地理定位不适用于Android设备。

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

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

位置类我访问了那里我存储用户位置,因为将在多个类修改的公共静态变量。

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'); 
    } 
} 

回答

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); 
      } 
....... 
....... 
相关问题