我如何从Marker对象中知道它是否已从谷歌地图中删除。我对表演/隐藏不感兴趣。我对"remove(("感兴趣。即使在marker.remove()
之后,isVisible
对于该标记也是真的。那我怎么知道呢?
有人要求复制一个示例代码。这是:
override fun onMapReady(googleMap: GoogleMap) {
this.googleMap = googleMap
val markerOptions = MarkerOptions()
markerOptions.position(LatLng(31.520959,74.352154))
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.curr_loc_grey))
val marker = googleMap.addMarker(markerOptions)
/........../
marker.remove()
}
我正在使用
api.com.google.maps:google地图服务:0.10.2'
当我调试时,我看到marker.isVisible
在添加时是true
,它在remove()
之后保持true
。是虫子吗?
无论如何,您可以在将标记添加到映射的同时将其添加到HashSet:
...
HashSet<Makrer> markersOnMap = new HashSet<>();
...
...
// when add marker to map
Marker marker = googleMap.addMarker(markerOptions);
markersOnMap.add(marker);
...
...
// when remove
markersOnMap.remove(marker);
marker.remove();
...
...
// when check is marker on map
if (markersOnMap.contains(marker)) {
// marker is on map
} else {
// marker removed
}
当然,这只是方法描述,而不是最终代码。
更新:
您可以创建自定义的基于MapView/MapFragment
的组件并继承HashSet
交互。类似的东西:
public class MarkesrMapView extends MapView implements OnMapReadyCallback {
private OnMapReadyCallback mMapReadyCallback;
private GoogleMap mGoogleMap;
private HashSet<Makrer> mMarkersOnMap;
public MarkersMapView(@NonNull Context context) {
super(context);
init();
}
public MarkersMapView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public MarkersMapView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public MarkersMapView(@NonNull Context context, @Nullable GoogleMapOptions options) {
super(context, options);
init();
}
private void init() {
mMarkersOnMap = new HashSet<>();
}
@Override
public void getMapAsync(OnMapReadyCallback callback) {
mMapReadyCallback = callback;
super.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
if (mMapReadyCallback != null) {
mMapReadyCallback.onMapReady(googleMap);
}
}
public Marker addMarker(MarkerOptions markerOptions) {
Marker marker = mGoogleMap.addMarker(markerOptions);
mMarkersOnMap.add(marker);
return marker;
}
public void removeMarker(Marker marker) {
if (marker == null) return;
mMarkersOnMap.remove(marker);
marker.remove();
}
public boolean isOnMap(Marker marker) {
if (marker == null) return false;
return mMarkersOnMap.contains(marker);
}
}
并像一样使用它
...
private markersMapView mMapView;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle mapViewBundle = null;
if (savedInstanceState != null) {
mapViewBundle = savedInstanceState.getBundle(MAP_VIEW_BUNDLE_KEY);
}
mMapView = (MarkersMapView) findViewById(R.id.mapview);
mMapView.onCreate(mapViewBundle);
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mMapView.addMarker(new MarkerOptions().position(...).title("Marker"));
}
});
...
if (mMapView.isOnMap(myMarker)) {
...
}
它可以是非常好的简单调用.isOnMap()
(尤其是在没有其他方法的情况下(。