Example usage for android.location Geocoder isPresent

List of usage examples for android.location Geocoder isPresent

Introduction

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

Prototype

public static boolean isPresent() 

Source Link

Document

Returns true if the Geocoder methods getFromLocation and getFromLocationName are implemented.

Usage

From source file:Main.java

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static boolean isReverseGeocodePresent() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD && Geocoder.isPresent();
}

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  av a  2  s .co  m
    }
    return "Fehler, bitte manuell eintragen";
}

From source file:it.ms.theing.loquitur.functions.LocationInterface.java

/**
 * Get a string with the location.//from  w ww  . jav a2  s.c o  m
 * @param lat
 * latitude
 * @param lon
 * longitude
 * @return
 * location or empty string
 */
@JavascriptInterface
public String geoCoder(float lat, float lon) {
    try {
        Geocoder geo = new Geocoder(context);
        if (!geo.isPresent())
            return "";
        List<Address> addresses = geo.getFromLocation(lat, lon, 1);
        if (addresses == null)
            return "";
        if (addresses.size() > 0) {
            JSONArray ja = new JSONArray();
            if (addresses.get(0).getFeatureName() != null)
                ja.put(addresses.get(0).getFeatureName());
            else
                ja.put("");
            if (addresses.get(0).getAddressLine(0) != null)
                ja.put(addresses.get(0).getAddressLine(0));
            else
                ja.put("");
            if (addresses.get(0).getLocality() != null)
                ja.put(addresses.get(0).getLocality());
            else
                ja.put("");
            if (addresses.get(0).getAdminArea() != null)
                ja.put(addresses.get(0).getAdminArea());
            else
                ja.put("");
            if (addresses.get(0).getCountryName() != null)
                ja.put(addresses.get(0).getCountryName());
            else
                ja.put("");
            return ja.toString();
        }
    } catch (Exception e) {
    }
    return "";
}

From source file:com.cecs492a_group4.sp.SingleEvent.java

public String reverseGeocode(double latitude, double longitude) throws IOException {
    Geocoder gc = new Geocoder(this);

    if (gc.isPresent()) {
        List<Address> list = gc.getFromLocation(latitude, longitude, 1);

        //(latitude, longitude, 1)
        //33.777043, -118.114395, 1)

        Address address = list.get(0);//from   w  w w .  j  ava2  s .  c  o  m

        StringBuffer str = new StringBuffer();

        if (address.getAddressLine(0) != null && address.getLocality() != null && address.getAdminArea() != null
                && address.getPostalCode() != null && address.getCountryName() != null) {
            //str.append(address.getAddressLine(0) + ", ");
            //str.append(address.getLocality() + ", ");
            //str.append(address.getAdminArea() + " ");
            //str.append(address.getPostalCode() + ", ");
            //str.append(address.getCountryName());
            //str.append("USA");

            //String strAddress = str.toString();

            String strAddress = (address.getAddressLine(0) + ", " + address.getLocality() + ", "
                    + address.getAdminArea() + " " + address.getPostalCode() + ", " + "USA");

            return strAddress;
        } else {
            return null;
        }
    }

    return null;
}

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

public static void stop() {
    SmartLocation.with(IdealNote.getAppContext()).location().stop();
    if (Geocoder.isPresent()) {
        SmartLocation.with(IdealNote.getAppContext()).geocoding().stop();
    }//  www.  j  ava2 s.com
}

From source file:com.adampmarshall.speedo.LocationActivity.java

/**
 * This sample demonstrates how to incorporate location based services in
 * your app and process location updates. The app also shows how to convert
 * lat/long coordinates to human-readable addresses.
 *///  w  ww .j a  va 2s. co m
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Restore apps state (if exists) after rotation.
    if (savedInstanceState != null) {
        mUseFine = savedInstanceState.getBoolean(KEY_FINE);
        mUseBoth = savedInstanceState.getBoolean(KEY_BOTH);
    } else {
        mUseFine = false;
        mUseBoth = true;
    }
    mSpeed = (TextView) findViewById(R.id.speed);
    Typeface font = Typeface.createFromAsset(getAssets(), "DS-DIGI.TTF");
    mSpeed.setTypeface(font);

    // The isPresent() helper method is only available on Gingerbread or
    // above.
    mGeocoderAvailable = Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD && Geocoder.isPresent();

    // Handler for updating text fields on the UI like the lat/long and
    // address.
    mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case UPDATE_SPEED:
                mSpeed.setText((String) msg.obj);
                break;
            }
        }
    };
    // Get a reference to the LocationManager object.
    mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    mGmeterView = (GmeterView) findViewById(R.id.gmeter);

}

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));
                    }//from w w w . java 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:com.example.get_location.Get_location.java

/**
 * This sample demonstrates how to incorporate location based services in your app and
 * process location updates.  The app also shows how to convert lat/long coordinates to
 * human-readable addresses.// ww w  .ja va  2 s  . com
 */
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_getlocation);

    // Restore apps state (if exists) after rotation.
    if (savedInstanceState != null) {
        mUseFine = savedInstanceState.getBoolean(KEY_FINE);
        mUseBoth = savedInstanceState.getBoolean(KEY_BOTH);
    } else {
        mUseFine = false;
        mUseBoth = false;
    }
    mLatLng = (TextView) findViewById(R.id.latlng);
    mAddress = (TextView) findViewById(R.id.address);
    // Receive location updates from the fine location provider (gps) only.
    mFineProviderButton = (Button) findViewById(R.id.provider_fine);
    // Receive location updates from both the fine (gps) and coarse (network) location
    // providers.
    mBothProviderButton = (Button) findViewById(R.id.provider_both);

    // The isPresent() helper method is only available on Gingerbread or above.
    mGeocoderAvailable = Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD && Geocoder.isPresent();

    // Handler for updating text fields on the UI like the lat/long and address.
    mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case UPDATE_ADDRESS:
                mAddress.setText((String) msg.obj);
                break;
            case UPDATE_LATLNG:
                mLatLng.setText((String) msg.obj);
                break;
            }
        }
    };
    // Get a reference to the LocationManager object.
    mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}

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

/**
 * Convert geographic coordinates to a human-readable address.
 */// w  w  w .  j a  va  2s . co 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.dycody.android.idealnote.utils.GeocodeHelper.java

public static void getAddressFromCoordinates(Location location,
        final OnGeoUtilResultListener onGeoUtilResultListener) {
    if (!Geocoder.isPresent()) {
        onGeoUtilResultListener.onAddressResolved("");
    } else {/*from  w  w w  .  j av  a 2  s . c o m*/
        SmartLocation.with(IdealNote.getAppContext()).geocoding().reverse(location, (location1, list) -> {
            String address = list.size() > 0 ? list.get(0).getAddressLine(0) : null;
            onGeoUtilResultListener.onAddressResolved(address);
        });
    }
}