首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从地图中移除符号

如何从地图中移除符号
EN

Stack Overflow用户
提问于 2020-03-16 23:49:19
回答 2查看 668关注 0票数 0

我目前正在创建一个地图,根据用户的选择更新,并显示最近的5个位置。然而,当用户改变他们的选择时,这是有效的,地图更新并显示5个新位置和5个旧位置。

我不确定如何删除旧的符号。

公共空displayResults(ArrayList allLocation) {

代码语言:javascript
复制
    SymbolManager sm = new SymbolManager(mapView,map,styleMap);

    sm.deleteAll();
    SymList.clear();


        sm.setIconAllowOverlap(true);
        sm.setIconIgnorePlacement(true);

    int count = 1;

    for (LocationDetails a : allLocation
    ) {
        // gets the distance from user to Location
        double LocationLat = Double.parseDouble(a.getLatitude());
        double LocationLng = Double.parseDouble(a.getLongitude());
        float[] disResult = new float[1];
        Location.distanceBetween(lat, lng, LocationLat, LocationLng, disResult);
        results.append(count + ": " + a.getName() + " " + "\n");
        distanceResults.append(Math.round(disResult[0]) + "m" + "\n");

        SymbolOptions symbolOptions = new SymbolOptions()
                .withLatLng(new LatLng(LocationLat, LocationLng))
                .withIconImage("marker-11")
                .withTextField(""+count)
                .withIconColor("black")
                .withIconSize(2.5f);

        SymList.add(symbolOptions);

        count++;
    }

    LatLngBounds latLngBounds = new LatLngBounds.Builder()
            .include(SymList.get(0).getLatLng())
            .include(SymList.get(1).getLatLng())
            .include(SymList.get(2).getLatLng())
            .include(SymList.get(3).getLatLng())
            .include(SymList.get(4).getLatLng())
            .build();

    map.animateCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, 50), 2000);

    for(SymbolOptions a : SymList){
        sm.create(a);
    }

    SymList.clear();
}
EN

回答 2

Stack Overflow用户

发布于 2020-03-17 05:28:09

我已经使用mapbox 3个月了。经过几个小时的研究,我发现在Android上,删除地图上的符号或任何元素的唯一方法是从头开始重新加载所有元素。不幸的是,目前还没有删除单个元素的方法。所以我建议你创建一个容器类来保存你的项目。

票数 1
EN

Stack Overflow用户

发布于 2020-03-25 08:22:34

如果您的用例一次只需要在地图上显示大约五个标记,那么使用原生源和SymbolLayer可能比依赖SymbolManager提供的抽象更容易。

例如,此icon updates based on API response Android demo展示了如何将GeoJSON源和相应的图层添加到地图中,然后更新所述源以获得不同的视觉结果。基本上,您需要的所有逻辑都封装在这里,但您的GeoJSON将是多个(即5个)功能的FeatureCollection,而不只是一个点。

因此,您可以按照链接示例中的方式设置您的元件:

代码语言:javascript
复制
private void initSpaceStationSymbolLayer(@NonNull Style style) {
  style.addImage("space-station-icon-id",
  BitmapFactory.decodeResource(
  this.getResources(), R.drawable.iss));

  style.addSource(new GeoJsonSource("source-id"));

  style.addLayer(new SymbolLayer("layer-id", "source-id").withProperties(
    iconImage("space-station-icon-id"),
    iconIgnorePlacement(true),
    iconAllowOverlap(true),
    iconSize(.7f)
  ));
}

,然后将源的GeoJSON更新为最接近用户位置的新位置,类似于updateMarkerPostion方法:

代码语言:javascript
复制
private void updateMarkerPosition(LatLng position) {
  // This method is where we update the marker position once we have new coordinates. First we
  // check if this is the first time we are executing this handler, the best way to  do this is
  // check if marker is null;
  if (map.getStyle() != null) {
    GeoJsonSource spaceStationSource = map.getStyle().getSourceAs("source-id");
    if (spaceStationSource != null) {
      spaceStationSource.setGeoJson(FeatureCollection.fromFeature(
      Feature.fromGeometry(Point.fromLngLat(position.getLongitude(), position.getLatitude()))));
    }
  }

  // Lastly, animate the camera to the new position so the user
  // wont have to search for the marker and then return.
  map.animateCamera(CameraUpdateFactory.newLatLng(position));
}

当然,需要进行一些修改,但此选项可能更直接地适用于您的具体实现。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60709094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档