无需为多个标记类型创建多个类,只需创建一个新类,扩展标记并添加标记类型属性并覆盖 onTap:
public class DescriptedMarker extends Marker {
public DescriptedMarker(LatLong latLong, Bitmap bitmap, int horizontalOffset, int verticalOffset) {
super(latLong, bitmap, horizontalOffset, verticalOffset);
}
public String marker_description;
public int marker_type;
private Runnable action;
public void setOnTabAction(Runnable action){
this.action = action;
}
@Override
public boolean onTap(LatLong tapLatLong, Point layerXY, Point tapXY) {
double centerX = layerXY.x + getHorizontalOffset();
double centerY = layerXY.y + getVerticalOffset();
double radiusX = (getBitmap().getWidth() / 2) *1.1;
double radiusY = (getBitmap().getHeight() / 2) *1.1;
double distX = Math.abs(centerX - tapXY.x);
double distY = Math.abs(centerY - tapXY.y);
if( distX < radiusX && distY < radiusY){
if(action != null){
action.run();
return true;
}
}
return false;
}
}
现在您可以轻松创建多种类型的市场:
org.mapsforge.core.graphics.Bitmap bmp = AndroidGraphicFactory.convertToBitmap(getResources().getDrawable(R.drawable.myMarkerDrawable));
//pos is a LatLong variable
final DescriptedMarker marker = new DescriptedMarker(pos,bmp,0,0);
marker.marker_type = x; //x is an int and determine marker type
marker.setOnTabAction(new Runnable() {
@Override
public void run() {
if(marker.marker_type == 0)
Application.toast_short(marker.marker_description);
else if(marker.marker_type == 1)
{
//display a dialog for example
}// you can add many if for many types
}
});