Android Utililty Methods Distance Calculate

List of utility methods to do Distance Calculate

Description

The list of methods to do Distance Calculate are organized into topic(s).

Method

StringgetMessageForDistance(long distance)
get Message For Distance
if (distance == 0)
    return TODAY;
if (distance == 1)
    return TOMORROW;
if (distance == -1)
    return YESTERDAY;
String pastOrFuture = distance > 0 ? AWAY : PAST;
distance = distance < 0 ? distance * -1 : distance;
...
StringcalculateEte(boolean useBearing, double distance, double speed, double bearing, double heading)
Fetch the estimate travel time to the indicated target
if (0 == speed) {
    return "--:--";
Time eteRaw = fetchRawEte(useBearing, distance, speed, bearing,
        heading);
if (null == eteRaw) {
    return "--:--";
int eteHr = eteRaw.hour;
int eteMin = eteRaw.minute;
int eteSecond = eteRaw.second;
if (eteHr > 99) {
    return "XX:XX";
if (eteHr > 0) {
    String hr = String.format(Locale.getDefault(), "%02d", eteHr);
    String min = String.format(Locale.getDefault(), "%02d", eteMin);
    return hr + ":" + min;
String min = String.format(Locale.getDefault(), "%02d", eteMin);
String sec = String.format(Locale.getDefault(), "%02d", eteSecond);
return min + "." + sec;
TimefetchRawEte(boolean useBearing, double distance, double speed, double bearing, double heading)
Fetch the raw estimated time enroute given the input parameters
double xFactor = 1;
if (useBearing) {
    double angDif = angularDifference(heading, bearing);
    if (angDif >= 90)
        return null;
    xFactor = Math.cos(angDif * Math.PI / 180);
double eteTotal = (distance / (speed * xFactor)) * 3600;
...
doubledistance(double lat1, double lon1, double lat2, double lon2, char unit)
Calculates the distance between two geo-coordinates
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2))
        + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2))
        * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == 'K') {
...
doublecomputeSpeed(int distance, long duration)
Computes the average speed in km/h.
return ((double) distance / 1000)
        / ((double) duration / DateTimeConstants.MILLIS_PER_HOUR);
intgetClosestStore(ArrayList storeIds, SparseArray distanceMap)
get Closest Store
float closestDistance = -1;
int closestStore = 0;
if (storeIds != null && distanceMap != null) {
    for (int j = 0; j < storeIds.size(); j++) {
        float nextDistance = distanceMap.get(storeIds.get(j));
        if (closestDistance < 0 || nextDistance < closestDistance) {
            closestDistance = nextDistance;
            closestStore = storeIds.get(j);
...
UritoDistanceSearchUri(Uri contentUri, Location location, double distance)
Makes a URI that queries the locatable item by distance.
return contentUri
        .buildUpon()
        .appendQueryParameter(
                SERVER_QUERY_PARAMETER,
                location.getLongitude() + ","
                        + location.getLatitude() + "," + distance)
        .build();
DisplaygetDisplay(Activity activity)
get Display
WindowManager wm = (WindowManager) activity
        .getSystemService(Context.WINDOW_SERVICE);
return wm.getDefaultDisplay();