Samsung & Ionic 3 Geolocation

Mobile Development   •   Feb 24, 2019

samsung ionic geolocation

Here at the Van Curen Group we are big fans of the Ionic Framework. It allows for rapid development and native like functionality that is almost indistinguishable (if done right). Recently, however, we ran into a problem with the Native Geolocation API on certain Samsung phones. After a recent Samsung update it stopped working for certain users that each had a new Samsung model phone. Sure enough, an issue popped up on Github.com in this plugin’s repository. With no solution in sight our team created a work around that ensured the function ran without breaking the app.

Note that this workaround required the user set it’s Android location settings to High Accuracy. Essentially, what we did was to ensure that we check that the location services are available. If they are, we want to make sure the mode is set to High Accuracy. If the user has not enabled location settings or High Accuracy is turned off, we do an internal alert with a prompt that asks the user if they would like to turn it on. If the user opts out, the function will not load but will also not break the app. They may not be able to use that particular service though until they agree and turn it on.

This will require a few prerequisites:

Step 1.

Edit the following file:

/platforms/android/project.properties

Find this line and make sure that the end part is changed to read :15.+ , see below.

cordova.system.library.6=com.google.android.gms:play-services-location:15.+

Step 2.

Edit the following file:

/platforms/android/app/build.gradle

Find this line and make sure that the end part is changed to read :15.+ like below

compile "com.google.android.gms:play-services-location:15.+"

Step 3.

Next create a provider and add the following code.

  • We’re first checking for Android settings. They are different for iOS and Windows so this will only work with Android.
  • Then we check if the location is enabled. If location is enabled we then check what location mode is set.
  • If location mode is High Accuracy the geolocation will work as expected.
  • If not we will prompt to enable high accuracy.
  • You can handle no location enabled errors or user rejecting high accuracy errors in the areas commented below.

If the user has opted in and the location permissions are set the function will run and grab the coordinates. You can now assign to a variable.


getLocation() {
    const HIGH_ACCURACY = 'high_accuracy'';
    if (this.platform.is('android')) {
      this.diagnostic.isLocationEnabled().then(enabled => {
        if (enabled) {
          this.diagnostic.getLocationMode().then(locationMode => {
            if (locationMode === HIGH_ACCURACY) {
              this.geolocation.getCurrentPosition().then((position) => {
                // load position variables
              });
            } else {
                this.promptHighAccuracy();
            }
          });
        } else {
          // handle if user rejects prompt to enable high accuracy location 
        }
      });
    } 
  }


promptHighAccuracy() {
 this.locationAccuracy
   .request(this.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY).then(() => {
     this.geolocation.getCurrentPosition().then((position) => {
       // populate my data
     });
   });
}

NEED HELP?