Cordova app: how to find a user's country

I am creating a cordova app for android platform.

I need to get a custom country.

I know geolocation gives GPS coordinates.

Is there a way to have a country without using any external API? if this is not possible, what is the best solution?

Thanks everyone for your help.

+3


source to share


4 answers


I can think of several options. The list is below and it is ordered with minimal effort, and on the other hand, how accurate the information will be.

  • Use the custom locale string provided by the globalization plugin , can contain the country code next to the language code, such as "en-US" or "en-GB".

Using

function successCallback(obj) {
    var locale = obj.value;
}
navigator.globalization.getPreferredLanguage(successCallback, errorCallback);

      

  1. For Android only . Use a plugin such as the Device Information Plugin that allows you to access information about your Android Telephony phone . From this information you can get


Telephone network provider ISO country

according to the plugin author. To use the plugin, your code will look something like this.

deviceInfo.get(function(result) {
    console.log("result = " + result); 
}, function() {
    console.log("error");
});

      

where the result will contain your netCountry field as part of the returned string.

  1. Convert GPS to country by yourself based on country borders like GPS. There is one possible map (dataset) available [here] (World Borders Dataset). I would recommend just using some external API to provide you with this information.
+2


source


There is important difference from UX and legal point of view in which country you need:

  • Country of current location : use the geolocation API and any mapping solution. This is the only reliable way of locating the country of your location, as IP or SIM detection can only identify the country of the telecommunications service provider. Please note that the country of your current location is not your country of citizenship and does not allow for custom language or regional preferences.
  • Country as context for localization : Use the ECMAScript Internationalization API [1] and the cordova-globalization-plugin [2] as a fallback. Please note that these APIs do not provide location information at all: the language and language information are not the same as the user's country (for example, the user can select French and en_US while living in Monaco and being a Russian citizen).
  • Country of citizenship . In some cases, this is important information for your service (for example, when the personal data of the user collected by the client-server system must be physically processed in the country of citizenship, as required by law). The only way to determine this is to ask the user and demand an honest answer to your Terms of Service. Please note that the country of citizenship does not determine the localization settings or the user's current location.


[1] http://www.ecma-international.org/ecma-402/1.0/

[2] https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-globalization/

+2


source


If your app is for a phone or sim device only, you can use the sim info plugin, https://github.com/dtmtec/cordova-plugin-carrier .

+1


source


Easy solution is to create custom plugin and use google geocoder, this code will return location from latitude and longitude ... This is not for cordova, you need to make some changes

try {
   Geocoder gcd = new Geocoder(this, Locale.getDefault());
   List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1);
         if (addresses.size() > 0)
         String cityname = addresses.get(0).getLocality();
         String country = addresses.get(0).getCountryName();
} catch (IOException e) {
     e.printStackTrace();
}

      

-1


source







All Articles