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, including its pricing structure, 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)
.tilesetIds(tilesetId)
.query(Point.fromLngLat(LONGITUDE,LATITUDE))
.radius(radiusInMeters)
.limit(maxNumOfFeaturesReturned)
.geometry(geoJsonGeometryString) // "point", "linestring", or "polygon"
.dedupe(boolean)
.layers(singleOrListOfMapLayerIds) // layer name within a tileset, not a style
.build();
val tilequery = MapboxTilequery.builder()
.accessToken(MAPBOX_ACCESS_TOKEN)
.tilesetIds(tilesetId)
.query(Point.fromLngLat(LONGITUDE,LATITUDE))
.radius(radiusInMeters)
.limit(maxNumOfFeaturesReturned)
.geometry(geoJsonGeometryString) // "point", "linestring", or "polygon"
.dedupe(boolean)
.layers(singleOrListOfMapLayerIds) // layer name within a tileset, not a style
.build()
Create and run Tilequery API queries and see the results on a map.
Query response
Access the Tilequery API response object inside 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 Feature
s 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> featureList = response.body().features();
}
@Override public void onFailure(Call<FeatureCollection> call, Throwable throwable) {
Log.d("Request failed: %s", throwable.getMessage());
}
});
tilequery.enqueueCall(object : Callback<FeatureCollection> {
override fun onResponse(call: Call<FeatureCollection>, response: Response<FeatureCollection>) {
val featureList = response.body()?.features()
}
override fun onFailure(call: Call<FeatureCollection>, throwable: Throwable){
Log.d(TAG, "Request failed: %s", throwable.message)
}
})
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> featureList = response.body().features();
// Distance that the Feature is from the original Tilequery Point coordinate
String distance = featureList.get(0).getProperty("tilequery").getAsJsonObject().get("distance").toString();
// The Feature's GeoJSON geometry type
String geometryType = featureList.get(0).getProperty("tilequery").getAsJsonObject().get("geometry").toString();
// The id of the map layer which the Feature is a part of
String layerId = featureList.get(0).getProperty("tilequery").getAsJsonObject().get("layer").toString();
}
@Override public void onFailure(Call<FeatureCollection> call, Throwable throwable) {
Log.d("Request failed: %s", throwable.getMessage());
}
});
tilequery.enqueueCall(object : Callback<FeatureCollection> {
override fun onResponse(call: Call<FeatureCollection>, response: Response<FeatureCollection>) {
// The FeatureCollection that is inside the API response
val featureList.get = response.body()?.features()
// Distance that the Feature is from the original Tilequery Point coordinate
val distance = featureList[0].getProperty("tilequery").asJsonObject.get("distance").toString()
// The Feature's GeoJSON geometry type
val geometryType = featureList[0].getProperty("tilequery").asJsonObject.get("geometry").toString()
// The id of the map layer which the Feature is a part of
val layerId = featureList[0].getProperty("tilequery").asJsonObject.get("layer").toString()()
}
override fun onFailure(call: Call<FeatureCollection>, throwable: Throwable){
Log.d(TAG, "Request failed: %s", throwable.message)
}
})