2013-12-11 248 views
4

好吧,這一個已被竊聽了我幾個小時,我有以下幾個比較簡單的代碼,放置一個標記一個osmdroid地圖在OSMDroid for Android中旋轉標記?

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)的頂部。我怎樣才能輕鬆地將每個標記旋轉到指定的程度(360deg是一個整圓)?

+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

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

不幸的是,這是一個谷歌地圖,而不是OSM,並旋轉整個地圖,而不僅僅是標記。感謝至少回答:-) – Apqu

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

如果你不想添加任何自定義代碼,也可以取代你的ItemizedIconOverlay通過來自OSMBonusPack library的標記(在osmdroid之上)。

它支持setRotation(degreees)

它還支持「平面」或「廣告牌」模式,具體取決於您在地圖旋轉時想要發生的情況。