CodeChimpy
CodeChimpy

Reputation: 560

Ionic hybrid mobile app get device location

We are developing a mobile app with Ionic/Cordova and Angular 5. We have to display a distance either in metric or imperial system depending on the locale of the device settings. At that point of the app we already have a value in meters which we have to transform accordingly.

We don't want to do another network call so the locale must be extracted from the device.

Is there a build-in way with our tech stack to do this or is there a plugin for extracting device settings?

We use:

I found this https://angular.io/guide/i18n guide and we would prefer to use the LOCALE_ID but this is not a must.

Upvotes: 1

Views: 1566

Answers (3)

CodeChimpy
CodeChimpy

Reputation: 560

We found a solution which works on mobile devices without doing another Network call. The first line reads the locale from the device settings, the second line extracts the country of the locale.

Unfortunately, this is not reliable in browsers but good enough for our mobile only use case.

const locale = window.navigator.language;
const country = locale.split('-')[1];

Upvotes: 2

user9102407
user9102407

Reputation:

   if ("geolocation" in navigator) {
      navigator.geolocation.getCurrentPosition((position) => {
        //this.latitude = position.coords.latitude;
        //this.longitude = position.coords.longitude;
      });
    }

Upvotes: 1

user4676340
user4676340

Reputation:

Use the new navigator object (you are creating an hybrid app, I assume you're up-do-date with the versions of your browsers)

let lang = navigator.language || navigator.userLanguage; // Language
let showPosition = (position) => {
  position.coords.latitude;
  position.coords.longitude; 
}
navigator.geolocation.getCurrentPosition(showPosition); // Geoposition

Upvotes: 1

Related Questions