Example usage for android.location Address setAdminArea

List of usage examples for android.location Address setAdminArea

Introduction

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

Prototype

public void setAdminArea(String adminArea) 

Source Link

Document

Sets the administrative area name of the address to the given String, which may be null

Usage

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";
    }/*from w w  w .  j a  va2s .  com*/
    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;
}

From source file:org.microg.networklocation.backends.mapquest.NominatimGeocodeSource.java

private Address parseResponse(Locale locale, JSONObject result) throws JSONException {
    if (!result.has(WIRE_LATITUDE) || !result.has(WIRE_LONGITUDE) || !result.has(WIRE_ADDRESS)) {
        return null;
    }/*from www  . j  a v a2s .  c  o  m*/
    Address address = new Address(locale);
    address.setLatitude(result.getDouble(WIRE_LATITUDE));
    address.setLatitude(result.getDouble(WIRE_LONGITUDE));

    int line = 0;
    JSONObject a = result.getJSONObject(WIRE_ADDRESS);

    if (a.has(WIRE_THOROUGHFARE)) {
        address.setAddressLine(line++, a.getString(WIRE_THOROUGHFARE));
        address.setThoroughfare(a.getString(WIRE_THOROUGHFARE));
    }
    if (a.has(WIRE_SUBLOCALITY)) {
        address.setSubLocality(a.getString(WIRE_SUBLOCALITY));
    }
    if (a.has(WIRE_POSTALCODE)) {
        address.setAddressLine(line++, a.getString(WIRE_POSTALCODE));
        address.setPostalCode(a.getString(WIRE_POSTALCODE));
    }
    if (a.has(WIRE_LOCALITY_CITY)) {
        address.setAddressLine(line++, a.getString(WIRE_LOCALITY_CITY));
        address.setLocality(a.getString(WIRE_LOCALITY_CITY));
    } else if (a.has(WIRE_LOCALITY_TOWN)) {
        address.setAddressLine(line++, a.getString(WIRE_LOCALITY_TOWN));
        address.setLocality(a.getString(WIRE_LOCALITY_TOWN));
    } else if (a.has(WIRE_LOCALITY_VILLAGE)) {
        address.setAddressLine(line++, a.getString(WIRE_LOCALITY_VILLAGE));
        address.setLocality(a.getString(WIRE_LOCALITY_VILLAGE));
    }
    if (a.has(WIRE_SUBADMINAREA)) {
        address.setAddressLine(line++, a.getString(WIRE_SUBADMINAREA));
        address.setSubAdminArea(a.getString(WIRE_SUBADMINAREA));
    }
    if (a.has(WIRE_ADMINAREA)) {
        address.setAddressLine(line++, a.getString(WIRE_ADMINAREA));
        address.setAdminArea(a.getString(WIRE_ADMINAREA));
    }
    if (a.has(WIRE_COUNTRYNAME)) {
        address.setAddressLine(line++, a.getString(WIRE_COUNTRYNAME));
        address.setCountryName(a.getString(WIRE_COUNTRYNAME));
    }
    if (a.has(WIRE_COUNTRYCODE)) {
        address.setCountryCode(a.getString(WIRE_COUNTRYCODE));
    }

    return address;
}