List of usage examples for android.location LocationManager getLastKnownLocation
@RequiresPermission(anyOf = { ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION })
public Location getLastKnownLocation(String provider)
From source file:mx.developerbus.foodbus.FoodBus_Main.java
public LatLng getLocation() { // Get the location manager LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); Criteria criteria = new Criteria(); String bestProvider = locationManager.getBestProvider(criteria, false); Location location = locationManager.getLastKnownLocation(bestProvider); Double lat, lon;//from w w w . j ava 2 s . c om try { lat = location.getLatitude(); lon = location.getLongitude(); return new LatLng(lat, lon); } catch (NullPointerException e) { e.printStackTrace(); return null; } }
From source file:digital.dispatch.TaxiLimoNewUI.GCM.GCMIntentService.java
private Location getLocationByProvider(String provider) { Location location = null;//from www . ja v a 2 s. com LocationManager locationManager = (LocationManager) c.getSystemService(Context.LOCATION_SERVICE); try { if (locationManager.isProviderEnabled(provider)) { location = locationManager.getLastKnownLocation(provider); } } catch (IllegalArgumentException e) { Logger.d(TAG, "Cannot acces Provider " + provider); } return location; }
From source file:com.jeremy.tripcord.main.TripcordFragment.java
private CameraUpdate getLastKnownLocation() { LocationManager lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_LOW); String provider = lm.getBestProvider(criteria, true); if (provider == null) { return null; }//from ww w.ja v a 2s .c o m Location loc = lm.getLastKnownLocation(provider); if (loc != null) { return CameraUpdateFactory.newCameraPosition( CameraPosition.fromLatLngZoom(new LatLng(loc.getLatitude(), loc.getLongitude()), 14.0f)); } return null; }
From source file:com.example.scrumptious.PickerActivity.java
@Override protected void onStart() { super.onStart(); if (FRIEND_PICKER.equals(getIntent().getData())) { try {//from w w w . jav a 2 s . c om friendPickerFragment.loadData(false); } catch (Exception ex) { onError(ex); } } else if (PLACE_PICKER.equals(getIntent().getData())) { try { Location location = null; Criteria criteria = new Criteria(); LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); String bestProvider = locationManager.getBestProvider(criteria, false); if (bestProvider != null) { location = locationManager.getLastKnownLocation(bestProvider); if (locationManager.isProviderEnabled(bestProvider) && locationListener == null) { locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { boolean updateLocation = true; Location prevLocation = placePickerFragment.getLocation(); if (prevLocation != null) { updateLocation = location.distanceTo(prevLocation) >= LOCATION_CHANGE_THRESHOLD; } if (updateLocation) { placePickerFragment.setLocation(location); placePickerFragment.loadData(true); } } @Override public void onStatusChanged(String s, int i, Bundle bundle) { } @Override public void onProviderEnabled(String s) { } @Override public void onProviderDisabled(String s) { } }; locationManager.requestLocationUpdates(bestProvider, 1, LOCATION_CHANGE_THRESHOLD, locationListener, Looper.getMainLooper()); } } if (location != null) { placePickerFragment.setLocation(location); placePickerFragment.setRadiusInMeters(SEARCH_RADIUS_METERS); placePickerFragment.setSearchText(SEARCH_TEXT); placePickerFragment.setResultsLimit(SEARCH_RESULT_LIMIT); placePickerFragment.loadData(false); } } catch (Exception ex) { onError(ex); } } }
From source file:org.ohmage.triggers.base.TriggerBase.java
public void notifyTrigger(Context context, int trigId) { Log.i(DEBUG_TAG, "TriggerBase: notifyTrigger(" + trigId + ")"); TriggerDB db = new TriggerDB(context); db.open();//from w ww.j a v a 2s.c o m String rtDesc = db.getRunTimeDescription(trigId); TriggerRunTimeDesc desc = new TriggerRunTimeDesc(); desc.loadString(rtDesc); //Save trigger time stamp in the run time description desc.setTriggerTimeStamp(System.currentTimeMillis()); //Save trigger current loc in the run time description LocationManager locMan = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); Location loc = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER); desc.setTriggerLocation(loc); //Save the run time desc in the database db.updateRunTimeDescription(trigId, desc.toString()); //Call the notifier to display the notification //Pass the notification description corresponding to this trigger Notifier.notifyNewTrigger(context, trigId, db.getNotifDescription(trigId)); db.close(); }
From source file:com.example.snapcacheexample.PickerActivity.java
@Override protected void onStart() { super.onStart(); if (FRIEND_PICKER.equals(getIntent().getData())) { try {/* w w w .j a v a 2s. c o m*/ friendPickerFragment.loadData(false); } catch (Exception ex) { onError(ex); } } else if (PLACE_PICKER.equals(getIntent().getData())) { try { Location location = null; Criteria criteria = new Criteria(); LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); String bestProvider = locationManager.getBestProvider(criteria, false); if (bestProvider != null) { location = locationManager.getLastKnownLocation(bestProvider); if (locationManager.isProviderEnabled(bestProvider) && locationListener == null) { locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { float distance = location.distanceTo(placePickerFragment.getLocation()); if (distance >= LOCATION_CHANGE_THRESHOLD) { placePickerFragment.setLocation(location); placePickerFragment.loadData(true); } } @Override public void onStatusChanged(String s, int i, Bundle bundle) { } @Override public void onProviderEnabled(String s) { } @Override public void onProviderDisabled(String s) { } }; locationManager.requestLocationUpdates(bestProvider, 1, LOCATION_CHANGE_THRESHOLD, locationListener, Looper.getMainLooper()); } } if (location == null) { String model = Build.MODEL; if (model.equals("sdk") || model.equals("google_sdk") || model.contains("x86")) { // this may be the emulator, pretend we're in an exotic place location = SAN_FRANCISCO_LOCATION; } } if (location != null) { placePickerFragment.setLocation(location); placePickerFragment.setRadiusInMeters(SEARCH_RADIUS_METERS); placePickerFragment.setSearchText(SEARCH_TEXT); placePickerFragment.setResultsLimit(SEARCH_RESULT_LIMIT); placePickerFragment.loadData(false); } else { onError(getResources().getString(R.string.no_location_error), true); } } catch (Exception ex) { onError(ex); } } }
From source file:com.example.david.wheretogo_test1.PickerActivity.java
@Override protected void onStart() { super.onStart(); if (FRIEND_PICKER.equals(getIntent().getData())) { try {// w ww . ja va2s. c o m friendPickerFragment.loadData(false); } catch (Exception ex) { onError(ex); } } else if (PLACE_PICKER.equals(getIntent().getData())) { try { Location location = null; Criteria criteria = new Criteria(); LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); String bestProvider = locationManager.getBestProvider(criteria, false); if (bestProvider != null) { location = locationManager.getLastKnownLocation(bestProvider); if (locationManager.isProviderEnabled(bestProvider) && locationListener == null) { locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { boolean updateLocation = true; Location prevLocation = placePickerFragment.getLocation(); if (prevLocation != null) { updateLocation = location.distanceTo(prevLocation) >= LOCATION_CHANGE_THRESHOLD; } if (updateLocation) { placePickerFragment.setLocation(location); placePickerFragment.loadData(true); } } @Override public void onStatusChanged(String s, int i, Bundle bundle) { } @Override public void onProviderEnabled(String s) { } @Override public void onProviderDisabled(String s) { } }; locationManager.requestLocationUpdates(bestProvider, 1, LOCATION_CHANGE_THRESHOLD, locationListener, Looper.getMainLooper()); } } if (location == null) { String model = Build.MODEL; if (model.equals("sdk") || model.equals("google_sdk") || model.contains("x86")) { // this may be the emulator, pretend we're in an exotic place location = SAN_FRANCISCO_LOCATION; } } if (location != null) { placePickerFragment.setLocation(location); placePickerFragment.setRadiusInMeters(SEARCH_RADIUS_METERS); placePickerFragment.setSearchText(SEARCH_TEXT); placePickerFragment.setResultsLimit(SEARCH_RESULT_LIMIT); placePickerFragment.loadData(false); } else { onError(getResources().getString(R.string.no_location_error), true); } } catch (Exception ex) { onError(ex); } } }
From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.webview.CurrentPosition.java
public CurrentPosition() { Prefs prefs = GBApplication.getPrefs(); this.latitude = prefs.getFloat("location_latitude", 0); this.longitude = prefs.getFloat("location_longitude", 0); lastKnownLocation = new Location("preferences"); lastKnownLocation.setLatitude(this.latitude); lastKnownLocation.setLongitude(this.longitude); LOG.info("got longitude/latitude from preferences: " + latitude + "/" + longitude); this.timestamp = System.currentTimeMillis() - 86400000; //let accessor know this value is really old if (ActivityCompat.checkSelfPermission(GBApplication.getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED && prefs.getBoolean("use_updated_location_if_available", false)) { LocationManager locationManager = (LocationManager) GBApplication.getContext() .getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); String provider = null;/* ww w . ja v a2 s . c o m*/ if (locationManager != null) { provider = locationManager.getBestProvider(criteria, false); } if (provider != null) { Location lastKnownLocation = locationManager.getLastKnownLocation(provider); if (lastKnownLocation != null) { this.lastKnownLocation = lastKnownLocation; this.timestamp = lastKnownLocation.getTime(); this.timestamp = System.currentTimeMillis() - 1000; //TODO: request updating the location and don't fake its age this.latitude = (float) lastKnownLocation.getLatitude(); this.longitude = (float) lastKnownLocation.getLongitude(); this.accuracy = lastKnownLocation.getAccuracy(); this.altitude = (float) lastKnownLocation.getAltitude(); this.speed = lastKnownLocation.getSpeed(); } } } }
From source file:com.adwhirl.AdWhirlManager.java
public Location getLocation() { if (contextReference == null) { return null; }// ww w .j av a 2 s.c o m Context context = contextReference.get(); if (context == null) { return null; } Location location = null; if (context.checkCallingOrSelfPermission( android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); } else if (context.checkCallingOrSelfPermission( android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); } return location; }
From source file:com.nextgis.maplibui.fragment.CompassFragment.java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (null == getParentFragment()) { setRetainInstance(true);//www .j a v a 2s . co m } // reference to vibrator service mDeclination = 0; mVibrator = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE); mSensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE); if (!PermissionUtil.hasPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) || !PermissionUtil.hasPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION)) return; if (mCurrentLocation == null) { LocationManager locationManager = (LocationManager) getActivity() .getSystemService(Context.LOCATION_SERVICE); mCurrentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (mCurrentLocation == null) { mCurrentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); } } if (mCurrentLocation != null) mDeclination = getDeclination(mCurrentLocation, System.currentTimeMillis()); }