Java Code Examples for android.location.Location#setAccuracy()
The following examples show how to use
android.location.Location#setAccuracy() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Sensors.java From Multiwii-Remote with Apache License 2.0 | 6 votes |
public void setMOCKLocation(double Latitude, double Longitude, float Altitude, float Heading, float speed) { Location mockLocation = new Location(mocLocationProvider); // a string mockLocation.setLatitude(Latitude); // double mockLocation.setLongitude(Longitude); mockLocation.setAltitude(Altitude); mockLocation.setTime(System.currentTimeMillis()); mockLocation.setAccuracy(1); mockLocation.setBearing(Heading); mockLocation.setSpeed(speed * 0.01f); try { Method locationJellyBeanFixMethod = Location.class.getMethod("makeComplete"); if (locationJellyBeanFixMethod != null) { locationJellyBeanFixMethod.invoke(mockLocation); } } catch (Exception e) { // TODO: handle exception } locationManager.setTestProviderLocation(mocLocationProvider, mockLocation); }
Example 2
Source File: GeoHashLocationTest.java From android-sdk with MIT License | 6 votes |
@Test public void parcelable_test() throws Exception { String provider = "some_provider"; Location l = new Location(provider); l.setLatitude(1); l.setLongitude(2); l.setTime(System.currentTimeMillis()); l.setAccuracy(4); GeoHashLocation tested = new GeoHashLocation(l); Parcel p = Parcel.obtain(); tested.writeToParcel(p, 0); p.setDataPosition(0); GeoHashLocation generated = GeoHashLocation.CREATOR.createFromParcel(p); assertEquals(generated.getGeohash(), tested.getGeohash()); assertEquals(generated.getLatitude(), tested.getLatitude(), 0.01); assertEquals(generated.getLongitude(), tested.getLongitude(), 0.01); assertEquals(generated.getTime(), tested.getTime()); assertEquals(generated.getAccuracy(), tested.getAccuracy(), 0.01); }
Example 3
Source File: BackgroundLocation.java From background-geolocation-android with Apache License 2.0 | 6 votes |
/** * Return android Location instance * * @return android.location.Location instance */ public Location getLocation() { Location l = new Location(provider); l.setLatitude(latitude); l.setLongitude(longitude); l.setTime(time); if (hasAccuracy) l.setAccuracy(accuracy); if (hasAltitude) l.setAltitude(altitude); if (hasSpeed) l.setSpeed(speed); if (hasBearing) l.setBearing(bearing); l.setExtras(extras); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { l.setElapsedRealtimeNanos(elapsedRealtimeNanos); } return l; }
Example 4
Source File: ContentProviderLocationDAOTest.java From background-geolocation-android with Apache License 2.0 | 5 votes |
@Test public void testPersistLocation() { LocationDAO dao = new ContentProviderLocationDAO(getContext()); Location location = new Location("fake"); location.setAccuracy(200); location.setAltitude(900); location.setBearing(2); location.setLatitude(40.21); location.setLongitude(23.45); location.setSpeed(20); location.setProvider("test"); location.setTime(1000); BackgroundLocation bgLocation = BackgroundLocation.fromLocation(location); dao.persistLocation(bgLocation); ArrayList<BackgroundLocation> locations = new ArrayList(dao.getAllLocations()); assertEquals(1, locations.size()); BackgroundLocation storedLocation = locations.get(0); assertEquals(200, storedLocation.getAccuracy(), 0); assertEquals(900, storedLocation.getAltitude(), 0); assertEquals(2, storedLocation.getBearing(), 0); assertEquals(40.21, storedLocation.getLatitude(), 0); assertEquals(23.45, storedLocation.getLongitude(), 0); assertEquals(20, storedLocation.getSpeed(), 0); assertEquals("test", storedLocation.getProvider(), "test"); assertEquals(1000, storedLocation.getTime(), 0); junit.framework.Assert.assertFalse(storedLocation.hasMockLocationsEnabled()); junit.framework.Assert.assertFalse(storedLocation.areMockLocationsEnabled()); junit.framework.Assert.assertTrue(storedLocation.hasIsFromMockProvider()); // because setIsFromMockProvider is called in constructor junit.framework.Assert.assertFalse(storedLocation.isFromMockProvider()); }
Example 5
Source File: Kalman.java From DejaVu with GNU General Public License v3.0 | 5 votes |
public synchronized Location getLocation() { Long timeMs = System.currentTimeMillis(); final Location location = new Location(BackendService.LOCATION_PROVIDER); predict(timeMs); location.setTime(timeMs); location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos()); location.setLatitude(mLatTracker.getPosition()); location.setLongitude(mLonTracker.getPosition()); if (mAltTracker != null) location.setAltitude(mAltTracker.getPosition()); float accuracy = (float) (mLatTracker.getAccuracy() * BackendService.DEG_TO_METER); if (accuracy < MIN_ACCURACY) accuracy = MIN_ACCURACY; location.setAccuracy(accuracy); // Derive speed from degrees/ms in lat and lon double latVeolocity = mLatTracker.getVelocity() * BackendService.DEG_TO_METER; double lonVeolocity = mLonTracker.getVelocity() * BackendService.DEG_TO_METER * Math.cos(Math.toRadians(location.getLatitude())); float speed = (float) Math.sqrt((latVeolocity*latVeolocity)+(lonVeolocity*lonVeolocity)); location.setSpeed(speed); // Compute bearing only if we are moving. Report old bearing // if we are below our threshold for moving. if (speed > MOVING_THRESHOLD) { mBearing = (float) Math.toDegrees(Math.atan2(latVeolocity, lonVeolocity)); } location.setBearing(mBearing); Bundle extras = new Bundle(); extras.putLong("AVERAGED_OF", samples); location.setExtras(extras); return location; }
Example 6
Source File: WeightedAverage.java From DejaVu with GNU General Public License v3.0 | 5 votes |
public Location result() { if (count < 1) return null; final Location location = new Location(BackendService.LOCATION_PROVIDER); location.setTime(timeMs); location.setElapsedRealtimeNanos(mElapsedRealtimeNanos); location.setLatitude(latEst.getMean()); location.setLongitude(lonEst.getMean()); // // Accuracy estimate is in degrees, convert to meters for output. // We calculate North-South and East-West independently, convert to a // circular radius by finding the length of the diagonal. // double sdMetersLat = latEst.getStdDev() * BackendService.DEG_TO_METER; double cosLat = Math.max(BackendService.MIN_COS, Math.cos(Math.toRadians(latEst.getMean()))); double sdMetersLon = lonEst.getStdDev() * BackendService.DEG_TO_METER * cosLat; float acc = (float) Math.max(Math.sqrt((sdMetersLat*sdMetersLat)+(sdMetersLon*sdMetersLon)),MINIMUM_BELIEVABLE_ACCURACY); location.setAccuracy(acc); Bundle extras = new Bundle(); extras.putLong("AVERAGED_OF", count); location.setExtras(extras); return location; }
Example 7
Source File: MockLocationProvider.java From android_coursera_1 with MIT License | 5 votes |
public void pushLocation(double lat, double lon) { Location mockLocation = new Location(mProviderName); mockLocation.setLatitude(lat); mockLocation.setLongitude(lon); mockLocation.setAltitude(0); mockLocation.setTime(System.currentTimeMillis()); mockLocation .setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos()); mockLocation.setAccuracy(mockAccuracy); mLocationManager.setTestProviderLocation(mProviderName, mockLocation); }
Example 8
Source File: TripStatisticsUpdaterTest.java From mytracks with Apache License 2.0 | 5 votes |
/** * Creates a location and returns it. * * @param altitude altitude of location * @param latitude latitude of location * @param speed speed of location * @param time time of location */ private Location getLocation(double altitude, double latitude, float speed, long time) { Location location = new Location("test"); location.setAccuracy(1.0f); location.setLongitude(45.0); location.setAltitude(altitude); location.setLatitude(latitude); location.setSpeed(speed); location.setTime(time); return location; }
Example 9
Source File: MockLocationProvider.java From coursera-android-labs with MIT License | 5 votes |
public void pushLocation(double lat, double lon) { Location mockLocation = new Location(mProviderName); mockLocation.setLatitude(lat); mockLocation.setLongitude(lon); mockLocation.setAltitude(0); mockLocation.setTime(System.currentTimeMillis()); mockLocation .setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos()); mockLocation.setAccuracy(sMockAccuracy); mLocationManager.setTestProviderLocation(mProviderName, mockLocation); }
Example 10
Source File: DatabaseHelper.java From BackPackTrackII with GNU General Public License v3.0 | 5 votes |
public Location getLocation(long id) { SQLiteDatabase db = this.getReadableDatabase(); String query = "SELECT * FROM location"; query += " WHERE ID = " + id; Cursor cursor = null; try { cursor = db.rawQuery(query, new String[0]); if (cursor.moveToNext()) { int colTime = cursor.getColumnIndex("time"); int colLatitude = cursor.getColumnIndex("latitude"); int colLongitude = cursor.getColumnIndex("longitude"); int colAltitude = cursor.getColumnIndex("altitude"); int colAccuracy = cursor.getColumnIndex("accuracy"); int colName = cursor.getColumnIndex("name"); int colDeleted = cursor.getColumnIndex("deleted"); Location location = new Location(cursor.getString(colName)); // hack location.setTime(cursor.isNull(colDeleted) ? cursor.getLong(colTime) : Long.MAX_VALUE); location.setLatitude(cursor.getDouble(colLatitude)); location.setLongitude(cursor.getDouble(colLongitude)); if (!cursor.isNull(colAltitude)) location.setAltitude(cursor.getDouble(colAltitude)); if (!cursor.isNull(colAccuracy)) location.setAccuracy(cursor.getFloat(colAccuracy)); return location; } else return null; } finally { if (cursor != null) cursor.close(); } }
Example 11
Source File: SQLiteLocationDAOTest.java From background-geolocation-android with Apache License 2.0 | 5 votes |
@Test public void persistLocation() { Context ctx = InstrumentationRegistry.getTargetContext(); SQLiteDatabase db = new SQLiteOpenHelper(ctx).getWritableDatabase(); SQLiteLocationDAO dao = new SQLiteLocationDAO(db); Location location = new Location("fake"); location.setAccuracy(200); location.setAltitude(900); location.setBearing(2); location.setLatitude(40.21); location.setLongitude(23.45); location.setSpeed(20); location.setProvider("test"); location.setTime(1000); BackgroundLocation bgLocation = new BackgroundLocation(location); dao.persistLocation(bgLocation); ArrayList<BackgroundLocation> locations = new ArrayList(dao.getAllLocations()); Assert.assertEquals(1, locations.size()); BackgroundLocation storedLocation = locations.get(0); Assert.assertEquals(200, storedLocation.getAccuracy(), 0); Assert.assertEquals(900, storedLocation.getAltitude(), 0); Assert.assertEquals(2, storedLocation.getBearing(), 0); Assert.assertEquals(40.21, storedLocation.getLatitude(), 0); Assert.assertEquals(23.45, storedLocation.getLongitude(), 0); Assert.assertEquals(20, storedLocation.getSpeed(), 0); Assert.assertEquals("test", storedLocation.getProvider(), "test"); Assert.assertEquals(1000, storedLocation.getTime(), 0); Assert.assertFalse(storedLocation.hasMockLocationsEnabled()); Assert.assertFalse(storedLocation.areMockLocationsEnabled()); Assert.assertTrue(storedLocation.hasIsFromMockProvider()); // because setIsFromMockProvider is called in constructor Assert.assertFalse(storedLocation.isFromMockProvider()); }
Example 12
Source File: TriggerLocationTest.java From AndroidAPS with GNU Affero General Public License v3.0 | 5 votes |
public Location mockedLocationOut() { Location newLocation = new Location("test"); newLocation.setLatitude(12f); newLocation.setLongitude(13f); newLocation.setAccuracy(1f); return newLocation; }
Example 13
Source File: LocationHelper.java From android_external_UnifiedNlpApi with Apache License 2.0 | 5 votes |
public static Location create(String source, double latitude, double longitude, float accuracy) { Location location = create(source); location.setLatitude(latitude); location.setLongitude(longitude); location.setAccuracy(accuracy); return location; }
Example 14
Source File: LocationSourceDemoActivity.java From android-samples with Apache License 2.0 | 5 votes |
@Override public void onMapLongClick(LatLng point) { if (mListener != null && !mPaused) { Location location = new Location("LongPressLocationProvider"); location.setLatitude(point.latitude); location.setLongitude(point.longitude); location.setAccuracy(100); mListener.onLocationChanged(location); } }
Example 15
Source File: CbService.java From PressureNet-SDK with MIT License | 5 votes |
/** * Access the database to fetch recent, locally-recorded observations * * @return */ public ArrayList<CbObservation> getRecentDatabaseObservations() { ArrayList<CbObservation> recentDbList = new ArrayList<CbObservation>(); CbDb db = new CbDb(getApplicationContext()); db.open(); Cursor c = db.fetchAllObservations(); while (c.moveToNext()) { CbObservation obs = new CbObservation(); Location location = new Location("network"); location.setLatitude(c.getDouble(1)); location.setLongitude(c.getDouble(2)); location.setAltitude(c.getDouble(3)); location.setAccuracy(c.getInt(4)); location.setProvider(c.getString(5)); obs.setLocation(location); obs.setObservationType(c.getString(6)); obs.setObservationUnit(c.getString(7)); obs.setObservationValue(c.getDouble(8)); obs.setSharing(c.getString(9)); obs.setTime(c.getInt(10)); obs.setTimeZoneOffset(c.getInt(11)); obs.setUser_id(c.getString(12)); recentDbList.add(obs); } db.close(); return recentDbList; }
Example 16
Source File: MockLocationProvider.java From android_coursera_1 with MIT License | 5 votes |
public void pushLocation(double lat, double lon) { Location mockLocation = new Location(mProviderName); mockLocation.setLatitude(lat); mockLocation.setLongitude(lon); mockLocation.setAltitude(0); mockLocation.setTime(System.currentTimeMillis()); mockLocation .setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos()); mockLocation.setAccuracy(mockAccuracy); mLocationManager.setTestProviderLocation(mProviderName, mockLocation); }
Example 17
Source File: MoveService.java From together-go with GNU Lesser General Public License v3.0 | 5 votes |
public void setLocation(double longitude, double latitude) { Location location = new Location(LocationManager.GPS_PROVIDER); location.setTime(System.currentTimeMillis()); location.setLatitude(latitude); location.setLongitude(longitude); // 北京海拔大概范围,随机生成 location.setAltitude(genDouble(38.0, 50.5)); // GPS定位精度范围,随机生成 location.setAccuracy((float)genDouble(1.0, 15.0)); if (Build.VERSION.SDK_INT > 16) { location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos()); } locationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER, location); }
Example 18
Source File: BackgroundService.java From BackPackTrackII with GNU General Public License v3.0 | 4 votes |
private void handleConnectivity(Intent intent) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); // Update weather/pressure long ref_time = prefs.getLong(SettingsFragment.PREF_PRESSURE_REF_TIME, 0); int weatherInterval = Integer.parseInt(prefs.getString(SettingsFragment.PREF_WEATHER_INTERVAL, SettingsFragment.DEFAULT_WEATHER_INTERVAL)); if (ref_time + 60 * 1000L * weatherInterval < new Date().getTime()) { Intent intentWeather = new Intent(this, BackgroundService.class); intentWeather.setAction(BackgroundService.ACTION_UPDATE_WEATHER); startService(intentWeather); } boolean metered = Util.isMeteredNetwork(this); long last = prefs.getLong(SettingsFragment.PREF_LIFELINE_LAST, 0); int interval = Integer.parseInt(prefs.getString(SettingsFragment.PREF_LIFELINE_METERED_INTERVAL, SettingsFragment.DEFAULT_LIFELINE_METERED_INTERVAL)); if (!metered || interval == 0 || last + 60 * 1000L * interval < new Date().getTime()) { Log.i(TAG, "Lifeline update metered=" + metered + " interval=" + interval); // Update lifeline if (prefs.getBoolean(SettingsFragment.PREF_LIFELINE_ENABLED, SettingsFragment.DEFAULT_LIFELINE_ENABLED)) try { DatabaseHelper dh = null; Cursor cursor = null; try { dh = new DatabaseHelper(this); cursor = dh.getUnsentLocations(); int colID = cursor.getColumnIndex("ID"); int colTime = cursor.getColumnIndex("time"); int colProvider = cursor.getColumnIndex("provider"); int colLatitude = cursor.getColumnIndex("latitude"); int colLongitude = cursor.getColumnIndex("longitude"); int colAltitude = cursor.getColumnIndex("altitude"); int colAccuracy = cursor.getColumnIndex("accuracy"); int colName = cursor.getColumnIndex("name"); int colDeleted = cursor.getColumnIndex("deleted"); while (Util.isConnected(this) && (!Util.isMeteredNetwork(this) || interval == 0) && cursor.moveToNext()) { long id = cursor.getLong(colID); String name = cursor.getString(colName); Location location = new Location(cursor.getString(colProvider)); location.setTime(cursor.isNull(colDeleted) ? cursor.getLong(colTime) : Long.MAX_VALUE); location.setLatitude(cursor.getDouble(colLatitude)); location.setLongitude(cursor.getDouble(colLongitude)); if (!cursor.isNull(colAltitude)) location.setAltitude(cursor.getDouble(colAltitude)); if (!cursor.isNull(colAccuracy)) location.setAccuracy(cursor.getFloat(colAccuracy)); postLocation(id, location, name); } Log.i(TAG, "All locations sent=" + cursor.isAfterLast()); } finally { if (cursor != null) cursor.close(); if (dh != null) dh.close(); } } catch (Throwable ex) { Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); } } }
Example 19
Source File: MyTracksProviderUtilsImplTest.java From mytracks with Apache License 2.0 | 4 votes |
/** * Tests the method {@link MyTracksProviderUtilsImpl#createContentValues(Waypoint)}. */ public void testCreateContentValues_waypoint() { long trackId = System.currentTimeMillis(); Track track = getTrack(trackId, 10); providerUtils.insertTrack(track); // Bottom long startTime = 1000L; // AverageSpeed double minGrade = -20.11; TripStatistics statistics = new TripStatistics(); statistics.setStartTime(startTime); statistics.setStopTime(2500L); statistics.setTotalTime(1500L); statistics.setMovingTime(700L); statistics.setTotalDistance(750.0); statistics.setTotalElevationGain(50.0); statistics.setMaxSpeed(60.0); statistics.setMaxElevation(1250.0); statistics.setMinElevation(1200.0); statistics.setMaxGrade(15.0); statistics.setMinGrade(minGrade); statistics.setBounds(-10000, 20000, 30000, -40000); // Insert at first. Waypoint waypoint = new Waypoint(); waypoint.setDescription(TEST_DESC); waypoint.setType(WaypointType.STATISTICS); waypoint.setTripStatistics(statistics); Location loc = new Location("test"); loc.setLatitude(22); loc.setLongitude(22); loc.setAccuracy((float) 1 / 100.0f); loc.setAltitude(2.5); waypoint.setLocation(loc); providerUtils.insertWaypoint(waypoint); MyTracksProviderUtilsImpl myTracksProviderUtilsImpl = new MyTracksProviderUtilsImpl( new MockContentResolver()); long waypointId = System.currentTimeMillis(); waypoint.setId(waypointId); ContentValues contentValues = myTracksProviderUtilsImpl.createContentValues(waypoint); assertEquals(waypointId, contentValues.get(WaypointsColumns._ID)); assertEquals(22 * 1000000, contentValues.get(WaypointsColumns.LONGITUDE)); assertEquals(TEST_DESC, contentValues.get(WaypointsColumns.DESCRIPTION)); assertEquals(startTime, contentValues.get(WaypointsColumns.STARTTIME)); assertEquals(minGrade, contentValues.get(WaypointsColumns.MINGRADE)); }
Example 20
Source File: InstrumentationService.java From sana.mobile with BSD 3-Clause "New" or "Revised" License | 4 votes |
@SuppressLint("NewApi") public void handleMessage(Message msg) { Log.i(TAG, "msg obj = " + String.valueOf(msg.obj)); switch (msg.what) { case MSG_GET_LOCATION: Uri uri = Uri.parse(msg.obj.toString()); if (msg.getData() != null) { //Intent reply = msg.getData().getParcelable(Intent.EXTRA_INTENT); PendingIntent replyTo = msg.getData().getParcelable(Intent.EXTRA_INTENT); //Log.d(TAG, "replyTo: " + String.valueOf(replyTo)); LocationListener listener = getListener(null, uri, msg.arg1, msg.getData()); try { Criteria criteria = new Criteria(); if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.FROYO) { criteria.setPowerRequirement(Criteria.POWER_HIGH); criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH); } else { criteria.setAccuracy(Criteria.ACCURACY_FINE); } List<String> providers = locationManager.getProviders(criteria, true); for (String provider : providers) { Log.d(TAG, "Using location provider: " + provider); locationManager.requestLocationUpdates(provider, 0, 0, listener); } //if(TextUtils.isEmpty(provider)) // throw new IllegalArgumentException("No location providers available"); // add to our listeners so that we can clean up later if necessary //replies.put(msg.arg1, replyTo); if (providers.size() == 0) { Location nullLocation = new Location(LocationManager.GPS_PROVIDER); nullLocation.setAccuracy(0); nullLocation.setLatitude(0); nullLocation.setLongitude(0); listener.onLocationChanged(nullLocation); } else { listeners.put(msg.arg1, listener); } } catch (Exception e) { Log.e(TAG, "Error getting location updates: " + e.getMessage()); e.printStackTrace(); removeListener(msg.arg1); } } else { Log.w(TAG, "no replyTo in original intent sent to InstrumentationService"); removeListener(msg.arg1); } break; default: Log.w(TAG, "Unknown message! Message = " + msg.what); removeListener(msg.arg1); } }