我想学习只绘制地图上可见的覆盖项,因为我正在展示一张带有数千个标记的地图。我知道我必须检查覆盖项是否可见,如下所示:
private boolean isLocationVisible(location)
{
Rect currentMapBoundsRect = new Rect();
Point currentDevicePosition = new Point();
GeoPoint deviceLocation = new GeoPoint((int) (location.getLatitude() * 1000000.0), (int) (location.getLongitude() * 1000000.0));
mapView.getProjection().toPixels(deviceLocation, currentDevicePosition);
mapView.getDrawingRect(currentMapBoundsRect);
return currentMapBoundsRect.contains(currentDevicePosition.x, currentDevicePosition.y);
}
然后如果这个方法返回 true 我必须绘制覆盖项,如果不是,我不能绘制它。
问题是我无法覆盖 OverlayItem 中的 onDraw,所以我不知道如何满足我的需求。
我应该在我的代码中进行哪些更改以仅绘制地图上可见的标记?
这是我的 ItemizedOverlayClass:
public class mItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
boolean onTapActivated=false;
private Drawable marker = null;
private BusMap map = null;
boolean comingFromFavoriteNameEdit=false;
public BmItemizedOverlay(Drawable defaultMarker, BusMap map) {
super(boundCenterBottom(defaultMarker));
this.map=map;
this.marker=defaultMarker;
}
public mItemizedOverlay(Drawable defaultMarker, BusMap map, boolean onTapActivated) {
super(boundCenterBottom(defaultMarker));
this.map=map;
this.onTapActivated=onTapActivated;
this.marker=defaultMarker;
}
protected OverlayItem createItem(int i){return mOverlays.get(i);}
public int size() {return mOverlays.size();}
public void clear(){mOverlays.clear();}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
}
public void setOverlays(ArrayList <OverlayItem> overlays) {
mOverlays=overlays;
populate();
}
public void draw(Canvas canvas, MapView mapView, boolean shadow){
if (mapView.getZoomLevel() > 17){
boundCenterBottom(this.marker);
super.draw(canvas, mapView, false);
}
}
}