Example usage for android.location Address setFeatureName

List of usage examples for android.location Address setFeatureName

Introduction

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

Prototype

public void setFeatureName(String featureName) 

Source Link

Document

Sets the feature 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 av  a  2 s.c  o  m*/
    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;
}