문제

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

도움이 되었습니까?

해결책

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.

다른 팁

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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top