问题
what is the best and fastest way to get the country iso code by starting an App build with phonegap? There is a function to get the geo coordinates, but not the Iso code. For getting the iso code maybe it is necessary to call an external webservice and get the iso by given coordinates. Is there a simplest way?
Thanks Nik
回答1:
Are you looking for where the user is at the moment or for something more like the users language/nationallity? For the latter you could always look at what's in navigator.language
. Depending on target platform it'll give you either just language code ('en' on my Android test machine) or language code and country code ('en-us' on my iOS test machine). To see what it will give you, go with your device's stock browser here and type in navigator.language
in the text input box.
Otherwise, casual browsing of the HTML5 Geolocation spec seems to indicate that you'll either need a web-service or package a datafile with codes in the app. This seems to be a pretty good one.
回答2:
I found navigator.language sometimes only gave me the language and not the locale (eg "en" rather than "en-gb"). To solve this in android I created a new java class in src/com called DeviceCulture.java with this code:
package com;
import java.util.Locale;
public class DeviceCulture {
public String get() {
try{
return Locale.getDefault().toString();//get the locale from the Android Device
}catch(Exception e){
return "";
}
}
}
Then add this before the super.loadUrl line in /src/com/phonegap/YOUR_APP/App.java
DeviceCulture deviceCulture = new DeviceCulture();
appView.addJavascriptInterface(deviceCulture , "DeviceCulture");
Then in your javascript use this to get the locale (eg "en_GB")
window.DeviceCulture.get();
来源:https://stackoverflow.com/questions/4578347/phonegap-the-best-way-to-discover-the-country-iso-code-from-a-user