ListenerRegistration(String provider, long minTime, float minDistance, Location locationAtCreation, LocationListener listener) { this.provider = provider; this.minTime = minTime; this.minDistance = minDistance; this.lastSeenTime = locationAtCreation == null ? 0 : locationAtCreation.getTime(); this.lastSeenLocation = locationAtCreation; this.listener = listener; } }
LocationManager manager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER) && !manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { locationEnabled = false; Toast.makeText(getActivity(), "Enable location services for accurate data", Toast.LENGTH_SHORT).show(); } else locationEnabled = true;
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE); try { Log.d(TAG ,"Removing Test providers") lm.removeTestProvider(LocationManager.GPS_PROVIDER); } catch (IllegalArgumentException error) { Log.d(TAG,"Got exception in removing test provider"); } lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
private Location copyOf(Location location) { if (location == null) return null; Location copy = new Location(location); copy.setAccuracy(location.getAccuracy()); copy.setAltitude(location.getAltitude()); copy.setBearing(location.getBearing()); copy.setExtras(location.getExtras()); copy.setLatitude(location.getLatitude()); copy.setLongitude(location.getLongitude()); copy.setProvider(location.getProvider()); copy.setSpeed(location.getSpeed()); copy.setTime(location.getTime()); return copy; }
@JavascriptInterface public String getLocation() throws JSONException { Location loc=myLocationManager.getLastKnownLocation(PROVIDER); if (loc == null) { return(null); } JSONObject json=new JSONObject(); json.put("lat", loc.getLatitude()); json.put("lon", loc.getLongitude()); return(json.toString()); } }
public static Point markLocation(MapView mMapView, Location location, GraphicsOverlay gLayerPos, PictureMarkerSymbol mMarkerSymbol, boolean isGPS, boolean isCenter) { Gps gps = getGps(location.getLongitude(), location.getLatitude(), isGPS); return markLocation(mMapView, gps, gLayerPos, mMarkerSymbol, isCenter); }
public TrackLocation(){ isRunning=true; location=new Location("not defined"); location.setLatitude(0); location.setLongitude(0); } @Override
LocationManager mLocationManager; Location myLocation = getLastKnownLocation(); private Location getLastKnownLocation() { mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE); List<String> providers = mLocationManager.getProviders(true); Location bestLocation = null; for (String provider : providers) { Location l = mLocationManager.getLastKnownLocation(provider); if (l == null) { continue; } if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) { // Found best last known location: %s", l); bestLocation = l; } } return bestLocation; }
@Test public void shouldReturnLastKnownLocationForAProvider() throws Exception { assertNull(locationManager.getLastKnownLocation(NETWORK_PROVIDER)); Location networkLocation = new Location(NETWORK_PROVIDER); Location gpsLocation = new Location(GPS_PROVIDER); shadowLocationManager.setLastKnownLocation(NETWORK_PROVIDER, networkLocation); shadowLocationManager.setLastKnownLocation(GPS_PROVIDER, gpsLocation); assertSame(locationManager.getLastKnownLocation(NETWORK_PROVIDER), networkLocation); assertSame(locationManager.getLastKnownLocation(GPS_PROVIDER), gpsLocation); }
private Location getLastLocation(String provider) { Location lastLocation = locationManager.getLastKnownLocation(provider); if (lastLocation != null) { long lastLocationTs = lastLocation.getTime(); long locationAgeMs = System.currentTimeMillis() - lastLocationTs; if (locationAgeMs < params.getLastLocationMaxAgeMs()) { return lastLocation; } } return null; }
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); // Define a listener that responds to location updates LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { // Called when a new location is found by the network location provider. makeUseOfNewLocation(location); } public void onStatusChanged(String provider, int status, Bundle extras) {} public void onProviderEnabled(String provider) {} public void onProviderDisabled(String provider) {} }; // Register the listener with the Location Manager to receive location updates locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
@Override public void onLocationChanged(Location location) { locationManager.removeUpdates(this); }
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); double longitude = location.getLongitude(); double latitude = location.getLatitude();
@Test public void getBestProvider_returnsProviderBasedOnCriteriaAndEnabledState() throws Exception { Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_COARSE); assertThat(locationManager.getBestProvider(null, false)).isEqualTo(LocationManager.GPS_PROVIDER); assertThat(locationManager.getBestProvider(null, true)).isNull(); assertThat(locationManager.getBestProvider(criteria, false)).isEqualTo(LocationManager.NETWORK_PROVIDER); assertThat(locationManager.getBestProvider(criteria, true)).isNull(); }
@Test public void shouldStoreBestProviderCriteriaAndEnabledOnlyFlag() throws Exception { Criteria criteria = new Criteria(); assertNull(locationManager.getBestProvider(criteria, true)); assertSame(criteria, shadowLocationManager.getLastBestProviderCriteria()); assertTrue(shadowLocationManager.getLastBestProviderEnabledOnly()); }
private Listener addGpsListenerToLocationManager() { Listener listener = new TestGpsListener(); locationManager.addGpsStatusListener(listener); return listener; }
@JavascriptInterface public String getLocation() throws JSONException { Location loc=myLocationManager.getLastKnownLocation(PROVIDER); if (loc==null) { return(null); } JSONObject json=new JSONObject(); json.put("lat", loc.getLatitude()); json.put("lon", loc.getLongitude()); return(json.toString()); } }
final LocationManager manager = (LocationManager)context.getSystemService (Context.LOCATION_SERVICE ); if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) Toast.makeText(context, "GPS is disabled!", Toast.LENGTH_LONG).show(); else Toast.makeText(context, "GPS is enabled!", Toast.LENGTH_LONG).show();
// Acquire a reference to the system Location Manager LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); // Define a listener that responds to location updates LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { // Called when a new location is found by the network location provider. makeUseOfNewLocation(location); } public void onStatusChanged(String provider, int status, Bundle extras) {} public void onProviderEnabled(String provider) {} public void onProviderDisabled(String provider) {} }; // Register the listener with the Location Manager to receive location updates locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
/** * @return the last know best location */ private Location getLastBestLocation() { Location locationGPS = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); Location locationNet = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); long GPSLocationTime = 0; if (null != locationGPS) { GPSLocationTime = locationGPS.getTime(); } long NetLocationTime = 0; if (null != locationNet) { NetLocationTime = locationNet.getTime(); } if ( 0 < GPSLocationTime - NetLocationTime ) { return locationGPS; } else { return locationNet; } }