【问题标题】:I want to clear the markers in my map when each time location changes instead of clearing the map itself我想在每次位置更改时清除地图中的标记,而不是清除地图本身
【发布时间】:2020-02-23 16:12:21
【问题描述】:

我只想在位置更改时清除地图中的标记,而不是清除整个地图。例如,如果我在我的代码中使用 mMap.clear(),整个地图都会刷新,我无法查看地图中的其他任何地方。一旦我试图寻找其他地方,只要位置改变,整个地图就会清除。

提供的名为 updateMap() 的方法在 LocationListener 的重写方法 onLocationChanged 中调用,提供的参数是 Location 类对象的位置。

 public void updateMap(Location location){
     mMap.clear();
     LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());
     mMap.addMarker(new MarkerOptions().position(userLocation).title.("You are here").icon(BitmapDescriptorFactory.defaultMarker(Bit`````````````````````````mapDescriptorFactory.HUE_RED)));
     mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 15f));
}

我希望用户可以查看地图上任何位置的输出,即使用户的位置不断变化,而不是每次执行 mMap.clear() 行代码时都刷新地图。

【问题讨论】:

    标签: java google-maps android-studio


    【解决方案1】:

    在数据类型 Marker 上分配您的标记。

    Marker markerName = mMap.addMarker(new MarkerOptions().position(latLng).title("Title"));
    

    你可以像这样删除它。

    markerName.remove();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多