在OSMDroid for Android中旋转标记?

时间:2013-12-11 12:54:21

标签: java android maps openstreetmap osmdroid

好的,这个问题一直困扰我几个小时,我有以下相对简单的代码,在osmdroid地图上放置一个标记

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

然而,标记始终面向屏幕顶部(0deg reotation)。如何轻松地将每个标记旋转到指定的程度(360度是一个完整的圆圈)?

5 个答案:

答案 0 :(得分:3)

尝试使用 RotateMyBitmap 方法,如下所示:

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

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

结果是:

enter image description here

这很好但我建议您使用MyLocationOverlay

答案 1 :(得分:2)

如果您不想添加任何自定义代码,也可以使用OSMBonusPack library(在osmdroid之上)用标记替换ItemizedIconOverlay

它支持setRotation(degreees)

它还支持“平面”或“广告牌”模式,具体取决于地图也旋转时您想要发生的事情。

答案 2 :(得分:1)

这个怎么样:

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

答案 3 :(得分:0)

您最好的选择可能是创建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();
}

答案 4 :(得分:-2)

尝试此操作:仅在您需要旋转相机时

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