1

我确定我可能会在 Mapbox 实现中遗漏一些东西,因为我一直在研究很多替代方案(Tangram ES、Mapzen 和其他)。

我需要在 Android 上原生显示数千个(而不是数百万个)交互点(或标记)。我只需要能够捕捉点击了哪个点/标记。

我成功地在 Mapbox for Android 上实现了标记。但是我不喜欢缩小手机时显示这么多标记的结果。

在 Leaflet 中有一个很棒的插件,叫做 Markercluster,它用一个数字分组在一个圆圈中,下面是隐藏的标记。我发现这个解决方案简直完美。

我找不到 Mapbox(或其他 API)的类似解决方案。到目前为止,我发现的唯一与我正在寻找的东西更接近的是在 Mapbox 上加载 GeoJSON 文件。这很简单,而且很容易做到。以后容易隐藏图层。但我无法知道点击了哪个点,因此我可以加载与该点相关的信息并将其显示在屏幕上。

因此,简报中,标记缺少我使用 GeoJSON 点获得的外观(当我缩小时会隐藏或在放大时显示,在我这样做时或多或少地显示)。但是,标记允许我点击它们。

PS 如果有人知道 Mapbox 的替代方案,可以根据需要显示 POI(兴趣点),请告诉我。我很乐意给它一个机会。

4

1 回答 1

2

好吧,显然有一种方法可以使用 Mapbox for Android SDK 来做我想做的事情。方法是这样的:

  1. 使用 GeoJson 文件加载点(标记)(添加数据源)
  2. 包括标记的图标(Mapbox 提供了广泛的默认标记https://www.mapbox.com/maki-icons/
  3. 将 GeoJson 点与图标相关联(因此每个点至少与一个图标相关,但可能是您希望所有标记都使用相同的图标)(添加视觉表示)
  4. 添加 onClick 事件侦听器以检测已单击的点(添加交互性)

1.使用 GeoJson 文件加载点(标记)(添加数据源)

URL geoJsonUrl = new URL("https://your-website.com/list.json");
final GeoJsonSource geoJsonSource = new GeoJsonSource("geojson-source", geoJsonUrl);
mapboxMap.addSource(geoJsonSource);

2.包括标记的图标(Mapbox 提供了广泛的默认标记https://www.mapbox.com/maki-icons/)(添加视觉表示)

Bitmap marker_type1_icon = BitmapFactory.decodeResource(getResources(), R.drawable.marker_type1_icon);
mapboxMap.addImage("marker_type1", marker_type1_icon);
Bitmap marker_type2_icon = BitmapFactory.decodeResource(getResources(), R.drawable.marker_type2_icon);
mapboxMap.addImage("marker_type2", marker_type2_icon);

3.将 GeoJson 点与图标相关联(因此每个点至少与一个图标相关,但可能是您希望所有标记都使用相同的图标)(添加视觉表示)

SymbolLayer symbolLayer = new SymbolLayer("layer-id", "geojson-source");
symbolLayer.setProperties(
    // This is the bit that makes the map to display an icon or another. "poi" is a property of a Point in a GeoJSON document.
    // If you enclosed between keys,, it will introduce the value of the poi property.
    // If you want a fixed icon for all markers, change this for "marker_type1", following my example from point 2.
    PropertyFactory.iconImage("{poi}"),
    // With this property we will show which Point was clicked, making the icon look bigger
    PropertyFactory.iconSize(
        Function.property(
            "selected",
            Stops.categorical(
                Stop.stop(true, PropertyFactory.iconSize(2.0f)),
                Stop.stop(false, PropertyFactory.iconSize(1.0f))
            )
        )
    )
);
mapboxMap.addLayer(symbolLayer);

4.添加onClick事件监听器以检测点击了哪个点(添加交互性)

mapboxMap.addOnMapClickListener(new MapboxMap.OnMapClickListener() {
    @Override
    public void onMapClick(LatLng point) {
        PointF screenPoint = mapboxMap.getProjection().toScreenLocation(point);
        List<Feature> features = mapboxMap.queryRenderedFeatures(screenPoint, "layer-id");
        if (!features.isEmpty()) {
            Feature selectedFeature = features.get(0);
            selectedFeature.getProperties().addProperty("selected", true);
            String title = selectedFeature.getStringProperty("title");
            Toast.makeText(MapActivity.this, "You selected " + title, Toast.LENGTH_SHORT).show();

            // This triggers the update of the feature (Point) on the data source so it updates the SymbolLayer and you can see the feature enabled (bigger in this example)
            geoJsonSource.setGeoJson(selectedFeature);
        }
    }
});

我找到的完整指南是这个https://blog.mapbox.com/a-guide-to-the-android-symbollayer-api-5daac7b66f2c非常棒,尽管一两个功能已被弃用,我寻找他们替代品。除此之外,这可能是深入了解 Mapbox SDK 的 SymbolLayer 的好方法。你可以用它做一些很好的事情。

于 2018-02-23T11:25:18.140 回答