Android地图覆盖手工绘制 [英] android maps overlay draw by hand
本文介绍了Android地图覆盖手工绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经完成动用我的叠加线条,但我怎么能画自由泳!?不固定样线和圆的几何形状,但只是借鉴的地方,我想!?
我已经拥有的是:
公共类OverlayMap扩展覆盖{
私人列表< MapGeoLine> geoLines =新的ArrayList< MapGeoLine>();
私人的GeoPoint geoFrom = NULL;
私人的GeoPoint geoTo = NULL; @覆盖
公共布尔onTouchEvent(MotionEvent motionEvent,MapView类MapView类){ 如果(motionEvent.getAction()== MotionEvent.ACTION_DOWN){
。geoFrom = MapView.getProjection()在fromPixels((int)的motionEvent.getX(),(INT)motionEvent.getY());
} 如果(motionEvent.getAction()== MotionEvent.ACTION_UP){
。geoTo = MapView.getProjection()在fromPixels((int)的motionEvent.getX(),(INT)motionEvent.getY());
} 如果(geoFrom = NULL&放大器;!&安培;!geoTo = NULL){
geoLines.add(新MapGeoLine(geoFrom,geoTo));
} 返回super.onTouchEvent(motionEvent,MapView类);
} @覆盖
公共无效画(油画画布,MapView类MapView类,布尔阴影){ 如果(geoLines.size()大于0){
涂料mPaint =新的油漆();
mPaint.setStrokeWidth(2);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setAntiAlias(真); 投影投影= MapView.getProjection()在;
路径path =新路径(); 对于(MapGeoLine行:geoLines){
Log.d(测试,P1+ line.getFrom()getLatitudeE6()+);
Log.d(测试,P2+ line.getTo()getLatitudeE6()+); 从=新的点对点();
点=新点(); projection.toPixels(line.getFrom(),从);
projection.toPixels(line.getTo(),到); path.moveTo(from.x,from.y);
path.lineTo(to.x,to.y);
} canvas.drawPath(路径,mPaint);
mapView.invalidate();
} super.draw(画布,MapView类,阴影);
}}
解决方案
您可以将您code beloow做到这一点:
公共类HandDrawOverlay扩展覆盖{ 私人布尔编辑模式= FALSE;
私人布尔isTouched = FALSE;
民营涂料粉刷=新的油漆();
私人点screenPt1 =新点();
私人点screenPt2 =新点();
私人的ArrayList<&GeoPoint的GT;点= NULL; 公共HandDrawOverlay(){
paint.setStrokeWidth(2.0F);
paint.setStyle(Style.STROKE);
paint.setColor(Color.BLUE);
} @覆盖
公共无效画(油画画布,MapView类MapView类,布尔阴影){
如果(点= NULL&放大器;!&安培; points.size()→1){
MapView.getProjection()在toPixels(points.get(0),screenPt1)。
的for(int i = 1; I< points.size();我++){
MapView.getProjection()在toPixels(points.get(ⅰ),screenPt2)。
canvas.drawLine(screenPt1.x,screenPt1.y,screenPt2.x,screenPt2.y,油漆);
screenPt1.set(screenPt2.x,screenPt2.y);
}
}
} @覆盖
公共布尔onTouchEvent(MotionEvent E,MapView类MapView类){
如果(编辑模式){
INT X =(int)的e.getX();
INT Y =(int)的e.getY();
。的GeoPoint geoP = MapView.getProjection()在fromPixels(X,Y); 开关(e.getAction()){
案例MotionEvent.ACTION_DOWN:
isTouched = TRUE;
点=新的ArrayList<&GeoPoint的GT;();
points.add(geoP);
打破;
案例MotionEvent.ACTION_MOVE:
如果(isTouched)
points.add(geoP);
打破;
案例MotionEvent.ACTION_UP:
如果(isTouched)
points.add(geoP);
isTouched = FALSE;
打破;
}
mapView.invalidate();
返回true;
}
返回false;
} / **
* @返回编辑模式
* /
公共布尔isEditMode(){
返回编辑模式;
} / **
* @参数编辑模式的编辑模式设置
* /
公共无效setEditMode(布尔编辑模式){
this.editMode =编辑模式;
}
}
问候。
I did already accomplish to draw lines on my overlay, but how can i draw freestyle!? Not fixed geometries like lines and circles but just draw wherever i want to!?
What i already have is:
public class OverlayMap extends Overlay {
private List<MapGeoLine> geoLines = new ArrayList<MapGeoLine>();
private GeoPoint geoFrom = null;
private GeoPoint geoTo = null;
@Override
public boolean onTouchEvent(MotionEvent motionEvent, MapView mapView) {
if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
geoFrom = mapView.getProjection().fromPixels((int)motionEvent.getX(),(int)motionEvent.getY());
}
if(motionEvent.getAction() == MotionEvent.ACTION_UP){
geoTo = mapView.getProjection().fromPixels((int)motionEvent.getX(),(int)motionEvent.getY());
}
if(geoFrom != null && geoTo != null){
geoLines.add(new MapGeoLine(geoFrom, geoTo));
}
return super.onTouchEvent(motionEvent, mapView);
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
if(geoLines.size() > 0){
Paint mPaint = new Paint();
mPaint.setStrokeWidth(2);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setAntiAlias(true);
Projection projection = mapView.getProjection();
Path path = new Path();
for(MapGeoLine line: geoLines){
Log.d("test", "p1: "+line.getFrom().getLatitudeE6()+ " ");
Log.d("test", "p2: "+line.getTo().getLatitudeE6()+ " ");
Point from = new Point();
Point to = new Point();
projection.toPixels(line.getFrom(), from);
projection.toPixels(line.getTo(), to);
path.moveTo(from.x, from.y);
path.lineTo(to.x, to.y);
}
canvas.drawPath(path, mPaint);
mapView.invalidate();
}
super.draw(canvas, mapView, shadow);
}
}
解决方案
You can you the code beloow to do it:
public class HandDrawOverlay extends Overlay {
private boolean editMode = false;
private boolean isTouched = false;
private Paint paint = new Paint();
private Point screenPt1 = new Point();
private Point screenPt2 = new Point();
private ArrayList<GeoPoint> points = null;
public HandDrawOverlay(){
paint.setStrokeWidth(2.0f);
paint.setStyle(Style.STROKE);
paint.setColor(Color.BLUE);
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
if(points != null && points.size() > 1){
mapView.getProjection().toPixels(points.get(0), screenPt1);
for(int i=1; i<points.size();i++){
mapView.getProjection().toPixels(points.get(i), screenPt2);
canvas.drawLine(screenPt1.x, screenPt1.y, screenPt2.x, screenPt2.y, paint);
screenPt1.set(screenPt2.x, screenPt2.y);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent e, MapView mapView) {
if(editMode){
int x = (int)e.getX();
int y = (int)e.getY();
GeoPoint geoP = mapView.getProjection().fromPixels(x,y);
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
isTouched = true;
points = new ArrayList<GeoPoint>();
points.add(geoP);
break;
case MotionEvent.ACTION_MOVE:
if(isTouched)
points.add(geoP);
break;
case MotionEvent.ACTION_UP:
if(isTouched)
points.add(geoP);
isTouched = false;
break;
}
mapView.invalidate();
return true;
}
return false;
}
/**
* @return the editMode
*/
public boolean isEditMode() {
return editMode;
}
/**
* @param editMode the editMode to set
*/
public void setEditMode(boolean editMode) {
this.editMode = editMode;
}
}
Regards.
这篇关于Android地图覆盖手工绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文