I am following the mapsforge library for some time now and I love the workings of the library.
In my project, I need different types of markers. One type should display a dialog, regarding the marker, when tapped and another type should make a toast of the marker's coordinates when tapped.
So I create two child classes of the Marker class say PoiMarker and LocationMarker and thus override the methods of onTap() for both the child classes. Now when I add the first marker(PoiMarker) it's all good and the dialog is shown. Then when I add the second marker(LocationMarker) the toast is also displayed, but when i tap on the first marker it displays a toast instead of a dialog. And any where i tap on the map it displays me a toast instead of a dialog.
I realize that when adding a marker to the mapview we are adding a layer to the mapview and when I add another marker the new layer just overlays the previous marker and the tap to the first marker is never encountered.
How do I make the first marker tappable even after I add a new second marker?
Thanks
No need to create multiple class for multiple marker Type, just create a new class, extend marker and add markerType property and override 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;
}
}
now you can easily create multiple type of market:
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
}
});