我关注 mapsforge 库有一段时间了,我喜欢这个库的工作方式。
在我的项目中,我需要不同类型的标记。一种类型应该在点击时显示一个关于标记的对话框,而另一种类型应该在点击时为标记的坐标干杯。
所以我创建了 Marker 类的两个子类 PoiMarker 和 LocationMarker 并因此为这两个子类覆盖了 onTap() 的方法。现在,当我添加第一个标记 (PoiMarker) 时,一切都很好,并且显示了对话框。然后,当我添加第二个标记 (LocationMarker) 时,也会显示 toast ,但是当我点击第一个标记时,它会显示 toast 而不是对话框。在我点击 map 上的任何位置时,它都会显示 toast 而不是对话框。
我意识到,在向 map View 添加标记时,我们正在向 map View 添加一个图层,而当我添加另一个标记时,新图层只是覆盖前一个标记,而不会遇到第一个标记的点击。
即使在我添加了新的第二个标记后,如何使第一个标记可点击?
谢谢
最佳答案
无需为多个标记类型创建多个类,只需创建一个新类,扩展标记并添加标记类型属性并覆盖 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
}
});
关于android - Mapsforge on tap listeners 用于多个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33688193/