Android Geocoder get short name of admin area
Asked Answered
S

2

5

When using the google maps API, for the admin area it gives you the long name and short name eg.

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

however when using android's native geocoder, I am only getting the long name (in this case "Victoria"). Is there a way to get the short name of an admin area in android, short of integrating the google maps geocoding API?

Synthesis answered 28/4, 2015 at 1:37 Comment(2)
Can any one has solution then please post it..Probative
@Probative If you both haven't found an answer, I just posted something I hope will helpCuspidation
C
6

I'm gonna keep this short and simple, I have done my location implementation a little different. Since it took me a couple of hours to do this.

First I made a Map List to find the state name then convert it to the 2 letter code like so:

    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");

Then I appended it into the Location String result after doing all the hocus pocus to get the address like

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

I hope you got this before I did, but I just decided to put this up for anyone else that needs it. I happened to be on this page when I fixed it.

NOTE: The Map(not really a Location Map) List has Canada "states" and some other places that use abbreviations.

Cuspidation answered 4/7, 2015 at 6:2 Comment(2)
You would have to look for some of them yourself, I only did this for North America. I bet there are some databases that would be able to do it.Cuspidation
It is very long and lengthy process . So i found solution for it . It will work for any country and any state.Sigmund
S
2

After some searching , I found one solution for that

String url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + "," + longitude + "&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);

Happy Codding :)

Sigmund answered 6/7, 2016 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.