Best way to get user GPS location in background in Android [duplicate]

None of the rest of the answers use: com.google.android.gms.location.FusedLocationProviderClient Which is the Fused Location Provider and the main entry point for interacting with the fused location provider by Google, and it is very hard to find a good example. This was released mid 2017 by Google. Google Play services location APIs are preferred over the … Read more

How to get current location in Android [duplicate]

First you need to define a LocationListener to handle location changes. int LOCATION_REFRESH_TIME = 15000; // 15 seconds to update int LOCATION_REFRESH_DISTANCE = 500; // 500 meters to update …. private final LocationListener mLocationListener = new LocationListener() { @Override public void onLocationChanged(final Location location) { //your code here } }; Then get the LocationManager and … Read more

Haversine Formula in Python (Bearing and Distance between two GPS points)

Here’s a Python version: from math import radians, cos, sin, asin, sqrt def haversine(lon1, lat1, lon2, lat2): “”” Calculate the great circle distance in kilometers between two points on the earth (specified in decimal degrees) “”” # convert decimal degrees to radians lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2]) # haversine formula … Read more

How to mock location on device?

It seems the only way to do is to use a mock location provider. You have to enable mock locations in the development panel in your settings and add <uses-permission android:name=”android.permission.ACCESS_MOCK_LOCATION” /> to your manifest. Now you can go in your code and create your own mock location provider and set the location of this … Read more

Smooth GPS data

Here’s a simple Kalman filter that could be used for exactly this situation. It came from some work I did on Android devices. General Kalman filter theory is all about estimates for vectors, with the accuracy of the estimates represented by covariance matrices. However, for estimating location on Android devices the general theory reduces to … Read more

How can I enable or disable the GPS programmatically on Android?

the GPS can be toggled by exploiting a bug in the power manager widget. see this xda thread for discussion. here’s some example code i use private void turnGPSOn(){ String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if(!provider.contains(“gps”)){ //if gps is disabled final Intent poke = new Intent(); poke.setClassName(“com.android.settings”, “com.android.settings.widget.SettingsAppWidgetProvider”); poke.addCategory(Intent.CATEGORY_ALTERNATIVE); poke.setData(Uri.parse(“3”)); sendBroadcast(poke); } } private void turnGPSOff(){ … Read more

Android : LocationManager vs Google Play Services

User Location on Android Getting the user’s location on Android is a little less straightforward than on iOS. To start the confusion, there are two totally different ways you can do it. The first is using Android APIs from android.location.LocationListener, and the second is using Google Play Services APIs com.google.android.gms.location.LocationListener. Let’s go through both of … Read more

How do I find out if the GPS of an Android device is enabled

Best way seems to be the following: final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE ); if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) { buildAlertMessageNoGps(); } private void buildAlertMessageNoGps() { final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(“Your GPS seems to be disabled, do you want to enable it?”) .setCancelable(false) .setPositiveButton(“Yes”, new DialogInterface.OnClickListener() { public void onClick(@SuppressWarnings(“unused”) final … Read more