mapbox-maps-android icon indicating copy to clipboard operation
mapbox-maps-android copied to clipboard

How to get the current walking speed

Open afterlifehy opened this issue 3 years ago • 4 comments

    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 avatar Jul 25 '22 02:07 afterlifehy

@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

ZiZasaurus avatar Jul 26 '22 12:07 ZiZasaurus

@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

afterlifehy avatar Jul 27 '22 03:07 afterlifehy

why my result.lastLocation.hasSpeed is false

afterlifehy avatar Jul 28 '22 02:07 afterlifehy

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 avatar Jul 28 '22 12:07 afterlifehy

@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.

ZiZasaurus avatar Sep 12 '22 12:09 ZiZasaurus