在 MapView 上用圆(半径)绘制覆盖图
我想在MapView上画一个圆,即半径。我这样做:
覆盖绘制圆圈:
public class RadiusOverlay extends Overlay {
private Context context;
private double latitude;
private double longitude;
private float radius;
public RadiusOverlay(Context context, double latitude, double longitude, float radius){
/*.....................*/
}
public void draw(Canvas canvas, MapView mapView, boolean shadow){
super.draw(canvas, mapView, shadow);
Point point = new Point();
GeoPoint geoPoint = new GeoPoint((int) (latitude *1e6), (int)(longitude * 1e6));
Projection projection = mapView.getProjection();
projection.toPixels(geoPoint, point);
radius = projection.metersToEquatorPixels(radius);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setARGB(100, 100, 100, 100);
canvas.drawCircle((float)point.x, (float)point.y, radius, paint);
}
}
在 MapActivity:
List<Overlay> mapOverlays = mapView.getOverlays();
RadiusOverlay radiusOverlay = new RadiusOverlay(this, latitude, longitude, radius);
mapOverlays.add(radiusOverlay);
当 MapActivity 启动时,我看到圆圈,它是我想要的大小,但后来它增长到大小屏幕并消失。我该如何解决这个问题? 将不胜感激任何帮助。
I want to draw a circle on MapView, i.e. radius. I do it like this:
Overlay to draw circle:
public class RadiusOverlay extends Overlay {
private Context context;
private double latitude;
private double longitude;
private float radius;
public RadiusOverlay(Context context, double latitude, double longitude, float radius){
/*.....................*/
}
public void draw(Canvas canvas, MapView mapView, boolean shadow){
super.draw(canvas, mapView, shadow);
Point point = new Point();
GeoPoint geoPoint = new GeoPoint((int) (latitude *1e6), (int)(longitude * 1e6));
Projection projection = mapView.getProjection();
projection.toPixels(geoPoint, point);
radius = projection.metersToEquatorPixels(radius);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setARGB(100, 100, 100, 100);
canvas.drawCircle((float)point.x, (float)point.y, radius, paint);
}
}
In MapActivity:
List<Overlay> mapOverlays = mapView.getOverlays();
RadiusOverlay radiusOverlay = new RadiusOverlay(this, latitude, longitude, radius);
mapOverlays.add(radiusOverlay);
I see circle when MapActivity starts, and it's the size I intended, but then it grows to the size of the screen and disappears. How do I fix this?
Will appreciate any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您每次绘制时都使用
metersToEquatorPixels
重新计算半径。您应该将该计算移至构造函数,看看是否可以解决您的问题。编辑:
正如奥尔加斯指出的那样,投影可以改变。因此,您要做的就是按原样存储原始的“半径”值,然后在您的绘制方法中执行以下操作:
并在稍后的绘制中使用该值:
It looks like you are re-calculating the radius using
metersToEquatorPixels
every time you draw it. You should move that calculation to the constructor and see if that fixes your problem.EDIT:
As Olgas pointed out, the Projection can change. Therefore what you want to do is store the original `radius value as you are, then in your draw method do:
and use this value later in draw as well: