mapbox-maps-android
mapbox-maps-android copied to clipboard
How to get the current walking speed
locationEngine = LocationEngineProvider.getBestLocationEngine(this)
var request = LocationEngineRequest.Builder(DEFAULT_INTERVAL_IN_MILLISECONDS)
.setPriority(LocationEngineRequest.PRIORITY_HIGH_ACCURACY)
.setMaxWaitTime(DEFAULT_MAX_WAIT_TIME)
.build()
locationEngine.requestLocationUpdates(request, callback, mainLooper)
locationEngine.getLastLocation(callback)
private class LocationListeningCallback internal constructor(activity: RealTimeDrawLineEnhanceActivity) :
LocationEngineCallback<LocationEngineResult> {
private val activityWeakReference: WeakReference<RealTimeDrawLineEnhanceActivity>
init {
this.activityWeakReference = WeakReference(activity)
}
@RequiresApi(Build.VERSION_CODES.O)
override fun onSuccess(result: LocationEngineResult) {
// The LocationEngineCallback interface's method which fires when the device's location has changed. if (result.lastLocation?.speed!! > 0f || result.lastLocation!!.speedAccuracyMetersPerSecond > 0f) { activityWeakReference.get()?.tvSpeed?.text = result.lastLocation?.speed.toString() activityWeakReference.get()?.tvSpeed2?.text = result.lastLocation?.speedAccuracyMetersPerSecond.toString() } result.lastLocation?.let { activityWeakReference.get() ?.drawLine(it, result.lastLocation!!.speedAccuracyMetersPerSecond.toString()) } }
/**
* The LocationEngineCallback interface's method which fires when the device's location can not be captured
*
* @param exception the exception message
*/
override fun onFailure(exception: Exception) {
// The LocationEngineCallback interface's method which fires when the device's location can not be captured
}
}
I want to get the current walking speed, but the speed I get through the above method is 0 .
Is it something I missed?
@afterlifehy, as you're using the built in locationEngine, you would get location object and use getSpeed() method. Please feel free to take a look at https://developer.android.com/reference/android/location/Location
@afterlifehy, as you're using the built in locationEngine, you would get location object and use
getSpeed()method. Please feel free to take a look at https://developer.android.com/reference/android/location/Location
Yes, I used the getspeed () method, but the speed is always 0.My code is above
why my result.lastLocation.hasSpeed is false
I found the answer,my device is android 12 and supports Google play location service, the provider is fused provider ,so i need to "implementation("com.google.android.gms:play-services-location:18.0.0")",now my speed is not 0 .
@NonNull public static LocationEngine getBestLocationEngine(@NonNull Context context) { checkNotNull(context, "context == null");
boolean hasGoogleLocationServices = isOnClasspath(GOOGLE_LOCATION_SERVICES);
if (isOnClasspath(GOOGLE_API_AVAILABILITY)) {
// Check Google Play services APK is available and up-to-date on this device
hasGoogleLocationServices &= GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context)
== ConnectionResult.SUCCESS;
}
return getLocationEngine(context, hasGoogleLocationServices);
}
private static LocationEngine getLocationEngine(Context context, boolean isGoogle) { return isGoogle ? new LocationEngineProxy<>(new GoogleLocationEngineImpl(context.getApplicationContext())) : new LocationEngineProxy<>(new MapboxFusedLocationEngineImpl(context.getApplicationContext())); }
@afterlifehy, I'm happy you were able to get it resolved! Closing the ticket, but please feel free to reach back out if you have additional questions.