android添加和删除标记没有调用clear()

时间:2016-10-11 10:37:33

标签: android google-maps marker

我有一张地图,当移动相机时,它会调用api来获取数据并添加新标记。

例如我第一次从api获得4个标记[a b c d]

在我拖动地图后,我从api

获得了[b c d e]

我不想调用map.clear()然后添加新的,因为这会导致闪光(所有标记消失并返回)

什么是一种有效的方式去除" a"并添加" e" 我试图避免使用for循环(需要根据他们的lat / lng比较所有数据,可能会减慢堆标记。)

我发现有人使用QuadTree来实现这一目标,但不确定它是如何工作的。

更新:关键点是如何找到要删除的标记以及要添加的标记

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您可以从执行Marker的地图中删除marker.remove();。例如:

Marker marker = mMap.addMarker(new MarkerOptions().position(new LatLng(40, 4)));

// More code

marker.remove();

在您的情况下,您可以使用HashMap维护具有指定ID的Marker,然后查询HashMap以将其从地图中删除:

private Map<String, Marker> markers = new HashMap<>();

//

markers.put("markerName", mMap.addMarker(new MarkerOptions().position(new LatLng(40, 4)))); // Add the marher to the map and to the hashmap

markers.remove("markerName") // Find and remove the marker from the hashmap
       .remove();            // Remove the marker from the map