3
public static Country getCountryFromLocation(Context cxt, List<Country> countryList, Location location, int maxAddress) {
    if (location == null || countryList == null || countryList.isEmpty()) {
        Log.d("ooo", "location == null || countryList == null || countryList.isEmpty()");
        return null;
    }

    double longitude = location.getLongitude();
    double latitude = location.getLatitude();

    Geocoder coder = new Geocoder(cxt);
    try {
        List<Address> addressList = coder.getFromLocation(latitude, longitude, maxAddress);

        for (Address address : addressList) {
            String curCountryCode = address.getCountryCode();

            Log.d("ooo", "address " + address.toString());
            if (TextUtils.isEmpty(curCountryCode)) {
                continue;
            }

            for (Country country : countryList) {
                if (country.countryCode.equals(curCountryCode)) {
                    return country;
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

我正在尝试编写一个函数,该函数将采用经度和纬度来获取基于 ISO 3166-2 的国家代码。根据Address类上的方法描述:

/**
 * Returns the country code of the address, for example "US",
 * or null if it is unknown.
 */
public String getCountryCode() {
    return mCountryCode;
}

我应该根据经度和纬度得到 CN,但我得到的地址输出如下:

 D/ooo: address Address[addressLines=[0:"河南省许昌市鄢陵县",1:"刘桥村"],feature=null,admin=河南省,sub-admin=null,locality=许昌市,thoroughfare=,postalCode=null,countryCode=155,countryName=中国,hasLatitude=true,latitude=34.16667716

155是我得到的国家代码,请让我知道我应该怎么做才能从经度和纬度得到国家代码。另外,当我使用 GeoCoder 时,即使输入的经度和纬度在同一个国家,它也会返回不同的 3 位数字,在我的例子中,它是中国。155代表一个省,131代表另一个中国省份。你能解释一下这些数字代表什么吗?我确定不是国家代码...

4

5 回答 5

5

在 Android 中有很多方法可以获取国家代码。

1. 从区域设置配置。

String CountryID = getResources().getConfiguration().locale.getCountry();

它将返回基于用户在设备中配置的区域设置的国家代码。前任。我在印度,我的设备配置了区域设置English (United States),然后它将返回US

2. 从TelephonyManager

TelephonyManager manager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

String CountryID= manager.getSimCountryIso().toUpperCase();

3. 从纬度/经度使用地理编码

Geocoder geoCoder = new Geocoder(mActivity);
List<Address> addresses = geoCoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) {
       String CountryID= addresses.get(0).getCountryCode();
}

注意:所有方法都将返回 2 个字母的国家代码,例如 US (USA), IN(INDIA)..etc

从字母中获取数字的国家代码。

数组.xml

<string-array name="CountryCodes" >
        <item>93,AF</item>
        <item>355,AL</item>
        <item>213,DZ</item>
        <item>376,AD</item>
        <item>244,AO</item>
        <item>672,AQ</item>
        <item>54,AR</item>
        <item>374,AM</item>
        <item>297,AW</item>
        <item>61,AU</item>
        <item>43,AT</item>
        <item>994,AZ</item>
        <item>973,BH</item>
        <item>880,BD</item>
        <item>375,BY</item>
        <item>32,BE</item>
        <item>501,BZ</item>
        <item>229,BJ</item>
        <item>975,BT</item>
        <item>591,BO</item>
        <item>387,BA</item>
        <item>267,BW</item>
        <item>55,BR</item>
        <item>673,BN</item>
        <item>359,BG</item>
        <item>226,BF</item>
        <item>95,MM</item>
        <item>257,BI</item>
        <item>855,KH</item>
        <item>237,CM</item>
        <item>1,CA</item>
        <item>238,CV</item>
        <item>236,CF</item>
        <item>235,TD</item>
        <item>56,CL</item>
        <item>86,CN</item>
        <item>61,CX</item>
        <item>61,CC</item>
        <item>57,CO</item>
        <item>269,KM</item>
        <item>242,CG</item>
        <item>243,CD</item>
        <item>682,CK</item>
        <item>506,CR</item>
        <item>385,HR</item>
        <item>53,CU</item>
        <item>357,CY</item>
        <item>420,CZ</item>
        <item>45,DK</item>
        <item>253,DJ</item>
        <item>670,TL</item>
        <item>593,EC</item>
        <item>20,EG</item>
        <item>503,SV</item>
        <item>240,GQ</item>
        <item>291,ER</item>
        <item>372,EE</item>
        <item>251,ET</item>
        <item>500,FK</item>
        <item>298,FO</item>
        <item>679,FJ</item>
        <item>358,FI</item>
        <item>33,FR</item>
        <item>689,PF</item>
        <item>241,GA</item>
        <item>220,GM</item>
        <item>995,GE</item>
        <item>49,DE</item>
        <item>233,GH</item>
        <item>350,GI</item>
        <item>30,GR</item>
        <item>299,GL</item>
        <item>502,GT</item>
        <item>224,GN</item>
        <item>245,GW</item>
        <item>592,GY</item>
        <item>509,HT</item>
        <item>504,HN</item>
        <item>852,HK</item>
        <item>36,HU</item>
        <item>91,IN</item>
        <item>62,ID</item>
        <item>98,IR</item>
        <item>964,IQ</item>
        <item>353,IE</item>
        <item>44,IM</item>
        <item>972,IL</item>
        <item>39,IT</item>
        <item>225,CI</item>
        <item>81,JP</item>
        <item>962,JO</item>
        <item>7,KZ</item>
        <item>254,KE</item>
        <item>686,KI</item>
        <item>965,KW</item>
        <item>996,KG</item>
        <item>856,LA</item>
        <item>371,LV</item>
        <item>961,LB</item>
        <item>266,LS</item>
        <item>231,LR</item>
        <item>218,LY</item>
        <item>423,LI</item>
        <item>370,LT</item>
        <item>352,LU</item>
        <item>853,MO</item>
        <item>389,MK</item>
        <item>261,MG</item>
        <item>265,MW</item>
        <item>60,MY</item>
        <item>960,MV</item>
        <item>223,ML</item>
        <item>356,MT</item>
        <item>692,MH</item>
        <item>222,MR</item>
        <item>230,MU</item>
        <item>262,YT</item>
        <item>52,MX</item>
        <item>691,FM</item>
        <item>373,MD</item>
        <item>377,MC</item>
        <item>976,MN</item>
        <item>382,ME</item>
        <item>212,MA</item>
        <item>258,MZ</item>
        <item>264,NA</item>
        <item>674,NR</item>
        <item>977,NP</item>
        <item>31,NL</item>
        <item>599,AN</item>
        <item>687,NC</item>
        <item>64,NZ</item>
        <item>505,NI</item>
        <item>227,NE</item>
        <item>234,NG</item>
        <item>683,NU</item>
        <item>850,KP</item>
        <item>47,NO</item>
        <item>968,OM</item>
        <item>92,PK</item>
        <item>680,PW</item>
        <item>507,PA</item>
        <item>675,PG</item>
        <item>595,PY</item>
        <item>51,PE</item>
        <item>63,PH</item>
        <item>870,PN</item>
        <item>48,PL</item>
        <item>351,PT</item>
        <item>1,PR</item>
        <item>974,QA</item>
        <item>40,RO</item>
        <item>7,RU</item>
        <item>250,RW</item>
        <item>590,BL</item>
        <item>685,WS</item>
        <item>378,SM</item>
        <item>239,ST</item>
        <item>966,SA</item>
        <item>221,SN</item>
        <item>381,RS</item>
        <item>248,SC</item>
        <item>232,SL</item>
        <item>65,SG</item>
        <item>421,SK</item>
        <item>386,SI</item>
        <item>677,SB</item>
        <item>252,SO</item>
        <item>27,ZA</item>
        <item>82,KR</item>
        <item>34,ES</item>
        <item>94,LK</item>
        <item>290,SH</item>
        <item>508,PM</item>
        <item>249,SD</item>
        <item>597,SR</item>
        <item>268,SZ</item>
        <item>46,SE</item>
        <item>41,CH</item>
        <item>963,SY</item>
        <item>886,TW</item>
        <item>992,TJ</item>
        <item>255,TZ</item>
        <item>66,TH</item>
        <item>228,TG</item>
        <item>690,TK</item>
        <item>676,TO</item>
        <item>216,TN</item>
        <item>90,TR</item>
        <item>993,TM</item>
        <item>688,TV</item>
        <item>971,AE</item>
        <item>256,UG</item>
        <item>44,GB</item>
        <item>380,UA</item>
        <item>598,UY</item>
        <item>1,US</item>
        <item>998,UZ</item>
        <item>678,VU</item>
        <item>39,VA</item>
        <item>58,VE</item>
        <item>84,VN</item>
        <item>681,WF</item>
        <item>967,YE</item>
        <item>260,ZM</item>
        <item>263,ZW</item>
    </string-array>

如何使用这个数组。

String CountryZipCode="";

String[] rl= getResources().getStringArray(R.array.CountryCodes);
    for(int i=0;i<rl.length;i++){
        String[] g=rl[i].split(",");
        if(g[1].trim().equals(CountryID.trim())){
            CountryZipCode=g[0];
            break;
        }
    }

注意:您还可以修改上述代码以从数字中获取国家/地区代码。

于 2016-06-02T06:31:22.797 回答
4

试试这个

String locale = context.getResources().getConfiguration().locale.getCountry();

另一种选择是使用此线程中指定的 TelephonyManager 类。感谢@Rawkode。

于 2016-05-30T13:56:49.957 回答
3

您可以使用 Geocoding API Web 服务作为原生 Geocoder 类的替代方案。Web 服务具有反向地理编码功能:

https://developers.google.com/maps/documentation/geocoding/intro?hl=en#ReverseGeocoding

因此,您可以指定要返回的纬度、经度和结果类型(国家/地区)。

在您的情况下,它应该类似于:

https://maps.googleapis.com/maps/api/geocode/json?latlng=34.16667716%2C114.1774&result_type=country&key=YOUR_API_KEY

在 Android 中,您可以使用 AndroidHttpClient 类来执行 HTTP 请求:

https://developer.android.com/reference/android/net/http/AndroidHttpClient.html

上述 HTTP 请求的 JSON 结果是

{
  "results":[
    {
      "address_components":[
        {
          "long_name":"China",
          "short_name":"CN",
          "types":[
            "country","political"
          ]
        }
      ],
      "formatted_address":"China",
      "geometry":{
        "bounds":{
          "northeast":{
            "lat":53.5587016,"lng":134.7728099
          },
          "southwest":{
            "lat":18.1576156,"lng":73.4994136
          }
        },
        "location":{
          "lat":35.86166,"lng":104.195397
        },
        "location_type":"APPROXIMATE",
        "viewport":{
          "northeast":{
            "lat":53.558106,"lng":134.7728099
          },
          "southwest":{
            "lat":18.1576156,"lng":73.4994136
          }
        }
      },
      "place_id":"ChIJwULG5WSOUDERbzafNHyqHZU",
      "types":[
        "country","political"
      ]
    }
  ],
  "status":"OK"
}

因此,您可以从地址组件短名称字段中获取 ISO 3166-2 代码。

于 2016-06-04T20:33:54.230 回答
1
public String getCountryCode() {
    return mCountryCode;
}

Basically, this method is responsible for returning country ISO Alpha/Numerical code.

For example: In the case of China it will return 86 which is actually a dial prefix or ISO numerical code.

however, the document says...

/**
 * Returns the country code of the address, for example "US",
 * or null if it is unknown.
 */

Here:

"US" = ISO Alpha-2 code ,
"CHN" = ISO Alpha-3 code ,
155 = ISO Numerical code/dial prefix

Country ISO code  Or Country Dial code/prefix = country code // this conflicts us

However, mCountryCode is responsible to return both ISO Alpha & Numerical code based on your function used from the Address class.

Now, according to your debug message:

 D/ooo: address Address[addressLines=[0:"河南省许昌市鄢陵县",1:"刘桥村"],feature=null,admin=河南省,sub-admin=null,locality=许昌市,thoroughfare=,postalCode=null,countryCode=155,countryName=中国,hasLatitude=true,latitude=34.16667716

You got countryCode=155 which is actually a mobile phone area-code prefix in China & it will change when you change the location. That's why you got 155 & 131 for different provinces. See the Wiki

If you expect ISO code like "US" or "CHN" as string output, you may use this line:

address.getLocale().getISO3Country();

In the case of numerical output, you will get different codes which are just dial prefix based on area.

Reference: country code list

于 2016-06-07T07:11:07.383 回答
0

字符串语言环境 = context.getResources().getConfiguration().locale.getCountry();

于 2016-06-02T11:45:09.897 回答