【问题标题】:How to clear all the markers in v2 google map?如何清除 v2 谷歌地图中的所有标记?
【发布时间】:2013-06-19 17:12:46
【问题描述】:

我需要清除 v2 谷歌地图中的所有标记。再次需要添加一些标记。如果有人知道答案,请分享您的想法。

【问题讨论】:

标签: android google-maps google-maps-markers


【解决方案1】:

您可以使用googleMap.clear(),也可以将标记存储在某种集合中,然后循环删除它们:

private ArrayList<Marker> mMarkers;
...
private void removeMarkers() {
    for (Marker marker: mMarkers) {
        marker.remove();
    }
    mMarkers.clear();
}

【讨论】:

  • clear() 将完成这项工作。第二个解决方案将从数组列表中删除,然后您将不得不更新您的视图/刷新我猜
  • 别忘了在removeMarkers函数结束时调用mMarkers.clear();
  • Raghunandan:marker.remove() 从地图中删除标记。 MaciejGórski:很好,更新了代码。
【解决方案2】:

ex - 如果您想刷新并加载地图中的新标记点以单击按钮(在此示例中,我得到按钮单击),

    switch ( view.getId() ) {
        case R.id.buttonOne:

    //clear googlemap
    googleMap.clear();
    //call to generate new marker
    this.getMarker(lat,lang);
        break;
        }

//to add new marker
public void getMarker ( String lat,String lang ) {

LatLng latLang = new LatLng( lat, lang);
//call to your googlemap implementation method
this.getGoogleMap();
Marker marker = googleMap.addMarker(new MarkerOptions().position(latLang))

}

【讨论】:

    【解决方案3】:

    使用 Google Map 对象并调用 clear 来清除标记。

    mMap.clear();
    

    https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap#clear()

    查看文档

    public final void clear ()

    从地图中删除所有markers, polylines, polygons, overlays等。

    【讨论】:

      【解决方案4】:

      只需创建一个方法为clearOverlays()

      在方法内部

      public void clearOverlays(){
              if(mMap!=null){
                  mMap.clear();
              }else{
                  Log.d("Maps::","mMap is null");
              }
          }
      

      mMap 在哪里

      public static GoogleMap mMap;
      

      mMap 将在
      public void onMapReady(GoogleMap googleMap) 方法中自动初始化。

      那里放mMap = googleMap;

      现在在任何地方使用clearOverlays() 方法。

      【讨论】:

        【解决方案5】:

        我认为this 会对您有所帮助。当您需要通过清除 Google Map 和 List 变量的对象来替换标记时,获取列表中的所有标记并刷新地图视图。

        【讨论】:

          【解决方案6】:

          你可以在 java 和 kotlin 中使用 clear()

          java

           googleMap.clear()
          

          科特林

           googleMap?.clear()
          

          【讨论】:

            猜你喜欢
            • 2013-12-09
            • 1970-01-01
            • 2016-07-19
            • 2016-10-12
            • 1970-01-01
            • 2017-02-02
            • 1970-01-01
            • 2013-03-17
            • 2012-08-21
            相关资源
            最近更新 更多