【问题标题】:drawing circle around my location在我的位置周围画圈
【发布时间】:2012-05-11 12:18:43
【问题描述】:

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

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

编辑:为了清楚起见,我将发布我的课程: CustomItemizedOverlay

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

}

有谁知道问题出在哪里?

非常感谢

【问题讨论】:

  • 如果我是对的,您正在尝试实现与谷歌地图中相同的 mylocation 图像?
  • @Akki 是的,这就是我正在尝试的,但我没有使用 MyLocationOverlay,只是一个扩展 ItemizedOverlay 的类

标签: android geolocation geometry ondraw


【解决方案1】:

试试这个代码

@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 类的对象,用于为圆圈赋予颜色

【讨论】:

  • 谢谢,但 MyLocationOverlay 类限制了我的应用太多,我想使用 ItemizedOverlay。
【解决方案2】:

我认为你的void draw有问题。

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    super.draw(canvas, mapView, 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);
    }

}

也可以在这里查看将图像放在地图上。drawing image as mapoverlay

【讨论】:

  • 有没有办法只用 ItemizedOverlay 来实现它?
【解决方案3】:

我也遇到过类似的问题。

在扩展 Overlay 的内部类中使用覆盖布尔绘制而不是 void one 来解决它。

看起来像这样:

 //inner class MapOverlay
class MapOverlay extends com.google.android.maps.Overlay
{
    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) 
    {
        super.draw(canvas, mapView, shadow);                   

        Projection projection = mapView.getProjection();

        //the rest of your code here................

        super.draw(canvas,mapView,shadow);

        return true;
    }
}

构建你的圈子
MapOverlay mapOverlayCircle = new MapOverlay();

并将其添加到 mapView 中的叠加层。就是这样。

【讨论】:

  • 谢谢,但我想扩展 ItemizedOverlay,而不是 Overlay
猜你喜欢
  • 1970-01-01
  • 2020-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-05
  • 1970-01-01
  • 1970-01-01
  • 2021-04-11
相关资源
最近更新 更多