Android - osmbonuspack - how to rotate the marker title on the map

℡╲_俬逩灬. 提交于 2021-02-08 03:14:14

问题


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.


回答1:


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.




回答2:


As OSMBonusPack Marker try to mimic Google Maps API, you can refer to https://developers.google.com/maps/documentation/android/marker

=> You will have description of Flat / Billboard orientation, and Rotation.

If you just want the Marker to be aligned with the screen, this is the default behaviour, just do nothing (no rotation).

BTW, what are you calling the "Marker title"? The title shown in the bubble?



来源:https://stackoverflow.com/questions/31701467/android-osmbonuspack-how-to-rotate-the-marker-title-on-the-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!