android - 围绕我的位置画圈

标签 android geolocation geometry ondraw

我正在尝试在我的位置周围画一个圆圈。我不确定我做错了什么,但圆圈没有显示:

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    Projection projection = mapView.getProjection();

    if (shadow == false && location != null) {
        // Get the current location
        Double latitude = location.getLatitude() * 1E6;
        Double longitude = location.getLongitude() * 1E6;
        GeoPoint geoPoint = new GeoPoint(latitude.intValue(),
                longitude.intValue());

        int radius = metersToRadius(100, mapView, latitude);

        // Convert the location to screen pixels
        Point point = new Point();
        projection.toPixels(geoPoint, point);

        // Setup the paint
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(2.0f);

        paint.setColor(0xff6666ff);
        paint.setStyle(Style.STROKE);
        canvas.drawCircle(point.x, point.y, radius, paint);

        paint.setColor(0x186666ff);
        paint.setStyle(Style.FILL);
        canvas.drawCircle(point.x, point.y, radius, paint);
    }
    super.draw(canvas, mapView, shadow);
}

编辑:为了明确起见,我将发布我的类(class): 自定义项目叠加层

public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {

protected final List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

protected final Context mContext;

public CustomItemizedOverlay(Drawable defaultMarker, Context context) {
    super(boundCenterBottom(defaultMarker));
    this.mContext = context;
}

public void addOverlay(OverlayItem overlay) {
    mOverlays.add(overlay);
    populate();
}

@Override
protected OverlayItem createItem(int i) {
    // TODO Auto-generated method stub
    return mOverlays.get(i);
}

public void removeOverlay(OverlayItem overlay) {
    mOverlays.remove(overlay);
    populate();
}

public void clear() {
    mOverlays.clear();
    populate();
}

@Override
public int size() {
    // TODO Auto-generated method stub
    return mOverlays.size();
}

@Override
protected boolean onTap(int i) {
    OverlayItem itemClicked = this.mOverlays.get(i);

    AlertDialog.Builder builder = new AlertDialog.Builder(this.mContext);

    builder.setTitle(itemClicked.getTitle());
    builder.setMessage(itemClicked.getSnippet());
    builder.setCancelable(true);

    AlertDialog alert = builder.create();
    alert.show();

    return true;
}

和PCCustomizedOverlay

public class PcCustomItemizedOverlay extends CustomItemizedOverlay {

public static int metersToRadius(float meters, MapView map, double latitude) {
    return (int) (map.getProjection().metersToEquatorPixels(meters) * (1 / Math
            .cos(Math.toRadians(latitude))));
}

private Location location;

public PcCustomItemizedOverlay(Drawable defaultMarker, Context context) {
    super(defaultMarker, context);
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    Projection projection = mapView.getProjection();

    if (shadow == false && location != null) {
        // Get the current location
        Double latitude = location.getLatitude() * 1E6;
        Double longitude = location.getLongitude() * 1E6;
        GeoPoint geoPoint = new GeoPoint(latitude.intValue(),
                longitude.intValue());

        int radius = metersToRadius(40, mapView, latitude);

        // Convert the location to screen pixels
        Point point = new Point();
        projection.toPixels(geoPoint, point);

        // Setup the paint
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(2.0f);

        paint.setColor(0xff6666ff);
        paint.setStyle(Style.STROKE);
        canvas.drawCircle(point.x, point.y, radius, paint);

        paint.setColor(0x186666ff);
        paint.setStyle(Style.FILL);
        canvas.drawCircle(point.x, point.y, radius, paint);
    }
    super.draw(canvas, mapView, shadow);
}

public Location getLocation() {
    return location;
}

public void setLocation(Location location) {
    this.location = location;
}

}

有谁知道问题出在哪里吗?

非常感谢

最佳答案

试试这个代码

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mapView = (MapView) findViewById(R.id.mapview);
    MapController mc = mapView.getController();
    MyLocationOverlay myLocationOverlay = new MyLocationOverlay(MainMap.this, mapView);
    mapView.getOverlays().add(myLocationOverlay);
    mc.animateTo( new GeoPoint(lat, lng));
    mc.setZoom(15);
    mapView.invalidate();
}

不要忘记添加overlay.enableMyLocation();在onresume()和overlay.disableMyLocation()中;暂停中

如果您想在您的点周围绘制圆圈,您可以使用以下示例代码来代替上面的代码:

Point screenPnts =new Point();
GeoPoint curr_geopoint = new GeoPoint((int)(location.getLatitude()*1E6),(int)(location.getLongitude()*1E6));
mapview.getProjection().toPixels(curr_geopoint, screenPnts);
canvas.drawCircle(screenPnts.x, screenPnts.y, 15, paint);

通过操作 screenPnts.x 和 screenPnts.y 值进行一些尝试和错误,以获得围绕该点的圆圈。这里paint是Paint类的对象,用于给圆赋予颜色

关于android - 围绕我的位置画圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10551204/

相关文章:

python - 如何在Pygame中慢慢画一个圆圈?

algorithm - N个圆的共同重叠

Android TTS 停止音乐播放器

javascript - iPhone JavaScript Web 应用程序的地理定位

algorithm - 在不规则形状内渲染 CoreText

android - Cordova android地理定位超时

android - 在应用程序启动期间获取当前位置

android - 在 Imageview 中使用 Glide 加载图片

java - 如何传递对当前 Activity 的引用

android - 当子 ViewPager 在最后一项时,如何防止父 ViewPager 滚动?