Example usage for android.location Address getLocality

List of usage examples for android.location Address getLocality

Introduction

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

Prototype

public String getLocality() 

Source Link

Document

Returns the locality of the address, for example "Mountain View", or null if it is unknown.

Usage

From source file:Main.java

public static String getAddressString(List<Address> addresses) {
    String locality = null;/*  w  ww .  j a  v a 2  s . c o  m*/

    for (Address address : addresses) {
        String subLocality = address.getSubLocality();
        if (subLocality != null || locality == null) {
            locality = address.getLocality();
        }

        if (subLocality != null && locality != null) {
            return String.format("%s, %s", subLocality, locality);
        }
    }

    return locality;
}

From source file:Main.java

public static String locationToAddress(Location loc, Context context) {
    try {//from  w w  w .j av a2 s .co m
        double latitude, longitude;
        String addressText = "";
        geocoder = new Geocoder(context);
        latitude = loc.getLatitude();
        longitude = loc.getLongitude();
        List<Address> list = geocoder.getFromLocation(latitude, longitude, 1);

        if (list != null && list.size() > 0) {
            Address address = list.get(0);
            addressText = String.format("%s, %s, %s",
                    address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
                    address.getLocality(), address.getCountryName());
        }

        return addressText;
    } catch (IOException e) {
        Log.e(LogDavid, "ERROR:" + e.toString());
        return "";
    }
}

From source file:net.frakbot.FWeather.util.WeatherHelper.java

/**
 * Gets the city name (where available), suffixed with the
 * country code./*ww w  .  jav a  2 s  .c o m*/
 *
 * @param context The current {@link Context}.
 * @param location The Location to get the name for.
 *
 * @return Returns the city name and country (eg. "London,UK")
 *         if available, null otherwise
 */
public static String getCityName(Context context, Location location) {
    String cityName = null;
    if (Geocoder.isPresent()) {
        Geocoder geocoder = new Geocoder(context);
        List<Address> addresses = null;
        try {
            addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
        } catch (IOException ignored) {
        }

        if (addresses != null && !addresses.isEmpty()) {
            final Address address = addresses.get(0);
            final String city = address.getLocality();
            if (!TextUtils.isEmpty(city)) {
                // We only set the city name if we actually have it
                // (to avoid the country code avoiding returning null)
                cityName = city + "," + address.getCountryCode();
            }
        }
    }

    String encodedCityName = null;

    try {
        encodedCityName = URLEncoder.encode(cityName, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        FLog.d(context, TAG, "Could not encode city name, assume no city available.");
    } catch (NullPointerException enp) {
        FLog.d(context, TAG, "Could not encode city name, assume no city available.");
    }

    return encodedCityName;
}

From source file:grandroid.geo.Geocoder.java

public static String convertAddress(double lat, double lng) throws Exception {
    List<Address> adds = getFromLocation(lat, lng, 1);
    if (adds == null || adds.isEmpty()) {
        throw new Exception("no address can be found");
    } else {//from w  w  w.ja  va  2 s  . co  m
        Address add = adds.get(0);
        if (add.getFeatureName() == null) {
            return add.getAdminArea() + add.getLocality() + add.getAddressLine(0);
        } else {
            return add.getFeatureName();
        }
    }
}

From source file:it.feio.android.omninotes.utils.GeocodeHelper.java

public static String getAddressFromCoordinates(Context mContext, double latitude, double longitude)
        throws IOException {
    String addressString = "";
    Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
    List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
    if (addresses.size() > 0) {
        Address address = addresses.get(0);
        if (address != null) {
            addressString = address.getThoroughfare() + ", " + address.getLocality();
        }/*from   w  ww. j  a  v a 2 s .  c  om*/
    }
    return addressString;
}

From source file:com.dycody.android.idealnote.utils.GeocodeHelper.java

static String getAddressFromCoordinates(Context mContext, double latitude, double longitude)
        throws IOException {
    String addressString = "";
    Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
    List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
    if (addresses.size() > 0) {
        Address address = addresses.get(0);
        if (address != null) {
            addressString = address.getThoroughfare() + ", " + address.getLocality();
        }/* w  w  w .  j  av a  2  s.c  o  m*/
    }
    return addressString;
}

From source file:com.kubotaku.android.openweathermap.lib.util.GeocodeUtil.java

/**
 * Get location name from Address.//from  w w  w.j  av a 2s .  com
 * <p/>
 * Use Android Geocode class.
 * <p/>
 *
 * @param context Context.
 * @param locale  Locale.
 * @param latlng  Address.
 * @return Location name of target address.
 */
@Deprecated
public static String pointToName(final Context context, final Locale locale, final LatLng latlng) {
    String name = "";

    try {
        Geocoder geocoder = new Geocoder(context, locale);
        List<Address> addressList = null;
        try {
            addressList = geocoder.getFromLocation(latlng.latitude, latlng.longitude, 1);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if ((addressList != null) && !addressList.isEmpty()) {

            int loopNum = addressList.size();
            for (int i = 0; i < loopNum; i++) {
                Address address = addressList.get(i);

                name = address.getLocality();
                if ((name == null) || (name.length() == 0)) {
                    name = address.getSubAdminArea();
                    if ((name != null) && (name.length() != 0)) {
                        break;
                    }
                } else {
                    break;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return name;
}

From source file:grandroid.geo.Geocoder.java

public static String convertAddress(double lat, double lng, boolean nation, boolean city, boolean district,
        boolean street) throws Exception {
    List<Address> adds = getFromLocation(lat, lng, 1);
    if (adds == null || adds.isEmpty()) {
        throw new Exception("no address can be found");
    } else {/*w  w  w .  j a v  a 2 s .co  m*/
        Address add = adds.get(0);
        StringBuilder sb = new StringBuilder();
        if (nation) {
            sb.append(add.getCountryName());
        }
        if (city) {
            sb.append(add.getAdminArea());
        }
        if (district) {
            sb.append(add.getLocality());
        }
        if (street) {
            sb.append(add.getAddressLine(0));
        }
        return sb.toString();
    }
}

From source file:ca.itquality.patrol.util.weather.YahooWeather.java

public static String addressToPlaceName(final Address address) {
    String result = "";
    if (address.getLocality() != null) {
        result += address.getLocality();
        result += " ";
    }/*from   w w  w.ja v a 2 s.co m*/
    if (address.getAdminArea() != null) {
        result += address.getAdminArea();
        result += " ";
    }
    if (address.getCountryName() != null) {
        result += address.getCountryName();
        result += " ";
    }
    return result;
}

From source file:com.googlecode.android_scripting.jsonrpc.JsonBuilder.java

private static JSONObject buildJsonAddress(Address address) throws JSONException {
    JSONObject result = new JSONObject();
    result.put("admin_area", address.getAdminArea());
    result.put("country_code", address.getCountryCode());
    result.put("country_name", address.getCountryName());
    result.put("feature_name", address.getFeatureName());
    result.put("phone", address.getPhone());
    result.put("locality", address.getLocality());
    result.put("postal_code", address.getPostalCode());
    result.put("sub_admin_area", address.getSubAdminArea());
    result.put("thoroughfare", address.getThoroughfare());
    result.put("url", address.getUrl());
    return result;
}