osmdroid API解讀(十二)
osmdroid-android org.osmdroid.views.overlay.simplefastpoint包
本包的目的在于友善的添加一系列的點在Overlay上。
1. LabelledGeoPoint
帶有标簽的地理點,在GeoPoint的基礎上添加了mLabel屬性。
public class LabelledGeoPoint extends GeoPoint {
private String mLabel;
...
}
2. SimplePointTheme
一個管理多個IGeoPoint的類,内置一個List來管理這些點。你可以添加帶标簽或不帶标簽兩種類型的GeoPoints。此類實作了SimpleFastPointOverlay.PointAdapter,友善在SimpleFastPointOverlay中管理這些點
/**
* This class is just a simple wrapper for a List of {@link IGeoPoint}s to be used in
* {@link SimpleFastPointOverlay}. Can be used for unlabelled or labelled GeoPoints. Be sure to set
* the labelled parameter of the constructor to match the kind of points.
* More complex cases should implement {@link SimpleFastPointOverlay.PointAdapter}, not extend this
* one.
* Created by Miguel Porto on 26-10-2016.
*/
public final class SimplePointTheme implements SimpleFastPointOverlay.PointAdapter {
private final List<IGeoPoint> mPoints;
private boolean mLabelled;
public SimplePointTheme(List<IGeoPoint> pPoints, boolean labelled) {
mPoints = pPoints;
mLabelled = labelled;
}
@Override
public int size() {
return mPoints.size();
}
@Override
public IGeoPoint get(int i) {
return mPoints.get(i);
}
@Override
public boolean isLabelled() {
return mLabelled;
}
/**
* NOTE: this iterator will be called very frequently, avoid complicated code.
* @return
*/
@Override
public Iterator<IGeoPoint> iterator() {
return mPoints.iterator();
}
}
3. SimpleFastPointOverlayOptions
用于繪制點的工具。
public class SimpleFastPointOverlayOptions {
public enum RenderingAlgorithm {NO_OPTIMIZATION, MEDIUM_OPTIMIZATION, MAXIMUM_OPTIMIZATION}//繪制算法
public enum Shape {CIRCLE, SQUARE}//繪制形狀
public enum LabelPolicy {ZOOM_THRESHOLD, DENSITY_THRESHOLD}
protected Paint mPointStyle;//點的畫筆工具
protected Paint mSelectedPointStyle;//被選擇點的畫筆工具
protected Paint mTextStyle;//标注字的風格
protected float mCircleRadius = 5;//點圓角大小
protected float mSelectedCircleRadius = 13;//被選擇點的圓角大小
protected boolean mClickable = true;//可點選性
protected int mCellSize = 10; //格網尺寸
protected RenderingAlgorithm mAlgorithm = RenderingAlgorithm.MAXIMUM_OPTIMIZATION;//繪制算法
protected Shape mSymbol = Shape.CIRCLE;//點的形狀
protected LabelPolicy mLabelPolicy = LabelPolicy.ZOOM_THRESHOLD;
protected int mMaxNShownLabels = 250;
protected int mMinZoomShowLabels = 11;
public SimpleFastPointOverlayOptions() {
mPointStyle = new Paint();
mPointStyle.setStyle(Paint.Style.FILL);
mPointStyle.setColor(Color.parseColor("#ff7700"));
mSelectedPointStyle = new Paint();
mSelectedPointStyle.setStrokeWidth(5);
mSelectedPointStyle.setStyle(Paint.Style.STROKE);
mSelectedPointStyle.setColor(Color.parseColor("#ffff00"));
mTextStyle = new Paint();
mTextStyle.setStyle(Paint.Style.FILL);
mTextStyle.setColor(Color.parseColor("#ffff00"));
mTextStyle.setTextAlign(Paint.Align.CENTER);
mTextStyle.setTextSize(24);
}
//擷取預設的點圖層SimpleFastPointOverlayOptions
public static SimpleFastPointOverlayOptions getDefaultStyle() {
return new SimpleFastPointOverlayOptions();
}
//設定點的風格
public SimpleFastPointOverlayOptions setPointStyle(Paint style) {
mPointStyle = style;
return this;
}
//設定被選擇點的風格
public SimpleFastPointOverlayOptions setSelectedPointStyle(Paint style) {
mSelectedPointStyle = style;
return this;
}
//設定圓角
public SimpleFastPointOverlayOptions setRadius(float radius) {
mCircleRadius = radius;
return this;
}
//設定被選擇點的圓角
public SimpleFastPointOverlayOptions setSelectedRadius(float radius) {
mSelectedCircleRadius = radius;
return this;
}
//設定可點選性
public SimpleFastPointOverlayOptions setIsClickable(boolean clickable) {
mClickable = clickable;
return this;
}
public SimpleFastPointOverlayOptions setCellSize(int cellSize) {
mCellSize = cellSize;
return this;
}
public SimpleFastPointOverlayOptions setAlgorithm(RenderingAlgorithm algorithm) {
mAlgorithm = algorithm;
return this;
}
public SimpleFastPointOverlayOptions setSymbol(Shape symbol) {
mSymbol = symbol;
return this;
}
public SimpleFastPointOverlayOptions setTextStyle(Paint textStyle) {
mTextStyle = textStyle;
return this;
}
public SimpleFastPointOverlayOptions setMinZoomShowLabels(int minZoomShowLabels) {
mMinZoomShowLabels = minZoomShowLabels;
return this;
}
public SimpleFastPointOverlayOptions setMaxNShownLabels(int maxNShownLabels) {
mMaxNShownLabels = maxNShownLabels;
return this;
}
public SimpleFastPointOverlayOptions setLabelPolicy(LabelPolicy labelPolicy) {
mLabelPolicy = labelPolicy;
return this;
}
}
4. SimpleFastPointOverlay
一個繪制簡單可點選點的Overlay,優化了加載速度。能夠很容易的加載10萬個點。不支援每個點的個性化,每個點的風格是一緻的。不支援地圖旋轉時的情況。
public class SimpleFastPointOverlay extends Overlay {
private final SimpleFastPointOverlayOptions mStyle;
private final PointAdapter mPointList;
private final BoundingBox mBoundingBox;
private Integer mSelectedPoint;
private OnClickListener clickListener;
private LabelledPoint grid[][];
private boolean gridBool[][];
private int gridWid, gridHei, viewWid, viewHei;
private float startX, startY, curX, curY, offsetX, offsetY;
private int prevNumPointers, numLabels;
private BoundingBox prevBoundingBox = new BoundingBox(0, 0, 0, 0);
...
}