1
votes

I have looked at the links but cannot find a simple example of reverse geocoding using the Mapbox API.

Does anybody have a link to a simple partial address input and a response?

3

3 Answers

3
votes

Why wouldn't the example's in: https://docs.mapbox.com/api/search/#reverse-geocoding work for you ?

At least when trying those quickly with browser, they return nice JSON reply. So for Reply, just copy paste the url into your browser, and let it load the response.

0
votes

Try this my friend, point is a type you can construct easily with Point.fromLngLat(point.longitude(), point.latitude())

   private fun reverseGeocode(point: Point) {
    try {
        val client = MapboxGeocoding.builder()
            .accessToken(getString(R.string.mapbox_access_token))
            .query(Point.fromLngLat(point.longitude(), point.latitude()))
            .geocodingTypes(GeocodingCriteria.TYPE_ADDRESS)
            .build()

        client.enqueueCall(object : Callback<GeocodingResponse>
        {
            override fun onResponse(call: Call<GeocodingResponse>, response: Response<GeocodingResponse>) {
                if (response.body() != null) {
                    val results = response.body()!!.features()
                    if (results.size > 0) {
                        val feature = results.get(0)
                        Log.d(TAG, "lig Current location is ${feature.placeName()}")
                    } else {
                        Log.d(TAG, "lig Current location is nothing")
                    }
                }
            }
            override fun onFailure(call: Call<GeocodingResponse>, throwable: Throwable) {
                Log.e(TAG, "Geocoding Failure", throwable)
            }
        })
    } catch (servicesException: ServicesException) {
        Log.e(TAG, servicesException.toString())
        servicesException.printStackTrace()
    }
}