Example usage for android.location Location getAccuracy

List of usage examples for android.location Location getAccuracy

Introduction

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

Prototype

public float getAccuracy() 

Source Link

Document

Get the estimated horizontal accuracy of this location, radial, in meters.

Usage

From source file:Main.java

/**
 * To be used if you just want a one-shot best last location, iterates over
 * all providers and returns the most accurate result.
 */// ww  w . j av a 2s .c  o m
public static Location getBestLastGeolocation(Context context) {
    LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = manager.getAllProviders();

    Location bestLocation = null;
    for (String it : providers) {
        Location location = manager.getLastKnownLocation(it);
        if (location != null) {
            if (bestLocation == null || location.getAccuracy() < bestLocation.getAccuracy()) {
                bestLocation = location;
            }
        }
    }

    return bestLocation;
}

From source file:Main.java

public static String locationToString(Location location) {
    return "(" + location.getLatitude() + ";" + location.getLongitude() + ")" + " S=" + location.getSpeed()
            + " C=" + location.getBearing() + " A=" + location.getAccuracy();
}

From source file:Main.java

private static Location chooseTimeOrderedLocation(Location location1, Location location2) {
    if ((location1.getTime() - location2.getTime()) > LOCATIONTIME_THRESHOLD) {
        return location1;
    } else {//from   w w w  .  j a  va 2s .  c  om
        return (location1.getAccuracy() > location2.getAccuracy()) ? location1 : location2;
    }
}

From source file:Main.java

public static String dumpLocationInfo(Location loc) {
    if (loc == null) {
        return "Location null or empty";
    }//  w  w w  . java2s . co  m
    return String.format(Locale.getDefault(), "lat: %f, lng: %f, acc: %f, provider: %s", loc.getLatitude(),
            loc.getLongitude(), loc.getAccuracy(), loc.getProvider());
}

From source file:Main.java

/***********************************/

public static void writeLocation(Parcel dest, Location loc) {
    dest.writeString(loc.getProvider());
    dest.writeLong(loc.getTime());/*  ww  w  .  ja v a 2 s .c o  m*/
    dest.writeDouble(loc.getLatitude());
    dest.writeDouble(loc.getLongitude());
    dest.writeDouble(loc.getAltitude());
    dest.writeFloat(loc.getAccuracy());
    dest.writeFloat(loc.getBearing());
    dest.writeFloat(loc.getSpeed());
}

From source file:com.tenforwardconsulting.cordova.bgloc.LocationConverter.java

public static JSONObject toJSONObject(android.location.Location location) throws JSONException {
    JSONObject json = new JSONObject();
    json.put("time", location.getTime());
    json.put("latitude", location.getLatitude());
    json.put("longitude", location.getLongitude());
    json.put("accuracy", location.getAccuracy());
    json.put("speed", location.getSpeed());
    json.put("altitude", location.getAltitude());
    json.put("bearing", location.getBearing());
    return json;/*  www  . ja v  a2  s .c  o m*/
}

From source file:Main.java

public static android.location.Location getLastBestLocation(Context _context) {
    if (locManager == null)
        locManager = (LocationManager) _context.getSystemService(Context.LOCATION_SERVICE);

    int minDistance = (int) 500;
    long minTime = System.currentTimeMillis() - (900 * 1000);

    android.location.Location bestResult = null;

    float bestAccuracy = Float.MAX_VALUE;
    long bestTime = Long.MIN_VALUE;

    // Iterate through all the providers on the system, keeping
    // note of the most accurate result within the acceptable time limit.
    // If no result is found within maxTime, return the newest Location.
    List<String> matchingProviders = locManager.getAllProviders();
    for (String provider : matchingProviders) {
        android.location.Location location = locManager.getLastKnownLocation(provider);
        if (location != null) {
            //                log(TAG, " location: " + location.getLatitude() + "," + location.getLongitude() + "," + location.getAccuracy() + "," + location.getSpeed() + "m/s");
            float accuracy = location.getAccuracy();
            long time = location.getTime();
            //                log(TAG, "time>minTime: " + (time > minTime) + ", accuracy<bestAccuracy: " + (accuracy < bestAccuracy));
            //                if ((time > minTime && accuracy < bestAccuracy)) {
            if (accuracy < bestAccuracy) {
                bestResult = location;//from w  w w  .  j  av a2s . c o m
                bestAccuracy = accuracy;
                bestTime = time;
            }
        }
    }

    return bestResult;
}

From source file:com.joulespersecond.oba.ObaAnalytics.java

/**
 * Tracks stop tap distance between bus stop location and users current location
 *
 * @param stopId       for action//from w w  w.j ava 2s  .c o m
 * @param myLocation   the users location
 * @param stopLocation tapped stop location
 */
public static void trackBusStopDistance(String stopId, Location myLocation, Location stopLocation) {
    if (isAnalyticsActive()) {
        if (myLocation == null) {
            return;
        }
        if (myLocation.getAccuracy() < LOCATION_ACCURACY_THRESHOLD) {
            float distanceInMeters = myLocation.distanceTo(stopLocation);
            ObaStopDistance stopDistance = null;

            if (distanceInMeters < ObaStopDistance.DISTANCE_1.getDistanceInMeters()) {
                stopDistance = ObaStopDistance.DISTANCE_1;
            } else if (distanceInMeters < ObaStopDistance.DISTANCE_2.getDistanceInMeters()) {
                stopDistance = ObaStopDistance.DISTANCE_2;
            } else if (distanceInMeters < ObaStopDistance.DISTANCE_3.getDistanceInMeters()) {
                stopDistance = ObaStopDistance.DISTANCE_3;
            } else if (distanceInMeters < ObaStopDistance.DISTANCE_4.getDistanceInMeters()) {
                stopDistance = ObaStopDistance.DISTANCE_4;
            } else if (distanceInMeters < ObaStopDistance.DISTANCE_5.getDistanceInMeters()) {
                stopDistance = ObaStopDistance.DISTANCE_5;
            } else if (distanceInMeters < ObaStopDistance.DISTANCE_6.getDistanceInMeters()) {
                stopDistance = ObaStopDistance.DISTANCE_6;
            } else if (distanceInMeters < ObaStopDistance.DISTANCE_7.getDistanceInMeters()) {
                stopDistance = ObaStopDistance.DISTANCE_7;
            } else {
                stopDistance = ObaStopDistance.DISTANCE_8;
            }

            reportEventWithCategory(ObaEventCategory.STOP_ACTION.toString(), "Stop Id: " + stopId,
                    stopDistance.toString());
        }
    }
}

From source file:org.onebusaway.android.io.ObaAnalytics.java

/**
 * Tracks stop tap distance between bus stop location and users current location
 *
 * @param stopId       for action/*from  w  ww. j a v  a  2s.c  om*/
 * @param myLocation   the users location
 * @param stopLocation tapped stop location
 */
public static void trackBusStopDistance(String stopId, Location myLocation, Location stopLocation) {
    if (isAnalyticsActive()) {
        if (myLocation == null) {
            return;
        }
        if (myLocation.getAccuracy() < LOCATION_ACCURACY_THRESHOLD) {
            float distanceInMeters = myLocation.distanceTo(stopLocation);
            ObaStopDistance stopDistance;

            if (distanceInMeters < ObaStopDistance.DISTANCE_1.getDistanceInMeters()) {
                stopDistance = ObaStopDistance.DISTANCE_1;
            } else if (distanceInMeters < ObaStopDistance.DISTANCE_2.getDistanceInMeters()) {
                stopDistance = ObaStopDistance.DISTANCE_2;
            } else if (distanceInMeters < ObaStopDistance.DISTANCE_3.getDistanceInMeters()) {
                stopDistance = ObaStopDistance.DISTANCE_3;
            } else if (distanceInMeters < ObaStopDistance.DISTANCE_4.getDistanceInMeters()) {
                stopDistance = ObaStopDistance.DISTANCE_4;
            } else if (distanceInMeters < ObaStopDistance.DISTANCE_5.getDistanceInMeters()) {
                stopDistance = ObaStopDistance.DISTANCE_5;
            } else if (distanceInMeters < ObaStopDistance.DISTANCE_6.getDistanceInMeters()) {
                stopDistance = ObaStopDistance.DISTANCE_6;
            } else if (distanceInMeters < ObaStopDistance.DISTANCE_7.getDistanceInMeters()) {
                stopDistance = ObaStopDistance.DISTANCE_7;
            } else {
                stopDistance = ObaStopDistance.DISTANCE_8;
            }

            reportEventWithCategory(ObaEventCategory.STOP_ACTION.toString(), "Stop Id: " + stopId,
                    stopDistance.toString());
        }
    }
}

From source file:de.ncoder.sensorsystem.android.logging.JSONUtils.java

private static Object wrapLocation(Location loc) {
    try {/*from ww w . j a  va 2 s  . c o  m*/
        JSONObject json = new JSONObject();
        json.put("provider", loc.getProvider());
        json.put("latitude", loc.getLatitude());
        json.put("longitude", loc.getLongitude());
        if (loc.hasAccuracy())
            json.put("accuracy", loc.getAccuracy());
        json.put("time", loc.getTime());
        if (loc.hasAltitude())
            json.put("alt", loc.getAltitude());
        if (loc.hasSpeed())
            json.put("vel", loc.getSpeed());
        if (loc.hasBearing())
            json.put("bear", loc.getBearing());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
            if (loc.isFromMockProvider())
                json.put("mock", true);
        if (loc.getExtras() != null) {
            json.put("extra", wrap(loc.getExtras()));
        }
        return json;
    } catch (JSONException e) {
        return loc.toString() + " threw " + e.toString();
    }
}