androidkotlinmapboxmapbox-androiddirections

Mapbox Directions API response body always returns null in Android after migrating from Mapbox SDK 9 to 11


Mapbox Directions API's response.body() always returns null. I've checked my access token and I'm using the same access token that I used in my old Mapbox 9 project (which still works and shows the route line properly) but my Directions API call seem to not work on Mapbox 11.

I'm assuming I'm not using the updated API correctly since a lot has changed on the available methods in Directions API used in mapbox 11. For example, this is how a route line was requested in mapbox 9: https://docs.mapbox.com/android/java/guides/directions/

But the methods .origin and .destination have been replaced by .routeOptions in Mapbox 11 where I need to create a RouteOptions object and pass the origin and destination to the .coordinatesList method.

The above changes are just a few of the changes I made to my old code as I also migrated it to use Kotlin instead as its the one that's mainly used in Mapbox 11.

I got my code from the example STORE LOCATOR of Mapbox which is in Java and Mapbox 9 and just modified it to use Kotlin and Mapbox 11 while also using the latest Standard 3D Map but as mentioned, I didn't get to make these parts work as the Mapbox Directions API request always returns null:

private fun getInformationFromDirectionsApi(
        destinationPoint: Point,
        fromMarkerClick: Boolean, @Nullable listIndex: Int?
    ) {
        val destinationMarker = Point.fromLngLat(destinationPoint.longitude(), destinationPoint.latitude())

        // Initialize the directionsApiClient object for eventually drawing a navigation route on the map
        // TODO: Replace mockOriginLocation with getDeviceLocation
        val mockOriginLocation = Point.fromLngLat(120.59270718466132, 16.418361457286892)

        // USED THE NEW ROUTEOPTIONS OBJECT
        val routeOptions: RouteOptions =
            RouteOptions.builder().applyDefaultNavigationOptions()
                .profile(DirectionsCriteria.PROFILE_WALKING)
                .coordinatesList(listOf(mockOriginLocation, destinationMarker)).build()

        // REQUEST THE ROUTE 
        val directionsApiClient: MapboxDirections =
            MapboxDirections.builder().routeOptions(routeOptions).accessToken(R.string.mapbox_access_token.toString())
                .build()

        directionsApiClient.enqueueCall(object : Callback<DirectionsResponse?> {
            override fun onResponse(call: Call<DirectionsResponse?>, response: Response<DirectionsResponse?>) {
                // Check that the response isn't null and that the response has a route
                // THIS IS ALWAYS TRUE AS RESPONSE BODY IS ALWAYS NULL
                if (response.body() == null) {
                    // THIS ALWAYS GETS LOGGED
                    Log.e("MapFragment", "No routes found, make sure you set the right user and access token.")
                } else if (response.body()!!.routes().size < 1) {
                    Log.e("MapFragment", "No routes found")
                } else {

                    ...

                }
            }

            ...

        })
    }

Solution

  • I used the getString() method instead of the toString() method to retrieve the access token

    enter image description here