Overview

Tilequery

The Mapbox Tilequery API allows you to retrieve data about specific features from a vector tileset, based on a given latitude and longitude. The API makes it possible to query for features within a radius, do point in polygon queries, query for features in multiple composite layers, and augment data from the Mapbox Geocoding API with custom data.

For more information about this API, see the Mapbox Tilequery API documentation.

Building the query URL

To begin with, you'll need to create a new instance of the MapboxTilequery object and use its builder to customize your query. The options offered in the builder includes choices such as the query coordinate, the type of GeoJSON geometry you're searching for, and the search area radius.

MapboxTilequery tilequery = MapboxTilequery.builder()
.accessToken(MAPBOX_ACCESS_TOKEN)
.mapIds(mapId)
.query(Point.fromLngLat(LONGITUDE,LATITUDE))
.radius(radiusInMeters)
.limit(maxNumOfFeaturesReturned)
.geometry(geoJsonShape)
.dedupe(boolean)
.layers(singleOrListOfMapLayerIds)
.build();

Query response

You can access the Tilequery API's response inside of the onResponse callback. The onResponse callback is built with Retrofit, like the Java SDK's other services' callbacks. The response will include a List<Feature> if the query you built has any Features in it. But, there's no guarantee that the response will have any Feature objects in it.

tilequery.enqueueCall(new Callback<FeatureCollection>() {
@Override public void onResponse(Call<FeatureCollection> call, Response<FeatureCollection> response) {
List<Feature> featureCollection = response.body().features();
}
@Override public void onFailure(Call<FeatureCollection> call, Throwable throwable) {
Log.d("Request failed: %s", throwable.getMessage());
}
});

Each Feature in the response has a distance, geometry, and layer property associated with it:

tilequery.enqueueCall(new Callback<FeatureCollection>() {
@Override public void onResponse(Call<FeatureCollection> call, Response<FeatureCollection> response) {
// The FeatureCollection that is inside the API response
List<Feature> featureCollection = response.body().features();
// Distance that the Feature is from the original Tilequery Point coordinate
String distance = featureCollection.get(0).getProperty("tilequery").getAsJsonObject().get("distance").toString();
// The Feature's GeoJSON geometry type
String geometryType = featureCollection.get(0).getProperty("tilequery").getAsJsonObject().get("geometry").toString();
// The id of the map layer which the Feature is a part of
String layerId = featureCollection.get(0).getProperty("tilequery").getAsJsonObject().get("layer").toString();
}
@Override public void onFailure(Call<FeatureCollection> call, Throwable throwable) {
Log.d("Request failed: %s", throwable.getMessage());
}
});
Was this page helpful?