Example usage for android.location Address getAdminArea

List of usage examples for android.location Address getAdminArea

Introduction

In this page you can find the example usage for android.location Address getAdminArea.

Prototype

public String getAdminArea() 

Source Link

Document

Returns the administrative area name of the address, for example, "CA", or null if it is unknown

Usage

From source file:com.cecs492a_group4.sp.SingleEvent.java

public String reverseGeocode(double latitude, double longitude) throws IOException {
    Geocoder gc = new Geocoder(this);

    if (gc.isPresent()) {
        List<Address> list = gc.getFromLocation(latitude, longitude, 1);

        //(latitude, longitude, 1)
        //33.777043, -118.114395, 1)

        Address address = list.get(0);

        StringBuffer str = new StringBuffer();

        if (address.getAddressLine(0) != null && address.getLocality() != null && address.getAdminArea() != null
                && address.getPostalCode() != null && address.getCountryName() != null) {
            //str.append(address.getAddressLine(0) + ", ");
            //str.append(address.getLocality() + ", ");
            //str.append(address.getAdminArea() + " ");
            //str.append(address.getPostalCode() + ", ");
            //str.append(address.getCountryName());
            //str.append("USA");

            //String strAddress = str.toString();

            String strAddress = (address.getAddressLine(0) + ", " + address.getLocality() + ", "
                    + address.getAdminArea() + " " + address.getPostalCode() + ", " + "USA");

            return strAddress;
        } else {/*from w w  w  .  j a v  a2 s.com*/
            return null;
        }
    }

    return null;
}

From source file:grandroid.geo.Geocoder.java

public static List<Address> getFromLocation(Locale locale, double lat, double lng, int maxResult) {
    String language = locale.getLanguage();
    if (locale == Locale.TAIWAN) {
        language = "zh-TW";
    }//w  w w  .j  a  v a 2  s .c  om
    String address = String.format(locale,
            "http://maps.googleapis.com/maps/api/geocode/json?latlng=%1$f,%2$f&sensor=false&language="
                    + language,
            lat, lng);//locale.getCountry()
    HttpGet httpGet = new HttpGet(address);
    HttpClient client = new DefaultHttpClient();
    client.getParams().setParameter(AllClientPNames.USER_AGENT,
            "Mozilla/5.0 (Java) Gecko/20081007 java-geocoder");
    //client.getParams().setIntParameter(AllClientPNames.CONNECTION_TIMEOUT, 5 * 1000);
    //client.getParams().setIntParameter(AllClientPNames.SO_TIMEOUT, 25 * 1000);
    HttpResponse response;

    List<Address> retList = null;

    try {
        response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        String json = EntityUtils.toString(entity, "UTF-8");

        JSONObject jsonObject = new JSONObject(json);

        retList = new ArrayList<Address>();

        if ("OK".equalsIgnoreCase(jsonObject.getString("status"))) {
            JSONArray results = jsonObject.getJSONArray("results");
            if (results.length() > 0) {
                for (int i = 0; i < results.length() && i < maxResult; i++) {
                    JSONObject result = results.getJSONObject(i);
                    //Log.e(MyGeocoder.class.getName(), result.toString());
                    Address addr = new Address(Locale.getDefault());
                    // addr.setAddressLine(0, result.getString("formatted_address"));

                    JSONArray components = result.getJSONArray("address_components");
                    String streetNumber = "";
                    String route = "";
                    for (int a = 0; a < components.length(); a++) {
                        JSONObject component = components.getJSONObject(a);
                        JSONArray types = component.getJSONArray("types");
                        for (int j = 0; j < types.length(); j++) {
                            String type = types.getString(j);
                            if (type.equals("locality") || type.equals("administrative_area_level_3")) {
                                addr.setLocality(component.getString("long_name"));
                            } else if (type.equals("street_number")) {
                                streetNumber = component.getString("long_name");
                            } else if (type.equals("route")) {
                                route = component.getString("long_name");
                            } else if (type.equals("administrative_area_level_1")) {
                                addr.setAdminArea(component.getString("long_name"));
                            } else if (type.equals("country")) {
                                addr.setCountryName(component.getString("long_name"));
                                addr.setCountryCode(component.getString("short_name"));
                            }
                        }
                    }
                    addr.setAddressLine(0, route + " " + streetNumber);
                    if (result.has("formatted_address")) {
                        addr.setFeatureName(result.getString("formatted_address"));
                    }
                    addr.setLatitude(
                            result.getJSONObject("geometry").getJSONObject("location").getDouble("lat"));
                    addr.setLongitude(
                            result.getJSONObject("geometry").getJSONObject("location").getDouble("lng"));
                    if (addr.getAdminArea() == null) {
                        addr.setAdminArea("");
                    }
                    retList.add(addr);
                }
            }
        }
    } catch (ClientProtocolException e) {
        Log.e("grandroid", "Error calling Google geocode webservice.", e);
    } catch (IOException e) {
        Log.e("grandroid", "Error calling Google geocode webservice.", e);
    } catch (JSONException e) {
        Log.e("grandroid", "Error parsing Google geocode webservice response.", e);
    }
    return retList;
}