0

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. 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).

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.

The Location class I access has a public static variable where I store the userLocation since will be modified in more classes.

this.Location.load just uses the user position to call a method in Location class to sort the list of places.

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 Answers1

0

Prerequisite : Check whether you have switch ON your GPS service in Android.

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);
          }
.......
.......
Lokkesh
  • 550
  • 3
  • 11