Example usage for android.location Geocoder getFromLocation

List of usage examples for android.location Geocoder getFromLocation

Introduction

In this page you can find the example usage for android.location Geocoder getFromLocation.

Prototype

public List<Address> getFromLocation(double latitude, double longitude, int maxResults) throws IOException 

Source Link

Document

Returns an array of Addresses that are known to describe the area immediately surrounding the given latitude and longitude.

Usage

From source file:Main.java

public static String getCountry(Context context) {
    @SuppressWarnings("static-access")
    LocationManager lm = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
    Location l = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    Geocoder gc = new Geocoder(context);

    try {/*from   w  w w  . j av a2 s . co  m*/
        List<Address> address = gc.getFromLocation(l.getLatitude(), l.getLongitude(), 1);
        if (address != null && address.isEmpty() == false)
            return address.get(0).getCountryName();
    } catch (IOException e) {
        return "";
    }

    return "";
}

From source file:Main.java

public static String getLocality(Context context) {
    @SuppressWarnings("static-access")
    LocationManager lm = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
    if (lm == null)
        return "";
    Location l = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    if (l == null)
        return "";
    Geocoder gc = new Geocoder(context);

    try {/*ww  w .j av a 2  s .  c o m*/
        List<Address> address = gc.getFromLocation(l.getLatitude(), l.getLongitude(), 1);
        if (address != null && address.isEmpty() == false)
            return address.get(0).getLocality();
    } catch (Exception e) {
        return "";
    }

    return "";
}

From source file:Main.java

public static List<Address> getAddress(Context c, Location loc) throws IOException {
    Geocoder geocoder = new Geocoder(c, Locale.getDefault());
    List<Address> addresses = null;

    addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);

    return addresses;
}

From source file:Main.java

public static String getAddressFromLoc(Context pContext, double latitude, double longitude) throws IOException {

    Geocoder geocoder = new Geocoder(pContext, Locale.getDefault());
    List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);

    String address = "";
    if (addresses.get(0) != null)
        address = addresses.get(0).getAddressLine(0);

    String city = addresses.get(0).getLocality();
    String zip = addresses.get(0).getPostalCode();
    String country = addresses.get(0).getCountryName();

    address = (address == null) ? "" : address;
    city = (city == null) ? "" : city;
    zip = (zip == null) ? "" : zip;
    country = (country == null) ? "" : country;

    return address + " " + city + " " + zip + " " + country;
}

From source file:Main.java

public static String getNameFromLocation(final double lat, final double lng, final Context context)
        throws IOException {
    final Geocoder gc = new Geocoder(context, Locale.getDefault());
    if (Geocoder.isPresent()) {
        final List<Address> list = gc.getFromLocation(lat, lng, 1);

        if (list.size() > 0) {
            final Address address = list.get(0);
            return address.getAddressLine(0);
        }/*from  w w w .  j  a v  a2  s.com*/
    }
    return "Fehler, bitte manuell eintragen";
}

From source file:Main.java

public static String getCompleteAddressString(Context context, double LATITUDE, double LONGITUDE) {
    String strAdd = "";
    Geocoder geocoder = new Geocoder(context, Locale.getDefault());
    try {//from   w  w w. j  a  va2  s. c o  m
        List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
        if (addresses != null) {
            Address returnedAddress = addresses.get(0);
            StringBuilder strReturnedAddress = new StringBuilder("");

            for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
                strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
            }
            strAdd = strReturnedAddress.toString();
            Log.w("My Current loction address", "" + strReturnedAddress.toString());
        } else {
            Log.w("My Current loction address", "No Address returned!");
        }

    } catch (Exception e) {
        e.printStackTrace();
        Log.w("My Current loction address", "Canont get Address!");
    }
    return strAdd;
}

From source file:Main.java

public static String getAddressFromLocation(@NonNull Context context, @NonNull Location location) {
    final Geocoder geocoder = new Geocoder(context, Locale.getDefault());
    final StringBuilder builder = new StringBuilder();
    try {/*from   w w w.j a  v a  2s  .c o  m*/
        final List<Address> addresses = geocoder.getFromLocation(location.getLatitude(),
                location.getLongitude(), 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5

        // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
        final String address = addresses.get(0).getAddressLine(0);
        if (addIfNotEmpty(builder, address)) {
            builder.append(", ");
        }

        final String city = addresses.get(0).getLocality();
        if (addIfNotEmpty(builder, city)) {
            builder.append(", ");
        }

        final String postalCode = addresses.get(0).getPostalCode();
        if (addIfNotEmpty(builder, postalCode)) {
            builder.append(", ");
        }

        final String state = addresses.get(0).getAdminArea();
        if (addIfNotEmpty(builder, state)) {
            builder.append(", ");
        }

        final String country = addresses.get(0).getCountryName();
        if (addIfNotEmpty(builder, country)) {
            builder.append(", ");
        }

        //            final String knownName = addresses.get(0).getFeatureName();
        //            addIfNotEmpty(builder, knownName);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return builder.toString();
}

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 w w  .  j a va 2 s.  c  om*/
    }
    return addressString;
}

From source file:net.giovannicapuano.galax.util.Utils.java

/**
 * Convert geographic coordinates to a human-readable address.
 *///  w w w .  ja  v a 2 s .  c  om
public static String coordinatesToAddress(double latitude, double longitude, Context context) {
    Geocoder geocoder = new Geocoder(context, Locale.getDefault());
    if (!Geocoder.isPresent())
        return "";

    try {
        List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);

        if (addresses != null && !addresses.isEmpty()) {
            Address returnedAddress = addresses.get(0);
            StringBuilder address = new StringBuilder();
            for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); ++i)
                address.append(returnedAddress.getAddressLine(i)).append("\n");
            return address.toString();
        } else {
            return "";
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

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();
        }//from  w  w w  . ja  v  a 2  s  .co  m
    }
    return addressString;
}