从GoogleMap的删除标记 [英] Remove a marker from a GoogleMap

查看:327
本文介绍了从GoogleMap的删除标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android的新的谷歌地图API,我们可以添加标记,但有现在的方式(容易)删除一个。

In the new Google Maps API for Android, we can add a marker, but there is now way to (easily) remove one.

我的解决办法是保持标记在地图上,并重新绘制地图时我想删除一个标记。但它不是很有效。

My solution is keeping the markers in a map and redraw the map when I want to remove a marker. But it is not very efficient.

private final Map<String, MarkerOptions> mMarkers = new ConcurrentHashMap<String, MarkerOptions>();

private void add(String name, LatLng ll) {
  final MarkerOptions marker = new MarkerOptions().position(ll).title(name);
  mMarkers.put(name, marker);

  runOnUiThread(new Runnable() {
    @Override
    public void run() {
      mMap.addMarker(marker);
    }
  });
}

private void remove(String name) {
  mMarkers.remove(name);

  runOnUiThread(new Runnable() {
    @Override
    public void run() {
      mMap.clear();

      for (MarkerOptions item : mMarkers.values()) {
        mMap.addMarker(item);
      }
    }
  });
}

有没有人有一个更好的主意?

Does anyone have a better idea?

推荐答案

方法签名的<一个href="http://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.html#addMarker%28com.google.android.gms.maps.model.MarkerOptions%29"><$c$c>addMarker是:

The method signature for addMarker is:

public final Marker addMarker (MarkerOptions options)

所以,当您使用指定的选项标记标记添加到 GoogleMap的,你应该保存<一href="http://developer.android.com/reference/com/google/android/gms/maps/model/Marker.html"><$c$c>Marker这将返回(而不是您用来创建它的 MarkerOptions )对象的对象。这个对象允许您更改后的标记状态。当您完成与标记,你可以叫<一href="http://developer.android.com/reference/com/google/android/gms/maps/model/Marker.html#remove%28%29"><$c$c>Marker.remove()从地图中删除。

So when you add a marker to a GoogleMap by specifying the options for the marker, you should save the Marker object that is returned (instead of the MarkerOptions object that you used to create it). This object allows you to change the marker state later on. When you are finished with the marker, you can call Marker.remove() to remove it from the map.

顺便说一句,如果你只希望暂时隐藏它,你可以通过调用<一个切换标志的可视性href="http://developer.android.com/reference/com/google/android/gms/maps/model/Marker.html#setVisible%28boolean%29"><$c$c>Marker.setVisible(boolean).

As an aside, if you only want to hide it temporarily, you can toggle the visibility of the marker by calling Marker.setVisible(boolean).

这篇关于从GoogleMap的删除标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆