Geolocation Plugin not working - Code based off of docs example?

#1

Hi there, i am trying to use the Geolocation native plugin to grab the users location for use in our app. WHen running the code in the browser through ionic lab (see code below) the code works fine, however when running it on my iOS device it leads to the timeout being triggered instead of the actual code. No errors are flagging up when running the sudo ionic cordova prepare command which makes it difficult to debug. The code:

app.component.ts

import { Geolocation } from '@ionic-native/geolocation';
// code up until the constructor
constructor (public location: Geolocation; //and other vars) { }

initializeApp() {
  this.platform.ready().then(() => {
    //other native code
    this.location.getCurrentPosition({
      enableHighAccuracy: true,
      timeout: 30000,
      maximumAge: 0
    }).then((position) => {
      alert(position.coords.longitude);
      alert(position.coords.latitude);
   }).catch((error) => {
      alert(JSON.stringify(error));
    });
  });
}

I can’t work out why it isn’t working on the device. For some reason when i head into my device settings > APPNAME > there is no toggle switch for accessing location. I have checked my config.xml and have the valid iOS11 variable to request the permission to use the location of the device. However the device never asks for my position which may possibly be the problem?

Any help will be much appreciated.

#2

At this moment I just have an Android device to test, but this code works fine in it, it just takes more time to load because of the enableHighAccuracy: true.

Maybe your problem could be the permission to use Geolocation that is disabled.

#3

Hi @HugoPetia,

Thank you for your reply, i believe i know the issue but dont know how to solve it. XCode is flagging up with the message:

No NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key is defined in the Info.plist file.

I have therefore gone into the Config.xml of my application and added the following code to the

<platform name="ios">

section:

<edit-config file="*-Info.plist" mode="merge" target="NSLocationWhenInUseUsageDescription">
            <string>Required to send machine location.</string>
        </edit-config>
<edit-config file="*-Info.plist" mode="merge" target="NSLocationAlwaysUsageDescription">
            <string>Required to send machine location</string>
        </edit-config>

Is this the correct place to put it, as running the prepare command and putting back onto my device still causes the timeout issue and flags the same message as above.

#4

You device is an iOS 8? Take a look at this Core Location Manager Changes in iOS 8

1 Like
#5

Hi Hugo,

Thanks for your reply once again, i imagine the link you provided may be useful for further reference, however i solved the issue by adding the following code:

Config.xml

    <edit-config file="*-Info.plist" mode="merge" platform="ios" target="NSLocationWhenInUseUsageDescription">
        <string>Required to send machine location.</string>
    </edit-config>

Then running the following commands in the CLI:

sudo ionic cordova platform remove ios
sudo ionic cordova platform add ios

I assume this refreshed the XCode project to a greater extent than the prepare code does, which meant a successful injection of the key|string pairs into the plist file.

Thanks for your help.

  • Matt
5 Likes
#6

Saved my day ! worked for me like a charm.