Java Code Examples for android.location.Address#getAdminArea()

The following examples show how to use android.location.Address#getAdminArea() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From your-local-weather with GNU General Public License v3.0 6 votes vote down vote up
public static String getCityAndCountryFromAddress(Address address) {
    if (address == null) {
        return "";
    }
    String geoCity = getCityFromAddress(address);
    String geoCountryDistrict = null;
    if(address.getAdminArea() != null) {
        geoCountryDistrict = address.getAdminArea();
    }
    String geoDistrictOfCity = address.getSubLocality();
    String geoCountryName = address.getCountryName();
    if ((geoDistrictOfCity == null) || "".equals(geoDistrictOfCity) || geoCity.equalsIgnoreCase(geoDistrictOfCity)) {
        if ((geoCountryDistrict == null) || "".equals(geoCountryDistrict) || geoCity.equals(geoCountryDistrict)) {
            return formatLocalityToTwoLines((("".equals(geoCity))?"":(geoCity)) + (("".equals(geoCountryName))?"":(", " + geoCountryName)));
        }
        return formatLocalityToTwoLines((("".equals(geoCity))?"":(geoCity + ", ")) + geoCountryDistrict + (("".equals(geoCountryName))?"":(", " + geoCountryName)));
    }
    return formatLocalityToTwoLines((("".equals(geoCity))?"":(geoCity + " - ")) + geoDistrictOfCity + (("".equals(geoCountryName))?"":(", " + geoCountryName)));
}
 
Example 2
Source File: ReverseGeocoder.java    From medialibrary with Apache License 2.0 6 votes vote down vote up
private String getLocalityAdminForAddress(final Address addr, final boolean approxLocation) {
    if (addr == null)
        return "";
    String localityAdminStr = addr.getLocality();
    if (localityAdminStr != null && !("null".equals(localityAdminStr))) {
        if (approxLocation) {
            // TODO: Uncomment these lines as soon as we may translations
            // for Res.string.around.
            // localityAdminStr =
            // mContext.getResources().getString(Res.string.around) + " " +
            // localityAdminStr;
        }
        String adminArea = addr.getAdminArea();
        if (adminArea != null && adminArea.length() > 0) {
            localityAdminStr += ", " + adminArea;
        }
        return localityAdminStr;
    }
    return null;
}
 
Example 3
Source File: EventsActivity.java    From apigee-android-sdk with Apache License 2.0 6 votes vote down vote up
public EventContainer(Entity entity) {
    this.eventName = entity.getStringProperty("eventName");
    JsonNode locationObject= (JsonNode) entity.getProperties().get("location");
    if( locationObject != null && Geocoder.isPresent() ) {
        Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());
        try {
            List<Address> addressList = myLocation.getFromLocation(locationObject.get("latitude").doubleValue(), locationObject.get("longitude").doubleValue(), 1);
            if( addressList != null && addressList.size() > 0 ) {
                Address locationAddress = addressList.get(0);
                this.eventLocation = locationAddress.getLocality() + ", " + locationAddress.getAdminArea();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
 
Example 4
Source File: LocationReader.java    From MuslimMateAndroid with GNU General Public License v3.0 5 votes vote down vote up
public boolean locationToAddress(double latitude, double longitude) {

        Geocoder geocoder = new Geocoder(this.context, Locale.getDefault());
        try {
            List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
            if (addresses != null) {
                Address returnedAddress = addresses.get(0);
                StringBuilder strReturnedAddress = new StringBuilder("");

                locationInfo.clear();

                String city = returnedAddress.getAdminArea();
                if(city==null) city = returnedAddress.getLocality();

                locationInfo.put("country", returnedAddress.getCountryName());
                locationInfo.put("countrycode", returnedAddress.getCountryCode().toLowerCase());
                locationInfo.put("latitude", ""+returnedAddress.getLatitude());
                locationInfo.put("longitude", ""+returnedAddress.getLongitude());
                if(city!=null)locationInfo.put("city", ""+city);

                String address = "";
                for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
                    address = address + returnedAddress.getAddressLine(i) + "\n";
                }
                locationInfo.put("address", address);

                return true;

            } else {
            }
        } catch (Exception e) {
        }

        return false;
    }
 
Example 5
Source File: LocationHelper.java    From SampleApp with Apache License 2.0 5 votes vote down vote up
/**
 * @param latitude  latitude of address
 * @param longitude longitude of address
 * @return simplified address of location
 */

public String getSimplifiedAddress(double latitude, double longitude) {
    String location = "";
    try {
        Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
        List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
        if (addresses.size() > 0) {
            Address address = addresses.get(0);
            String admin = address.getAdminArea();
            String subLocality = address.getSubLocality();
            String locality = address.getLocality();
            if (admin.length() > 10) {
                admin = admin.substring(0, 10) + "..";
            }
            if (locality != null && subLocality != null) {
                location = subLocality + "," + locality;
            } else if (subLocality != null) {
                location = subLocality + "," + admin;
            } else {
                location = locality + "," + admin;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return location;
}
 
Example 6
Source File: LocationHelper.java    From SampleApp with Apache License 2.0 5 votes vote down vote up
/**
 * @param latitude  latitude of address
 * @param longitude longitude of address
 * @return complete address of location
 */

public String getCompleteAddress(double latitude, double longitude) {
    String location = "";
    try {
        Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
        List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
        if (addresses.size() > 0) {
            Address address = addresses.get(0);
            String state, city, zip, street;
            if (address.getAdminArea() != null) {
                state = address.getAdminArea();
            } else {
                state = "";
            }
            if (address.getLocality() != null) {
                city = address.getLocality();
            } else {
                city = "";
            }
            if (address.getPostalCode() != null) {
                zip = address.getPostalCode();
            } else {
                zip = "";
            }

            if (address.getThoroughfare() != null) {
                street = address.getSubLocality() + "," + address.getThoroughfare();
            } else {
                street = address.getSubLocality() + "," + address.getFeatureName();
            }
            location = street + "," + city + "," + zip + "," + state;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return location;
}