并非所有mapbox.com的服务在中国提供
Android 地图 SDK
并非所有mapbox.com的服务在中国提供
翻译:唐立 审阅:王荣
Maps SDK 为您提供查询地图层的工具以获取 GeoJSON要素
的列表,保存着要素
的几何形状与属性信息。例如,用户可以通过点击地图上的位置,查询显示地图上以 GeoJSON 要素
形式存储的 POI。随后可获得相关要素的属性信息,包括以String
格式储存的 POI 名称等。需要注意的是,由于地图查询操作并不总是能够查询返回用户查找的信息,因此可能收到包含0个要素的要素列表
。
您可以在地图上查询位于某一点
或特定边界框
内的要素。除显示的图层外,无论被查询的数据是否实际显示在地图上,您都可以根据您需要的特定信息对其执行查询操作。
要素主要来源为矢量切片(或内部转换为切片的 GeoJSON 数据),因此在查询过程中可能会出现要素被拆分或在切片边界重复出现的情况,导致同一要素在查询结果中多次出现。
例如,当在一个区域内通过边界框查询时,如果该区域包括一条横跨多个切片的公路,则查询将为该公路横跨的每个切片返回一个单独的要素。各要素的几何形状将限于这条公路在该切片内存在的部分。同样,由于切片的缓冲作用,切片边界附近的某些点要素可能会出现在相邻的多个切片中。
使用查询渲染的要素
功能可以返回当前设备上渲染的所有地图要素。查询需要满足的条件,包括要素必须在设备的视区中可见,以及实现完全渲染。
查询渲染的要素
仅能接受使用屏幕像素值,而不是LatLng
值。因此在许多情况下,您需要将屏幕位置转换为地理位置。在以下代码段中,单击地图将提供一个LatLng
数值,我们将其用来查询地图并获取该位置的属性。
@Overridepublic void onMapClick(@NonNull LatLng point) { // Convert LatLng coordinates to screen pixel and only query the rendered features. final PointF pixel = mapboxMap.getProjection().toScreenLocation(point); List<Feature> features = mapboxMap.queryRenderedFeatures(pixel); // Get the first feature within the list if one exist if (features.size() > 0) { Feature feature = features.get(0); // Ensure the feature has properties defined if (feature.properties() != null) { for (Map.Entry<String, JsonElement> entry : feature.properties().entrySet()) { // Log all the properties Log.d(TAG, String.format("%s = %s", entry.getKey(), entry.getValue())); } } }}
override fun onMapClick(point: LatLng) { // Convert LatLng coordinates to screen pixel and only query the rendered features. val pixel = mapboxMap.projection.toScreenLocation(point) val features = mapboxMap.queryRenderedFeatures(pixel) // Get the first feature within the list if one exist if (features.size > 0) { val feature = features[0] // Ensure the feature has properties defined for ((key, value) in feature.properties()!!.entrySet()) { // Log all the properties Log.d(TAG, String.format("%s = %s", key, value)) }}
查询所有图层的渲染要素:
List<Feature> features = mapboxMap.queryRenderedFeatures(pixel);
val features = mapboxMap.queryRenderedFeatures(pixel)
查询某一图层的渲染要素:
// You can pass in a single layer id or a list of layer idsList<Feature> features = mapboxMap.queryRenderedFeatures(pixel,"LAYER-ID");
`kotlin={` // You can pass in a single layer id or a list of layer ids val features = mapboxMap.queryRenderedFeatures(pixel,"LAYER-ID")
### 基于边界框的查询 如需在地图上查询某一区域内的`要素`,可通过使用`RectF`对象传递边界框来实现。边界框既可以来自于地图顶部显示给用户的Android`视图`,也可以是视区域内显示的四个坐标。 以下代码段显示了如何获取四个坐标,将它们转换为新的`RectF`对象。这个`RectF`对象随后将被传递到 `queryRenderedFeatures()`中。 ```javaRectF rectF = new RectF( mapView.getLeft(), mapView.getTop(), mapView.getRight(), mapView.getBottom());mapboxMap.queryRenderedFeatures(rectF);
val rectF = RectF( mapView.left.toFloat(), mapView.top.toFloat(), mapView.right.toFloat(), mapView.bottom.toFloat())mapboxMap.queryRenderedFeatures(rectF)
使用querySourceFeatures()
函数查询后,所有与查询参数匹配的要素都将被返回,无论它们当前是否已被渲染可见。查询的域包括所有当前加载的矢量切片和 GeoJSON 源切片。此函数不会检索当前可见视区之外的切片。
若要查询数据源,您必须将查询参数传递为一组Filters
,只有满足该查询参数表述的要素才会添加至返回的要素列表中。例如,在以下代码段中,地图样式包含名为population-source
的 GeoJSON 源,它包含了每个要素的population(人口)
属性。以下查询将仅返回人口属性大于 100,000 的要素列表。
`java={` mapboxMap.getStyle(new Style.OnStyleLoaded() {@Override public void onStyleLoaded(@NonNull Style style) {
GeoJsonSource source = style.getSourceAs("population-source"); if (source != null){List
}});
```kotlinmapboxMap.getStyle { val source = it.getSourceAs<GeoJsonSource>("population-source") val features = source?.querySourceFeatures(Expression.get("population"))}