Example usage for android.location Geocoder Geocoder

List of usage examples for android.location Geocoder Geocoder

Introduction

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

Prototype

public Geocoder(Context context, Locale locale) 

Source Link

Document

Constructs a Geocoder whose responses will be localized for the given Locale.

Usage

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 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   ww  w  .j  av a  2 s.c  om
    }
    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 ww  w. j  av a 2  s.  co 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 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 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  va2  s .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:dk.laundav.locationservice.service.LocationService.java

public static LocationObject getLocationFromGeocoder(Context context) {

    gps = new GPSTracker(context);

    // check if GPS enabled     
    if (gps.canGetLocation()) {

        double latitude = gps.getLatitude();
        double longitude = gps.getLongitude();

        Geocoder geocoder = new Geocoder(context, Locale.getDefault());

        LocationObject location = new LocationObject(latitude, longitude);

        try {/*from   www . jav a  2 s.com*/

            // addresses er altid lig 0 (ingen elementer)
            List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(),
                    1);

            if (addresses != null && addresses.size() > 0) {

                System.out.println("The geocoder works!");

                location.setLocation(addresses.get(0).getAddressLine(0), // address
                        addresses.get(0).getAddressLine(1), // city
                        addresses.get(0).getAddressLine(2) // country
                );

            }

            return location;

        } catch (IOException e) {
            e.printStackTrace();
            return location;
        }

    } else {
        // can't get location
        // GPS or Network is not enabled
        // Ask user to enable GPS/network in settings
        gps.showSettingsAlert();
        return null;
    }
}

From source file:com.mk4droid.IMC_Utils.GEO.java

public static String ConvertGeoPointToAddress(LatLng pt, Context ctx) {

    Address maddress = null;/* ww w. jav  a  2s . c  o  m*/
    try {
        Geocoder geocoder = new Geocoder(ctx, Locale.getDefault());
        List<Address> list = geocoder.getFromLocation(pt.latitude, pt.longitude, 1);
        if (list != null && list.size() > 0) {
            maddress = list.get(0);
        }
    } catch (Exception e) {
        Log.e(Constants_API.TAG, "Gecoder falied: I will try with REST");
        new RevGEO_Try2_Asynch(pt.latitude, pt.longitude).execute();
    }

    String Address_STR = "";

    if (maddress != null) {
        for (int i = 0; i < maddress.getMaxAddressLineIndex(); i++)
            Address_STR += maddress.getAddressLine(i) + ", ";

        Address_STR += maddress.getCountryName();
    }

    return Address_STR;
}

From source file:self.yue.vehicletracker.ui.history.HistoryDetailActivity.java

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ButterKnife.bind(this);

    getData();//from  w w w .  jav  a2s  .  c  o  m
    initViews();
    setupMap();
    setupToolbar();

    mGeocoder = new Geocoder(this, Locale.getDefault());
    getLocationAddress(mStartPosition.latitude, mStartPosition.longitude, true);
    getLocationAddress(mEndPosition.latitude, mEndPosition.longitude, false);
}

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();
        }/*w w w  .  ja  v  a2  s . com*/
    }
    return addressString;
}

From source file:com.licenta.android.licenseapp.location.LocationService.java

private void saveLastKnownLocation(Location location) {
    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    List<Address> addresses = null;

    try {/* w w  w.j  av a 2 s  .  c  o  m*/
        addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        prefs.edit().putString(Constants.PREF_KEY_LAST_KNOWN_ADDRESS, addresses.get(0).getAddressLine(0))
                .apply();
    } catch (IOException | IllegalArgumentException e) {
        e.printStackTrace();
    }

}