Android Geocoder 获取管理区域的简称

Posted

技术标签:

【中文标题】Android Geocoder 获取管理区域的简称【英文标题】:Android Geocoder get short name of admin area 【发布时间】:2015-07-06 16:15:34 【问题描述】:

使用 google maps API 时,对于管理区域,它会为您提供长名称和短名称,例如。


    "long_name": "Victoria",
    "short_name": "VIC",
    "types": [
        "administrative_area_level_1",
        "political"
    ]

但是,当使用 android 的本地地理编码器时,我只得到长名称(在本例中为“Victoria”)。除了集成谷歌地图地理编码API之外,有没有办法在android中获取管理区域的简称?

【问题讨论】:

任何人都可以有解决方案然后请发布它.. @Jatin 如果你们都没有找到答案,我刚刚发布了一些希望对您有所帮助的内容 【参考方案1】:

我要保持简短,我的位置实现有点不同。因为我花了几个小时才做到这一点。

首先我制作了一个地图列表来查找州名,然后将其转换为 2 字母代码,如下所示:

    Map<String, String> states = new HashMap<>();
    states.put("Alabama","AL");
    states.put("Alaska","AK");
    states.put("Alberta","AB");
    states.put("American Samoa","AS");
    states.put("Arizona","AZ");
    states.put("Arkansas","AR");
    states.put("Armed Forces (AE)","AE");
    states.put("Armed Forces Americas","AA");
    states.put("Armed Forces Pacific","AP");
    states.put("British Columbia","BC");
    states.put("California","CA");
    states.put("Colorado","CO");
    states.put("Connecticut","CT");
    states.put("Delaware","DE");
    states.put("District Of Columbia","DC");
    states.put("Florida","FL");
    states.put("Georgia","GA");
    states.put("Guam","GU");
    states.put("Hawaii","HI");
    states.put("Idaho","ID");
    states.put("Illinois","IL");
    states.put("Indiana","IN");
    states.put("Iowa","IA");
    states.put("Kansas","KS");
    states.put("Kentucky","KY");
    states.put("Louisiana","LA");
    states.put("Maine","ME");
    states.put("Manitoba","MB");
    states.put("Maryland","MD");
    states.put("Massachusetts","MA");
    states.put("Michigan","MI");
    states.put("Minnesota","MN");
    states.put("Mississippi","MS");
    states.put("Missouri","MO");
    states.put("Montana","MT");
    states.put("Nebraska","NE");
    states.put("Nevada","NV");
    states.put("New Brunswick","NB");
    states.put("New Hampshire","NH");
    states.put("New Jersey","NJ");
    states.put("New Mexico","NM");
    states.put("New York","NY");
    states.put("Newfoundland","NF");
    states.put("North Carolina","NC");
    states.put("North Dakota","ND");
    states.put("Northwest Territories","NT");
    states.put("Nova Scotia","NS");
    states.put("Nunavut","NU");
    states.put("Ohio","OH");
    states.put("Oklahoma","OK");
    states.put("Ontario","ON");
    states.put("Oregon","OR");
    states.put("Pennsylvania","PA");
    states.put("Prince Edward Island","PE");
    states.put("Puerto Rico","PR");
    states.put("Quebec","PQ");
    states.put("Rhode Island","RI");
    states.put("Saskatchewan","SK");
    states.put("South Carolina","SC");
    states.put("South Dakota","SD");
    states.put("Tennessee","TN");
    states.put("Texas","TX");
    states.put("Utah","UT");
    states.put("Vermont","VT");
    states.put("Virgin Islands","VI");
    states.put("Virginia","VA");
    states.put("Washington","WA");
    states.put("West Virginia","WV");
    states.put("Wisconsin","WI");
    states.put("Wyoming","WY");
    states.put("Yukon Territory","YT");

然后我在做了所有的恶作剧后将它附加到位置字符串结果中以获得类似的地址

    Address address = addressList.get(0);
    String state = states.get(address.getAdminArea());
    result = address.getLocality() + ", " + state;

我希望你在我之前得到了这个,但我只是决定把它提供给其他需要它的人。当我修复它时,我碰巧在这个页面上。

注意:地图(不是真正的位置图)列表包含加拿大“州”和其他一些使用缩写的地方。

【讨论】:

您必须自己寻找其中一些,我只为北美这样做。我敢打赌,有些数据库可以做到这一点。 这是一个非常漫长而漫长的过程。所以我找到了解决方案。它适用于任何国家和任何州。【参考方案2】:

经过一番搜索,我找到了一个解决方案

String url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + "," + longitude + "&amp;sensor=true";

RequestQueue mVolleyQueue = Volley.newRequestQueue(this);
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() 
        @Override
        public void onResponse(String response) 
            try 
                Log.i("response ", response);
                JSONObject jsonObject = new JSONObject(response);
                JSONArray arrayResult = jsonObject.getJSONArray("results");

                JSONArray arrComponent = arrayResult.getJSONObject(0).getJSONArray("address_components");
                for (int i = 0; i < arrComponent.length(); i++) 
                    JSONArray arrType = arrComponent.getJSONObject(i).getJSONArray("types");

                    for (int j = 0; j < arrType.length(); j++) 

                        if (arrType.getString(j).equals("administrative_area_level_1")) 
                            longname = arrComponent.getJSONObject(i).getString("long_name");
                            shortname = arrComponent.getJSONObject(i).getString("short_name");
                            Log.i("longname", longname);
                            Log.i("shortname", shortname);
                        
                    
                

             catch (JSONException e) 
                e.printStackTrace();
            
        
    , new Response.ErrorListener() 
        @Override
        public void onErrorResponse(VolleyError error) 
            error.printStackTrace();
        
    );
    stringRequest.setShouldCache(false);
    stringRequest.setTag("Volley");
    mVolleyQueue.add(stringRequest);

编码愉快:)

【讨论】:

以上是关于Android Geocoder 获取管理区域的简称的主要内容,如果未能解决你的问题,请参考以下文章

Android在服务中使用Geocoder获取位置地址

在 GeoCoder 地址中获取 maxAddressLineIndex 的大小 0

服务不可用-Geocoder Android [重复]

Android Geocoder getFromLocationName 始终返回 null

使用json而不是Geocoder从谷歌地图获取位置地址

geocoder.getFromLocationName 仅返回 null