Question

Ok, this one has been bugging me for hours, I have the following relatively simple code that places a marker on a osmdroid map

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 is always facing the top of the screen (0deg reotation). How can I go about easily rotating each marker to a specified degree (360deg being a full circle)?

Was it helpful?

Solution

Try use RotateMyBitmap method like this:

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 RotateMyBitmap is:

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 is:

enter image description here

This works fine but I recommend you use MyLocationOverlay class

OTHER TIPS

If you don't want to add any custom code, you can also replace your ItemizedIconOverlay by a Marker from OSMBonusPack library (on top of osmdroid).

It supports setRotation(degreees).

It also supports "flat" or "billboard" modes, depending on what you want to happen if the map is also rotated.

How about this:

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);

Your best bet is probably to create a subclass of ItemizedItemOverlay. Override the onDrawItem() and try something like this:

@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();
}

Try this: only if you need rotating the camera

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));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top