List of usage examples for android.location Location getTime
public long getTime()
From source file:Main.java
private static Location chooseTimeOrderedLocation(Location location1, Location location2) { if ((location1.getTime() - location2.getTime()) > LOCATIONTIME_THRESHOLD) { return location1; } else {//from ww w . j a v a2s . co m return (location1.getAccuracy() > location2.getAccuracy()) ? location1 : location2; } }
From source file:Main.java
/***********************************/ public static void writeLocation(Parcel dest, Location loc) { dest.writeString(loc.getProvider()); dest.writeLong(loc.getTime()); dest.writeDouble(loc.getLatitude()); dest.writeDouble(loc.getLongitude()); dest.writeDouble(loc.getAltitude()); dest.writeFloat(loc.getAccuracy());//ww w . j ava 2s . c o m 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 a 2 s. c om }
From source file:Main.java
private static Location getMostCurrent(final Location location1, final Location location2) { if (location1 == null) return location2; if (location2 == null) return location1; return (location2.getTime() > location1.getTime()) ? location2 : location1; }
From source file:Main.java
private static Location getLastBestLocation(Context pContext) { LocationManager locationManager = (LocationManager) pContext.getSystemService(Context.LOCATION_SERVICE); Location locationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); Location locationNet = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); long GPSLocationTime = 0; if (null != locationGPS) { GPSLocationTime = locationGPS.getTime(); }/*from w w w . ja v a 2 s . co m*/ long NetLocationTime = 0; if (null != locationNet) { NetLocationTime = locationNet.getTime(); } if (0 < GPSLocationTime - NetLocationTime) { Log.e(TAG, "Located by GPS"); return locationGPS; } else { Log.e(TAG, "Located by network"); return locationNet; } }
From source file:Main.java
/** * Determines whether one Location reading is better than the current * Location fix/* w w w . java 2 s.com*/ * * @param location * The new Location that you want to evaluate * @param currentBestLocation * The current Location fix, to which you want to compare the new * one */ public static boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use // the new location // because the user has likely moved if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be // worse } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location are from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and // accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; }
From source file:Main.java
/** Determines whether one Location reading is better than the current Location fix. * Code taken from/*from ww w . j a va 2 s.c om*/ * http://developer.android.com/guide/topics/location/obtaining-user-location.html * * @param newLocation The new Location that you want to evaluate * @param currentBestLocation The current Location fix, to which you want to compare the new * one * @return The better Location object based on recency and accuracy. */ public static Location getBetterLocation(Location newLocation, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return newLocation; } // Check whether the new location fix is newer or older long timeDelta = newLocation.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use the new location // because the user has likely moved. if (isSignificantlyNewer) { return newLocation; // If the new location is more than two minutes older, it must be worse } else if (isSignificantlyOlder) { return currentBestLocation; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (newLocation.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location are from the same provider boolean isFromSameProvider = isSameProvider(newLocation.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return newLocation; } else if (isNewer && !isLessAccurate) { return newLocation; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return newLocation; } return currentBestLocation; }
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;/* w w w . j a va 2 s.c o m*/ bestAccuracy = accuracy; bestTime = time; } } } return bestResult; }
From source file:br.com.bioscada.apps.biotracks.io.sendtogoogle.SendToGoogleUtils.java
/** * Prepares a list of locations to send to Google Maps or Google Fusion * Tables. Splits the locations into segments if necessary. * /*from ww w . j a v a 2 s .c o m*/ * @param track the track * @param locations the list of locations * @return an array of split segments. */ public static ArrayList<Track> prepareLocations(Track track, List<Location> locations) { ArrayList<Track> splitTracks = new ArrayList<Track>(); // Create a new segment Track segment = createNewSegment(track, locations.size() > 0 ? locations.get(0).getTime() : -1L); for (Location location : locations) { /* * Latitude is greater than 90 if the location is a pause/resume * separator. */ if (location.getLatitude() > 90) { endSegment(segment, location.getTime(), splitTracks); segment = createNewSegment(track, location.getTime()); } else { segment.addLocation(location); } } endSegment(segment, locations.size() > 0 ? locations.get(locations.size() - 1).getTime() : -1L, splitTracks); return splitTracks; }
From source file:de.ncoder.sensorsystem.android.logging.JSONUtils.java
private static Object wrapLocation(Location loc) { try {/*from ww w. j a va 2s.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(); } }