如何从地图中移除一个符号

huangapple go评论88阅读模式
英文:

How to remove a symbol from a map

问题

我目前正在创建一个地图,根据用户的选择更新并显示距离他们最近的5个位置。这个功能是有效的,但是当用户更改他们的选择时,地图会更新并显示5个新的位置以及5个旧的位置。

我不确定如何移除旧的标记。

public <arrayList> void displayResults(ArrayList<LocationDetails> allLocation) {

    SymbolManager sm = new SymbolManager(mapView, map, styleMap);

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

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

    int count = 1;

    for (LocationDetails a : allLocation) {
        // 获取用户到位置的距离
        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();
}
英文:

I am currently creating a map which updates based on users selection and displays 5 location closest to them. This works however when the user changes their selection the map updates and displays the 5 NEW locations as well as the 5 OLD locations.

I am not sure how to remove the old symbols.

public <arrayList> void displayResults(ArrayList<LocationDetails> allLocation) {

    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 + &quot;: &quot; + a.getName() + &quot; &quot; + &quot;\n&quot;);
distanceResults.append(Math.round(disResult[0]) + &quot;m&quot; + &quot;\n&quot;);
SymbolOptions symbolOptions = new SymbolOptions()
.withLatLng(new LatLng(LocationLat, LocationLng))
.withIconImage(&quot;marker-11&quot;)
.withTextField(&quot;&quot;+count)
.withIconColor(&quot;black&quot;)
.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();
}

答案1

得分: 1

我已经使用Mapbox三个月了。经过数小时的研究,我发现在安卓平台上唯一移除地图上标记(Symbol)或任何元素的方式是从头开始重新加载所有元素。不幸的是,目前还没有一种方法能够移除单个元素。因此,我建议您创建一个容器类来保存您的物品。

英文:

I have been using mapbox for 3 months. After hours of research I discovered that on Android the only way to remove a Symbol or any element on the map was to reload all the elements from scratch. Unfortunately, there is currently no method to remove a single element.
So I suggest you create a container class in which to save your items.

答案2

得分: 1

如果您的用例只需要在地图上同时显示约五个标记物,您可能会更容易使用原生数据源和 SymbolLayer,而不是依赖于 SymbolManager 提供的抽象层。

例如,这个 基于 API 响应更新图标的 Android 演示 展示了如何添加一个 GeoJSON 数据源和相应的图层到地图,然后更新该数据源以获得不同的视觉效果。基本上,您所需要的所有逻辑都封装在这里,但您的 GeoJSON 将是一个包含多个特征(即,5个)的 FeatureCollection,而不仅仅是一个点。

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

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)
  ));
}

然后,类似于 updateMarkerPostion 方法,更新数据源的 GeoJSON 到离用户位置最近的新位置:

private void updateMarkerPosition(LatLng position) {
  // 这个方法是我们在有新坐标时更新标记位置的地方。首先,我们检查是否是第一次执行这个处理程序,最好的方法是检查标记是否为空;
  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()))));
    }
  }
 
  // 最后,将摄像头动画移至新位置,这样用户就不必搜索标记,然后返回。
  map.animateCamera(CameraUpdateFactory.newLatLng(position));
}

当然,需要进行一些修改,但这个选项可能对于您的特定实现更加直接。

英文:

If your use case only requires showing about five markers on the map at a time, it might be easier to use native sources and SymbolLayers rather than relying on the abstraction provided by the SymbolManager.

For example, this icon updates based on API response Android demo shows how to add a GeoJSON source and corresponding layer to the map, then update said source to get a different visual result. Basically all of the logic you will need is encapsulated here, but your GeoJSON will be a FeatureCollection of multiple (namely, 5) features rather than just one point.

So, you can set up your symbols similarly to how it's done in the linked example:

private void initSpaceStationSymbolLayer(@NonNull Style style) {
style.addImage(&quot;space-station-icon-id&quot;,
BitmapFactory.decodeResource(
this.getResources(), R.drawable.iss));
style.addSource(new GeoJsonSource(&quot;source-id&quot;));
style.addLayer(new SymbolLayer(&quot;layer-id&quot;, &quot;source-id&quot;).withProperties(
iconImage(&quot;space-station-icon-id&quot;),
iconIgnorePlacement(true),
iconAllowOverlap(true),
iconSize(.7f)
));
}

, and then update the source's GeoJSON to the new locations closest to the user's position, similar to the updateMarkerPostion method:

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(&quot;source-id&quot;);
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));
}

A few modifications will need to be made, of course, but this option might be more direct for your implementation specifically.

huangapple
  • 本文由 发表于 2020年3月16日 23:49:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/60709094.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定