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: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 ww.  ja  v a 2s .c o m*/
    }
    return addressString;
}

From source file:com.vishwa.pinit.LocationSuggestionProvider.java

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

    switch (uriMatcher.match(uri)) {
    case SEARCH_SUGGEST:
        String query = uri.getLastPathSegment();

        MatrixCursor cursor = new MatrixCursor(SEARCH_SUGGEST_COLUMNS, 1);
        if (Geocoder.isPresent()) {
            try {
                mGeocoder = new Geocoder(getContext(), Locale.ENGLISH);
                List<Address> addressList = mGeocoder.getFromLocationName(query, 5);
                for (int i = 0; i < addressList.size(); i++) {
                    Address address = addressList.get(i);
                    StringBuilder fullAddress = new StringBuilder();
                    for (int j = 1; j < address.getMaxAddressLineIndex(); j++) {
                        fullAddress.append(address.getAddressLine(j));
                    }//www. ja v a 2 s .c  o m
                    cursor.addRow(new String[] { Integer.toString(i), address.getAddressLine(0).toString(),
                            fullAddress.toString(), address.getLatitude() + "," + address.getLongitude() });
                }

                return cursor;
            } catch (IllegalArgumentException e) {
                return getLocationSuggestionsUsingAPI(query, cursor);
            } catch (IOException e) {
                return getLocationSuggestionsUsingAPI(query, cursor);
            }
        }
    default:
        throw new IllegalArgumentException("Unknown Uri: " + uri);
    }
}

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

/**
 * Convert geographic coordinates to a human-readable address.
 *//*from   w  w w .j a  v a 2 s .  c  o  m*/
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.alaskalinuxuser.mymemoriableplacesapp.MapsActivity.java

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;//from w  w w  .j  a v  a 2s.c  om
    mMap.clear();

    Double lat = Double.parseDouble(latPlace);
    Double lon = Double.parseDouble(longPlace);

    LatLng currentPlace = new LatLng(lat, lon);
    mMap.addMarker(new MarkerOptions().position(currentPlace).title(namePlace)
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
    mMap.moveCamera(CameraUpdateFactory.zoomTo(10));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(currentPlace));

    mMap.setOnMapLongClickListener(new OnMapLongClickListener() {

        @Override
        public void onMapLongClick(LatLng arg0) {

            mMap.addMarker(new MarkerOptions().position(arg0).title("new location")
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));

            Double lati = (arg0.latitude);
            Double loni = (arg0.longitude);
            String aLatPlace = lati.toString();
            String aLongPlace = loni.toString();

            Geocoder myGeo = new Geocoder(getApplicationContext(), Locale.getDefault());

            try {
                List<Address> myAddresses = myGeo.getFromLocation(lati, loni, 1);

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

                    // FOR TESTING //Log.i("WJH", myAddresses.get(0).toString());

                    myNewLocal = myAddresses.get(0).getAddressLine(0) + ", "
                            + myAddresses.get(0).getAddressLine(1);

                } else {

                    myNewLocal = "";

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

            Intent returnIntent = getIntent();
            returnIntent.putExtra("anamePlace", myNewLocal);
            returnIntent.putExtra("alatPlace", aLatPlace);
            returnIntent.putExtra("alongPlace", aLongPlace);
            setResult(Activity.RESULT_OK, returnIntent);
            finish();

        }
    });

}

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

public static double[] getCoordinatesFromAddress(Context mContext, String address) throws IOException {
    double[] result = new double[2];
    Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
    List<Address> addresses = geocoder.getFromLocationName(address, 1);
    if (addresses.size() > 0) {
        double latitude = addresses.get(0).getLatitude();
        double longitude = addresses.get(0).getLongitude();
        result[0] = latitude;//  www.j  ava 2  s .co m
        result[1] = longitude;
    }
    return result;
}

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

/**
 * Get location name from Address.//from w  w  w .  j  ava2 s .c  om
 * <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:semanticweb.hws14.movapp.activities.Criteria.java

private void useLocationData(Location location) {
    //Receive the location Data
    AlertDialog ad = new AlertDialog.Builder(that).create();
    ad.setCancelable(false); // This blocks the 'BACK' button
    Geocoder geocoder = new Geocoder(that, Locale.ENGLISH);
    try {/*from   ww w .  j  a  va 2s.c  o  m*/
        List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);

        if (addresses != null) {
            Address returnedAddress = addresses.get(0);
            final String strReturnedAdress = returnedAddress.getLocality();
            ad.setMessage("You are in: " + strReturnedAdress + "\nUse this location for the search?");

            ad.setButton(DialogInterface.BUTTON_POSITIVE, "YES", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    if (tabPosition == 0) {
                        MovieCriteria currentFragmet = (MovieCriteria) getFragmentByPosition(tabPosition);
                        currentFragmet.setGPSLocation(strReturnedAdress);
                    } else if (tabPosition == 1) {
                        ActorCriteria currentFragmet = (ActorCriteria) getFragmentByPosition(tabPosition);
                        currentFragmet.setGPSLocation(strReturnedAdress);
                    }

                    dialog.dismiss();
                }
            });

            ad.setButton(DialogInterface.BUTTON_NEGATIVE, "NO", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
        } else {
            ad.setMessage("No Address returned!");
            ad.setButton(DialogInterface.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
        }
    } catch (IOException e) {
        e.printStackTrace();
        ad.setMessage("Can not get Address!");
        ad.setButton(DialogInterface.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
    }

    ad.show();
    setProgressBarIndeterminateVisibility(false);
    locMgr.removeUpdates(locListner);
}

From source file:com.laocuo.weather.presenter.impl.LocationPresenter.java

private String saveCityByLocation(Location l) {
    String city = null;/*from   ww  w.  j ava  2 s.com*/
    Double latitude = l.getLatitude();
    Double longitude = l.getLongitude();
    Geocoder gc = new Geocoder(mContext, Locale.getDefault());
    List<Address> addressList = null;
    try {
        addressList = gc.getFromLocation(latitude, longitude, 10);
        Address address = addressList.get(0);
        L.d("Locality:" + address.getLocality());
        city = address.getLocality();
        if (TextUtils.isEmpty(city) == false) {
            SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(mContext).edit();
            editor.putString(CITY_KEY, city);
            editor.commit();
        }
    } catch (IOException e) {
        e.printStackTrace();
        mView.getLocationFail();
    } finally {
        return city;
    }
}

From source file:com.example.android.animationsdemo.RecentQueryActivity.java

public void getAddressByLocation(Location location) {
    String result = "No Result";
    try {/*w w w  .  j av  a  2  s  . co m*/
        if (location != null) {
            Double longitude = location.getLongitude(); //?
            Double latitude = location.getLatitude(); //?

            //Geocoder: Android 8 ?
            Geocoder gc = new Geocoder(this, Locale.TRADITIONAL_CHINESE); //?:??
            //??
            List<Address> lstAddress = gc.getFromLocation(latitude, longitude, 5);
            if (lstAddress.size() > 0) {
                result = "";
                for (int i = 0; i < lstAddress.size(); i++) {
                    result = result + i + " : " + "\n";
                    result = result + "Address: " + lstAddress.get(0).getAddressLine(0) + "\n";
                    result = result + "Locality: " + lstAddress.get(0).getLocality() + "\n";
                    result = result + "AdminArea: " + lstAddress.get(0).getAdminArea() + "\n";
                    result = result + "Country: " + lstAddress.get(0).getCountryName() + "\n";
                    result = result + "PostalCode: " + lstAddress.get(0).getPostalCode() + "\n";
                    result = result + "Feature: " + lstAddress.get(0).getFeatureName() + "\n\n";

                    //result = result + lstAddress.get(i).getAddressLine(0) + "\n";
                    //result = result + lstAddress.get(i).getPostalCode() + "\n";
                }
            }
            resultTextView.setText(result);
        }
    } catch (Exception e) {
        DKLog.e(TAG, Trace.getCurrentMethod() + e.toString());
    }
}

From source file:com.android.deskclock.worldclock.CityAndTimeZoneLocator.java

private String resolveCity() {
    try {/*ww w  . j a v  a  2s .  com*/
        Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
        List<Address> addresses = geocoder.getFromLocation(mLocation.getLatitude(), mLocation.getLongitude(),
                1);
        if (addresses.size() > 0) {
            return addresses.get(0).getLocality();
        } else {
            Log.w(TAG, "No city data");
        }
    } catch (IOException e) {
        Log.e(TAG, "Failed to retrieve city", e);
    }
    return null;
}