Created
January 12, 2019 18:38
-
-
Save DINESHKARPE/d086bab31fcb4a8cda824a8501d21012 to your computer and use it in GitHub Desktop.
Android fused location API with LocationCallback
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AppConstant { | |
public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 1000; | |
public static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.location.Location; | |
public interface PositionListener { | |
void onPositionUpdate(Location position); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.annotation.SuppressLint; | |
import android.content.Context; | |
import android.os.Looper; | |
import com.google.android.gms.location.FusedLocationProviderClient; | |
import com.google.android.gms.location.LocationServices; | |
public class TurnByTrunFusedLocationProvider { | |
private Context context; | |
private FusedLocationProviderClient fusedLocationProvider; | |
private TurnByTurnLocationCallBack turnByTurnLocationCallBack; | |
/** | |
* TurnByTrunFusedLocationProvider required context and TurnByTurnLocationCallBack as parameter | |
*/ | |
public TurnByTrunFusedLocationProvider(Context context,TurnByTurnLocationCallBack turnByTurnLocationCallBack){ | |
fusedLocationProvider = LocationServices.getFusedLocationProviderClient(context); | |
this.turnByTurnLocationCallBack = turnByTurnLocationCallBack; | |
this.context = context; | |
} | |
/** | |
* Start Location Update | |
*/ | |
public void startUpdates() { | |
try { | |
fusedLocationProvider.requestLocationUpdates(Utility.fetchLocationRequest(),turnByTurnLocationCallBack , Looper.myLooper()); | |
} catch (RuntimeException e) { | |
} | |
} | |
/** | |
* Stop Location Update | |
*/ | |
public void stopUpdate(){ | |
if(fusedLocationProvider != null){ | |
fusedLocationProvider.removeLocationUpdates(turnByTurnLocationCallBack); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.content.Intent; | |
import android.location.Location; | |
import android.support.v4.content.LocalBroadcastManager; | |
import android.widget.Toast; | |
import com.google.android.gms.location.LocationAvailability; | |
import com.google.android.gms.location.LocationCallback; | |
import com.google.android.gms.location.LocationResult; | |
import java.util.List; | |
public class TurnByTurnLocationCallBack extends LocationCallback { | |
private static String TAG = TurnByTurnLocationCallBack.class.getCanonicalName(); | |
private static final String PACKAGE_NAME = TurnByTurnLocationCallBack.class.getCanonicalName(); | |
public static final String EXTRA_LOCATION = PACKAGE_NAME + ".location"; | |
private PositionListener positionListener; | |
public static final String ACTION_BROADCAST = PACKAGE_NAME + ".broadcast"; | |
private Context context; | |
private Location oldLocation; | |
public TurnByTurnLocationCallBack() { | |
super(); | |
} | |
public TurnByTurnLocationCallBack(Context context, PositionListener positionListener) { | |
this.positionListener = positionListener; | |
this.context = context; | |
} | |
@Override | |
public void onLocationResult(LocationResult locationResult) { | |
super.onLocationResult(locationResult); | |
List<Location> locationList = locationResult.getLocations(); | |
Location location = locationList.get(locationList.size() - 1); | |
positionListener.onPositionUpdate(location); | |
Intent intent = new Intent(ACTION_BROADCAST); | |
intent.putExtra(EXTRA_LOCATION, location); | |
LocalBroadcastManager.getInstance(context).sendBroadcast(intent); | |
} | |
@Override | |
public void onLocationAvailability(LocationAvailability locationAvailability) { | |
super.onLocationAvailability(locationAvailability); | |
} | |
/** | |
* Make use of location after deciding if it is better than previous one. | |
* | |
* @param location Newly acquired location. | |
*/ | |
void doWorkWithNewLocation(Location location) { | |
} | |
/** | |
* Time difference threshold set for one minute. | |
*/ | |
static final int TIME_DIFFERENCE_THRESHOLD = 1 * 60 * 1000; | |
/** | |
* Decide if new location is better than older by following some basic criteria. | |
* This algorithm can be as simple or complicated as your needs dictate it. | |
* Try experimenting and get your best location strategy algorithm. | |
* | |
* @param oldLocation Old location used for comparison. | |
* @param newLocation Newly acquired location compared to old one. | |
* @return If new location is more accurate and suits your criteria more than the old one. | |
*/ | |
boolean isBetterLocation(Location oldLocation, Location newLocation) { | |
// If there is no old location, of course the new location is better. | |
if(oldLocation == null) { | |
return true; | |
} | |
// Check if new location is newer in time. | |
boolean isNewer = newLocation.getTime() > oldLocation.getTime(); | |
// Check if new location more accurate. Accuracy is radius in meters, so less is better. | |
boolean isMoreAccurate = newLocation.getAccuracy() < oldLocation.getAccuracy(); | |
if(isMoreAccurate && isNewer) { | |
// More accurate and newer is always better. | |
return true; | |
} else if(isMoreAccurate && !isNewer) { | |
// More accurate but not newer can lead to bad fix because of user movement. | |
// Let us set a threshold for the maximum tolerance of time difference. | |
long timeDifference = newLocation.getTime() - oldLocation.getTime(); | |
// If time difference is not greater then allowed threshold we accept it. | |
if(timeDifference > -TIME_DIFFERENCE_THRESHOLD) { | |
return true; | |
} | |
} | |
return false; | |
} | |
public Location getOldLocation() { | |
return oldLocation; | |
} | |
public void setOldLocation(Location oldLocation) { | |
this.oldLocation = oldLocation; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Utility { | |
public static LocationRequest fetchLocationRequest(){ | |
LocationRequest mLocationRequest = new LocationRequest(); | |
mLocationRequest.setInterval(AppConstant.UPDATE_INTERVAL_IN_MILLISECONDS); | |
mLocationRequest.setFastestInterval(AppConstant.FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS); | |
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); | |
return mLocationRequest; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment