[英]Rotate a Marker in OSMDroid for Android?
好的,這個已經困擾了我幾個小時,我有以下相對簡單的代碼,在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度是一個完整的圓圈)?
嘗試使用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);
}
結果是:
這工作正常,但我建議你使用MyLocationOverlay類
如果您不想添加任何自定義代碼,還可以通過OSMBonusPack庫中的Marker(在osmdroid之上)替換ItemizedIconOverlay
。
它支持setRotation(degreees)
。
它還支持“平面”或“廣告牌”模式,具體取決於地圖也旋轉時您想要發生的事情。
這個怎么樣:
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);
您最好的選擇可能是創建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();
}
試試這個:只有你需要旋轉相機
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));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.