2012-06-20 69 views
2

我正在使用用戶位置顯示地圖的Android應用程序,到目前爲止,我可以在線和離線顯示地圖。現在我想知道如何使用MylocationOverlay在osmdroid中繪製一個與標記一起出現的精確度圓圈?使用MyLocationOverlay繪製精度圓

這裏是我的代碼:

public class AhmedActivity extends Activity 
{ 
    /** Called when the activity is first created. */ 
    private MapView mapView; 
    private MapController mapController; 
    private ScaleBarOverlay mScaleBarOverlay; 
    MyLocationOverlay myLocationOverlay = null; 
    private LocationManager locationManager; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // Initialize the location: 
     locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

     initializemap(); 

     /* My location overlay */ 
     /* Create a static Overlay showing a the current location and a compass */     
     myLocationOverlay = new MyLocationOverlay(this, mapView); 
     mapView.getOverlays().add(myLocationOverlay); 
     myLocationOverlay.runOnFirstFix(new Runnable() { 
      public void run() { 
       mapController.animateTo(myLocationOverlay 
           .getMyLocation()); 
      } 
     }); 

     //Add Scale Bar 
     mScaleBarOverlay = new ScaleBarOverlay(this);       
     ScaleBarOverlay myScaleBarOverlay = new ScaleBarOverlay(this); 
     mScaleBarOverlay.setLineWidth(50); 
     mapView.getOverlays().add(myScaleBarOverlay); 
    } 

    /** (re-)enable location and compass updates */  
    @Override 
    public void onResume() { 
     super.onResume(); 
     myLocationOverlay.enableCompass(); 
     myLocationOverlay.enableMyLocation(); 
     //myLocationOverlay.followLocation(true); 
     //myLocationOverlay.setDrawAccuracyEnabled(true); 
     //myLocationOverlay.isDrawAccuracyEnabled(); 
    } 

    /** disable compass and location updates */   
    @Override  
    public void onPause() { 
     super.onPause(); 
     myLocationOverlay.disableMyLocation(); 
     myLocationOverlay.disableCompass(); 
    } 

    public void initializemap() 
    { 
     mapView = (MapView) this.findViewById(R.id.mapView); 
     mapView.setTileSource(TileSourceFactory.MAPNIK); 
     mapView.setBuiltInZoomControls(true); 
     mapView.setMultiTouchControls(true); 
     mapController = this.mapView.getController();  
     mapController.setZoom(12); 
     mapController.setCenter(new GeoPoint(15.610762,32.540345)); 
    } 
} 

回答

1

阿波7med,

您需要擴展覆蓋例如

public class AccuracyCircleOverlay extends Overlay { 
    public AccuracyCircleOverlay() {} 
    public void draw(Canvas canvas, MapView mapv, boolean shadow){ 
     super.draw(canvas, mapv, shadow); 
     // your drawing code here 
    } 
} 

您可以覆蓋添加到您的視圖,例如:

mapView.getOverlays().add(new AccuracyCircleOverlay()); 
0

A fully worked exa可以在您的位置繪製精確圓圈(這不包括您的位置標記)。這是基於OSMDroid類DirectedLocationOverlay用於顯示您的位置與方向箭頭(我不需要和刪除)。

import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Point; 

import org.osmdroid.util.GeoPoint; 
import org.osmdroid.views.MapView; 
import org.osmdroid.views.Projection; 
import org.osmdroid.views.overlay.Overlay; 

public class AccuracyOverlay extends Overlay { 

    private Paint paint = new Paint(); 
    private Paint accuracyPaint = new Paint(); 
    private GeoPoint location; 
    private final Point screenCoords = new Point(); 
    private float accuracy = 0; 

    public AccuracyOverlay(GeoPoint location, float accuracyInMeters) { 
     super(); 

     this.location = location; 
     this.accuracy = accuracyInMeters; 
     this.accuracyPaint.setStrokeWidth(2); 
     this.accuracyPaint.setColor(Color.BLUE); 
     this.accuracyPaint.setAntiAlias(true); 
    } 

    @Override 
    public void onDetach(MapView view){ 
     paint = null; 
     accuracyPaint = null; 
    } 

    @Override 
    public void draw(final Canvas c, final MapView map, final boolean shadow) { 

     if (shadow) { 
      return; 
     } 

     if (location != null) { 
      final Projection pj = map.getProjection(); 
      pj.toPixels(location, screenCoords); 

      if (accuracy > 0) { //Set this to a minimum pixel size to hide if accuracy high enough 
       final float accuracyRadius = pj.metersToEquatorPixels(accuracy); 

       /* Draw the inner shadow. */ 
       accuracyPaint.setAntiAlias(false); 
       accuracyPaint.setAlpha(30); 
       accuracyPaint.setStyle(Paint.Style.FILL); 
       c.drawCircle(screenCoords.x, screenCoords.y, accuracyRadius, accuracyPaint); 

       /* Draw the edge. */ 
       accuracyPaint.setAntiAlias(true); 
       accuracyPaint.setAlpha(150); 
       accuracyPaint.setStyle(Paint.Style.STROKE); 
       c.drawCircle(screenCoords.x, screenCoords.y, accuracyRadius, accuracyPaint); 
      } 
     } 
    } 
} 

然後在您的活動:

@Override 
public void onLocationChanged(Location location) { 
    if (accuracyOverlay != null) { 
     map.getOverlays().remove(accuracyOverlay); 
     map.invalidate(); 
    } 

    accuracyOverlay = new AccuracyOverlay(new GeoPoint(location), location.getAccuracy()); 
    map.getOverlays().add(accuracyOverlay); 
}