Mapbox reverse geocoding does not always return a result

Im trying reverse geocoding example on mapbox

Everything's working fine. But the problem is when i replace my current location it doesnt return a result. I think that mapbox doesnt have much data about my region (Im in Vietnam). Is there anyway to add my own places supposed that I had a dataset of coordinate and address

My code:

mapboxGeocoding = MapboxGeocoding.builder()
                .accessToken(getString(R.string.mapbox_access_token))
                .query(Point.fromLngLat(106.799142,10.875838))
                .geocodingTypes(GeocodingCriteria.TYPE_ADDRESS)
                .build();;

        mapboxGeocoding.enqueueCall(new Callback<GeocodingResponse>() {
            @Override
            public void onResponse(Call<GeocodingResponse> call, Response<GeocodingResponse> response) {

                List<CarmenFeature> results = response.body().features();

                if (results.size() > 0) {

                    // Log the first results Point.
                    //Point firstResultPoint = results.get(0).center();
                    String country=results.get(0).placeName();
                    Log.d("Geocoding",country);
                } else {

                    // No result for your request were found.

                    Toast.makeText(getApplicationContext(), "onResponse: No result found", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<GeocodingResponse> call, Throwable throwable) {
                throwable.printStackTrace();
            }
        });
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
        mapboxGeocoding.cancelCall();
    }

Upvotes: 1

Views: 639

Answers (1)

Moritz
Moritz

Reputation: 1790

There is currently no way of adding custom POIs to the data powering the Geocoding API, but the workaround is to use the Tilequery API. This API retrieves features from vector tiles based on proximity to a particular geographic coordinate. You can mimic reverse geocoding functionality with custom POIs via the following workflow:

  1. Create a tileset which contains your Custom POIs. That is, upload your data as a tileset.
  2. Retrieve the geographic coordinates of the point that you would like to use for reverse geocoding.
  3. Make a call to the Tilequery API where radius is 0, tileset_id is the id of the tileset you created in step 1, and {lon},{lat} are the longitude and latitude retrieved in step 2. More details about each of these parameters can be found here.

I'd recommend working through this tutorial on making a healthy food finder with the Tilequery API, to gain more intuition about how the API can be used for reverse geocoding with custom POIs. The Tilequery API playground is also an excellent resource for interactive experimentation.

Let me know if something is unclear!

Upvotes: 2

Related Questions