Skip to main content

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.

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()
playground
Tilequery Playground

Create and run Tilequery API queries and see the results on a map.

chevron-right

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 Features in it. But, there's no guarantee that the response will have any Feature objects in it.

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(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)

}
})
Was this page helpful?