1

我关注mapsforge库有一段时间了,我喜欢这个库的运作。

在我的项目中,我需要不同类型的标记。一种类型应该在点击时显示一个关于标记的对话框,另一种类型应该在点击时对标记的坐标进行吐司。

因此,我创建了 Marker 类的两个子类,分别是 PoiMarker 和 LocationMarker,从而覆盖了两个子类的onTap()方法。现在,当我添加第一个标记(PoiMarker)时,一切都很好,并显示了对话框。然后,当我添加第二个标记(LocationMarker)时,也会显示 toast,但是当我点击第一个标记时,它会显示 toast 而不是对话框。在我点击地图的任何地方,它都会向我显示祝酒词而不是对话框。

我意识到,当向地图视图添加标记时,我们正在向地图视图添加图层,当我添加另一个标记时,新图层只会覆盖前一个标记,并且从未遇到过点击第一个标记。

即使在添加新的第二个标记后,如何使第一个标记可点击?

谢谢

4

1 回答 1

1

无需为多个标记类型创建多个类,只需创建一个新类,扩展标记并添加标记类型属性并覆盖 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
       }
   });
于 2016-08-08T14:38:07.303 回答