Rotate marker in OSMDroid for Android?

Ok, this one listened to me for hours, I have the following relatively simple code that puts a marker on an osmdroid card

final ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
OverlayItem marker = new OverlayItem("markerTitle", "markerDescription", new GeoPoint(52.033954,1.210179));
marker.setMarkerHotspot(OverlayItem.HotspotPlace.TOP_CENTER);
items.add(marker);

Drawable newMarker = this.getResources().getDrawable(R.drawable.maincar);
DefaultResourceProxyImpl resProxyImp = new DefaultResourceProxyImpl(getApplicationContext());
ItemizedIconOverlay markersOverlay = new ItemizedIconOverlay<OverlayItem>(items, newMarker, null, resProxyImp);
mapView.getOverlays().add(markersOverlay);

However, the marker always faces the top of the screen (repeating 0deg). How can you easily rotate each marker to a certain extent (360deg - full circle)?

+4
source share
5 answers

Try using the RotateMyBitmap method , for example:

Bitmap source = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher);

Bitmap target = RotateMyBitmap(source, 120.0f);

final ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
OverlayItem marker = new OverlayItem("markerTitle", "markerDescription", new GeoPoint(52.033954,1.210179));
marker.setMarkerHotspot(OverlayItem.HotspotPlace.TOP_CENTER);
items.add(marker);

Drawable newMarker = new BitmapDrawable(getResources(), target);

//Drawable newMarker = this.getResources().getDrawable(R.drawable.maincar);
DefaultResourceProxyImpl resProxyImp = new DefaultResourceProxyImpl(getApplicationContext());
ItemizedIconOverlay markersOverlay = new ItemizedIconOverlay<OverlayItem>(items, newMarker, null, resProxyImp);
mapView.getOverlays().add(markersOverlay);

Where is RotateMyBitmap :

public static Bitmap RotateMyBitmap(Bitmap source, float angle)
{
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

Result:

enter image description here

This works fine, but I recommend using the MyLocationOverlay class

+3

- , ItemizedIconOverlay OSMBonusPack (on osmdroid).

setRotation(degreees).

"flat" "billboard", , , .

+2

:

Bitmap bmpOriginal = BitmapFactory.decodeResource(this.getResources(), R.drawable.maincar_osm);
Bitmap bmResult = Bitmap.createBitmap(bmpOriginal.getWidth()*2, bmpOriginal.getHeight()*2, Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(bmResult);
tempCanvas.rotate(270, bmpOriginal.getWidth()/2, bmpOriginal.getHeight()/2);
tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);
Drawable newMarker = new BitmapDrawable(getResources(),bmResult);
DefaultResourceProxyImpl resProxyImp = new DefaultResourceProxyImpl(getApplicationContext());
ItemizedIconOverlay markersOverlay = new ItemizedIconOverlay<OverlayItem>(items, newMarker, null, resProxyImp);
mapView.getOverlays().add(markersOverlay);
+1

, , ItemizedItemOverlay. onDrawItem() - :

@Override
protected void onDrawItem(ISafeCanvas canvas, Item item, Point curScreenCoords, float aMapOrientation) {

    int degrees = 90;
    canvas.save();
    canvas.rotate(degrees, curScreenCoords.x, curScreenCoords.y);
    super.onDrawItem(canvas, item, curScreenCoords, aMapOrientation);
    canvas.restore();
}
0

:

CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(centerCoordinates)      // Sets the center of the map
                .zoom(zoom)                   // Sets the zoom
                .bearing(bearing)             // Sets the orientation of the camera to east
                .tilt(30)                   // Sets the tilt of the camera to 30 degrees
                .build();                   // Creates a CameraPosition from the builder

googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
-2

All Articles