My mapview (osmbonuspack) rotates into the walking direction.
How can I rotate the marker's title on the map so that they are horizontal - i.e. easy for reading? I see that the marker itself does not need any rotation while the map is turned.
Any change in the walking direction will result in the right rotation of the map using:
mapview.setMapOrientation( (float) -headingByCompass);
So on any change, I first find all Markers on the mapview. Then I tried to rotate them ... but the title direction is still the same.
marker.setRotation( rotation);
To be sure: this is about the text that is near the inverted eye drop. This is not the info in the bubble.
The Marker itself has no label attached to it. So, I created a subclass of Marker called MarkerWithLabel. In this subclass the title or label is drawn.
When the map is rotated, the rotation is subsequently passed to all MarkerWithLabel objects. A subsequent invalidate on the mapview will make the changes visible. So, the markers and the labels are always horizontal for easy reading.
The MarkerWithLabel class it:
public class MarkerWithLabel extends Marker {
Paint textPaint = null;
String mLabel = null;
float rotation = 0.0f;
public MarkerWithLabel( MapView mapView, String label) {
super( mapView);
mLabel = label;
textPaint = new Paint();
textPaint.setColor( Color.RED);
textPaint.setTextSize( WaypointActivity.textSizeCanvas25sp);
textPaint.setAntiAlias(true);
textPaint.setTextAlign(Paint.Align.LEFT);
setTitle( label);
}
public void draw( final Canvas c, final MapView osmv, boolean shadow) {
draw( c, osmv);
}
public void draw( final Canvas c, final MapView osmv) {
super.draw( c, osmv, false);
Point p = this.mPositionPixels; // already provisioned by Marker
if( rotation <= -1 || rotation >= 1) { // could be left out
c.save();
c.rotate( rotation, p.x, p.y);
c.drawText( getTitle(), p.x, p.y+20, textPaint);
c.restore();
} else {
c.drawText( getTitle(), p.x, p.y+20, textPaint);
}
}
}
Finding all MarkerWithLabel instances is easy:
List<Overlay> markersOnTheMap = mv.getOverlays();
if( markersOnTheMap == null || markersOnTheMap.isEmpty()) {
return ;
}
for( int i = 0; i < markersOnTheMap.size(); i++) {
Object o = markersOnTheMap.get( i);
if( o instanceof MarkerWithLabel) {
MarkerWithLabel m = (MarkerWithLabel) o;
m.rotation = rotation;
}
}
Hope this helps you.