2017-10-10 129 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); 
      } 
....... 
....... 
相關問題