gpt4 book ai didi

android - 使用 MyLocationOverlay 绘制精度圆

转载 作者:搜寻专家 更新时间:2023-11-01 08:09:37 26 4
gpt4 key购买 nike

我正在开发一个显示带有用户位置的 map 的 Android 应用程序,到目前为止,我能够在线和离线显示 map 。现在我想知道如何使用 MylocationOverlay 在 osmdroid 中绘制与标记一起出现的精度圆?

这是我的代码:

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

最佳答案

一个完整的示例,用于在您的位置绘制一个精度圆(这不包括您的位置的标记)。这是基于 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);
}
}
}
}

然后在你的 Activity 中:

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

关于android - 使用 MyLocationOverlay 绘制精度圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11126709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com