Ionic Geolocation plugin working on browser but not iOS and Android Emulators

Hello everybody,

I’m stuck on an issue I’ve come across and am unsure if it’s related to my code or if it’s related to the Geolocation plugin itself.

Basically, I am able to get the user’s current location through my web browser (Google Chrome), but the issue is that the same code does not work on either the iOS Simulator and the Genymotion (Android) emulator. Meaning, the code is unable to get the user’s location, and instead I’m greeted with the following error in the Safari console:

Here’s the code that I am using to get the user’s location. Basically, I’m getting the user’s location as soon as the view for this code loads:

image

Would anyone be able to tell me what I might be doing wrong? Any help is greatly appreciated!

Did you install the cordova-plugin-geolocation plugin? This looks like a permission issue, and installing that plugin will add the proper permissions

Hi @max! Yes I have, here’s the console output when I type $ ionic plugin add cordova-plugin-geolocation:

Plugin "cordova-plugin-geolocation" already installed on android.

Plugin "cordova-plugin-geolocation" already installed on ios.

Do I need to configure the plugin on my config.xml file maybe?

On the android side, do you see these lines in your platforms/android/AndroidManifest.xml?

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Yes, I do:

image

Should I have something similar for my iOS config? If so, let me know what I need and I can check there, that way it’ll be easier to debug (since I’ve been using the iOS Simulator to debug this)

Weird, can you share what’s in your UserData class?

Also, try enabling high accuracy by passing enableHighAccuracy: true to the options param for the geolocation plugin

Sorry Max, I showed you the wrong piece of code (though the code I showed you is still relevant to me trying to get the cached user location that I would get from app.ts).

Please have a look at this code that I have in my app.ts file; basically I use the Geolocation in the constructor to ask the user for permission once the app starts up. The error occurs as soon as the user presses “OK” on the Geolocation’s Alert popup.

image

My UserData class is a bit long, but I will type the relevant code here:

import { Injectable }          from '@angular/core';
import { Http }                from '@angular/http';
import { Storage, SqlStorage } from 'ionic-angular';
import 'rxjs/add/operator/map';

@Injectable()
export class UserData {

  private storage = new Storage(SqlStorage);

  constructor() {}
  
  setLatitude(latitude: number) {
    this.storage.set('latitude', latitude);
  }

  getLatitude() {
    return this.storage.get('latitude').then((value) => {
      return value;
    });
  }

  setLongitude(longitude: number) {
    this.storage.set('longitude', longitude);
  }

  getLongitude() {
    return this.storage.get('longitude').then((value) => {
      return value;
    });
  }

I hope this helps more with figuring out the issue! Let me know if you need more information!

Did you try putting the Geolocation call in the platform.ready function?

Thanks for that suggestion, I tried it but same error :frowning:

Here’s my package.json file in case it’s helpful. Maybe I need to upgrade something? I noticed that cordova-plugin-gelocation isn’t under the cordovaPlugins key, but even when I specified that and ran $ ionic emulate ios again, the same error occurred, so I took it out.

I think the issue is that you are accessing geolocation before the device is ready. The browser doesn’t run into the timing issue, but on a real or emulated device you are making the call to the plugin before Cordova is ready.

Move the getUserLocation call within the platform.ready block and see if that resolves your issue

Chris

3 Likes

Thank you so much Chris, that makes perfect sense!

I’ll keep trying to find a solution based on that knowledge (maybe when I tried it earlier and saw that same error, I was doing it incorrectly); though for anyone else that might have more suggestions or more wisdom to share, please feel free! In the mean time I’ll update you guys on whether or not these suggestions work :slight_smile:

It says in the error that the code is 2 for POSITION_UNAVAILABLE. But I don’t know why because I have Custom Location enabled on my iOS Simulator. Hope this helps anyone with potential suggestions on how I can address the issue I’ve come across.

Here is the temporary solution,just put sdk version to 22 in your platforms/android/AndroidManifest.xml? :slight_smile:

 <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="22" />

I have the exact same issue. I tried to set android:targetSdkVersion=“22”, but problem remains. Any new suggestions?

I haven’t tested location on an Emulator recently so I don’t know if it’s still a problem for me, but I was able to get around this by testing location-related features on an actual iOS device… (meaning to say, it will work on devices but for whatever reason it didn’t work for me in the past on the emulator)

1 Like

Hi @jourdan
I got the same issue and I realize that I forgot turn on the GPS on the device.
I think you can try to turn on the GPS and re-check.

Same issue i am facing. But it is working in android 6 and not working in android 5.1.1

Hi all,
I have the same problem with a very simple app with a button that shows an alert with the local coordinates, using Geolocation.getCurrentPosition().
It works with Chrome on my pc (with ionic serve), but doesn’t work with Ionic View and if I publish the app as PWA.

I’ve installed the plugin in this way:
ionic plugin add cordova-plugin-geolocation
and imported it in the page in this way:
import { Geolocation } from 'ionic-native';

I think that it is strange that in the file “package.json” there is no reference to the geolocation plugin.
Could be this the problem?
Has someone solved the problem?

I’ve:
Cordova CLI: 6.4.0
Ionic Framework Version: 2.0.0
Ionic CLI Version: 2.1.18
Ionic App Lib Version: 2.1.9
Ionic App Scripts Version: 1.0.0
ios-deploy version: Not installed
ios-sim version: Not installed
OS: Windows 10
Node Version: v6.9.2
Xcode version: Not installed

I am facing same issue… is there any solution to this??

I had the same problem. Solved like this:

ionic plugin add cordova-plugin-geolocation

then:

npm install --save @ionic-native/geolocation

Imported it like that

import { Geolocation } from '@ionic-native/geolocation';

And set this options

    var options = {
      enableHighAccuracy: true,
      timeout: 5000,
      maximumAge: 0
    };

then you call the options inside the getCurrentPosition(options). And when you finally go to test it on a real device don’t forget to turn the gps on.

3 Likes