Android如何使用覆盖图在MapView中徒手绘制绘画?

2023-12-29

在我的应用程序中,在地图视图上徒手绘制油漆,但搜索了大量信息,最终从地图视图上绘制的矩形形状中获得,但我想代替像之字形那样徒手绘制矩形,如何更改我的代码请提供任何帮助..

MapOverlay.java

  public class MapOverlay extends Overlay {

private float x1,y1,x2,y2;
private GeoPoint p1=null,p2=null;
private MapExampleActivity mv = null;
private Paint paint = new Paint();
private Path path = new Path();
private boolean isUp = false;

//constructor receiving the initial point
  public MapOverlay(MapExampleActivity mapV,float x,float y){
    paint.setStrokeWidth(2.0f);
    x1 = x;
    y1 = y;
    mv = mapV;
    p1 = mapV.getMapView().getProjection().fromPixels((int)x1,(int)y1);
}
//override draw method to add our custom drawings
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {

    if(p1 != null && p2 != null){
        //get the 2 geopoints defining the area and transform them to pixels
        //this way if we move or zoom the map rectangle will follow accordingly
        Point screenPts1 = new Point();
        mapView.getProjection().toPixels(p1, screenPts1);
        Point screenPts2 = new Point();
        mapView.getProjection().toPixels(p2, screenPts2);                

        //draw inner rectangle
        paint.setColor(Color.BLUE);
     //   paint.setStyle(Style.FILL);
        canvas.drawPath(path, paint);       
        canvas.drawRect(screenPts1.x, screenPts1.y, screenPts2.x, screenPts2.y, paint);
        //draw outline rectangle

    //  paint.setColor(Color.YELLOW);
        paint.setStyle(Style.STROKE);
    //  canvas.drawRect(screenPts1.x, screenPts1.y, screenPts2.x, screenPts2.y, paint);
        canvas.drawPath(path, paint); 
    }
    return true;
}    

@Override
public boolean onTouchEvent(MotionEvent e, MapView mapView) {
    if(mv.isEditMode() && !isUp){

        if(e.getAction() == MotionEvent.ACTION_DOWN){
            x1 = y1 = 0;
            x1 = e.getX();
            y1 = e.getY();
            p1 = mapView.getProjection().fromPixels((int)x1,(int)y1);               
        }

        //here we constantly change geopoint p2 as we move out finger
        if(e.getAction() == MotionEvent.ACTION_MOVE){
            x2 = e.getX();
            y2 = e.getY();
            p2 = mapView.getProjection().fromPixels((int)x2,(int)y2);               
        }

        //---when user lifts his finger---
        if (e.getAction() == MotionEvent.ACTION_UP) {                
            isUp = true;
        }    
        return true;
    }
    return false;
}

}

使用这个我可以像这样绘制矩形形状并再次绘制,然后单击切换按钮(可以绘制多次)

我想要画线而不是像下图那样的矩形(画多次)。

最后我找到了这个链接这个链接提供了矩形绘制http://n3vrax.wordpress.com/2011/08/13/drawing-overlays-on-android-map-view/ http://n3vrax.wordpress.com/2011/08/13/drawing-overlays-on-android-map-view/

只需更改矩形即可自由绘制任何想法,请......


您可以使用以下代码徒手画一条线:

Code

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

to use

HandDrawOverlay handDrawOverlay;
handDrawOverlay = new HandDrawOverlay();
mapView.getOverlays().add(handDrawOverlay);

//Set edit mode to true to start drwaing
handDrawOverlay.setEditMode(true);

//Set edit mode to true to stop drwaing
handDrawOverlay.setEditMode(false);

Note

这是一个功能齐全的示例,可帮助您入门。但是,您应该优化代码以使其更加高效(即使用Path将绘图路径存储在onDraw(),减少记录的点数onTouch(), etc.).

好好享受。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android如何使用覆盖图在MapView中徒手绘制绘画? 的相关文章

随机推荐

  • 修改linux内核定时器

    我必须运行延迟敏感的应用程序 并且被要求将计时器分辨率更改为 1000 Hz 或更高 我在网上搜索了一下 找到了有关 CONFIG HZ 等的页面 但是 文件中似乎还有其他几个相关设置 因此我想确保不会弄乱这些设置 我在这里发布一些输出 c
  • C++:指向 std::string 转换的 char 指针是否复制内容?

    当我转换一个char to std string使用构造函数 char ps Hello std string str ps 我知道 std 容器在被要求存储值时倾向于复制值 是复制整个字符串还是仅复制指针 如果之后我这样做str Bye
  • 包“com.sun.webkit.dom”在模块“javafx.web”中声明,该模块不会将其导出到模块

    试图从Java 8 to Java 9 我得到这个错误 包 com sun webkit dom 在模块 javafx web 中声明 其中 不将其导出到模块 我该如何解决这个问题 以便预编译器 Intellij 和运行时我不明白这个问题
  • 语法平衡问题

    是否可以强制灵气提升以这种方式表现 生成的语法可以根据一些运行时可计算的条件 规则 速率进行调整 例如 输入由语言结构组成 这会在解析过程中导致不同的选择 有些更频繁 另一些则更少 但替代方案的顺序会影响效率 即语法的运行时最优性 在某些情
  • 上标和下标有 Ansi 转义序列吗?

    我正在玩弄 ANSI 转义序列 例如 echo e e 91mHello e m 在 Linux 控制台上显示彩色文本 现在我尝试使用上标和下标输出 例如a b I read here http www inwap com pdp10 an
  • Jackson JSON 和 Hibernate JPA 的无限递归问题(又一个)

    我找到了大量这方面的资源 例如这个 Jackson JSON 和 Hibernate JPA 问题的无限递归 https stackoverflow com questions 3325387 infinite recursion with
  • 使用 asp.net mvc 用逗号分隔数字

    我正在开发 MVC2 应用程序 我使用数据注释来验证数据 客户端和服务器端 我的模型中有几个字段只允许十进制值 一旦用户输入十进制值 我希望将其转换为逗号分隔的更易读的格式 例如 1200 应格式化为 1 200 而 500 应保持原样 这
  • JavaScript API 不适用于 Excel 2013?

    我刚刚收到了针对我提交的一个加载项的更改建议报告 它说Your add in is not working in the Excel 2013 client on Windows 7 with Internet Explorer 11 我一
  • 如何将 request.getParameterNames 放入字符串列表中?

    是否可以得到request getParameterNames 作为字符串列表 我需要以这种形式提供它 Just 构造 http docs oracle com javase 6 docs api java util ArrayList h
  • memset 不适用于指向字符的指针

    下面的代码有什么问题 memset 应该与指向要填充的内存块的指针一起使用 但此代码在控制台中显示问题 提示分段错误 核心已转储 include
  • 使用 javascript 或 jquery 从外部 xml 文件加载数据

    Hi 我有一个如下所示的 xml 文件
  • Apple ID 可从 Cocoa Touch 内的应用程序下载中检索

    你好 我正在开发一个 iPhone 应用程序 每次下载都需要维护一个唯一的标识符 有没有办法检索用于下载应用程序的 Apple ID 或其他标识符 我可以使用该标识符将所有设备与该下载链接到远程数据库 服务 基本上我想要一个自定义生成的数据
  • Git 递归地将遥控器添加到子模块

    我的机器上有一个 git 存储库 地址为 path to repo 其中包含几个子模块 path to repo submoduleA and path to repo foo bar submoduleB 由于我无法更改工作流程 git
  • Node.js - 注册时发送电子邮件

    我有带有单个电子邮件字段的注册表单 当用户输入电子邮件时 我需要发送注册链接 我见过this https github com alexyoung nodepad blob master app js带有注册表单的 Node js 示例 但
  • 如何将角度应用程序嵌入到另一个应用程序中?

    我的团队开发了一个 Angular 5 应用程序 该应用程序已经投入生产一段时间了 但我们最近的任务是让该应用程序在公司拥有的其他 3 个站点上运行 一个站点是使用 Angular6 构建的 SPA 另一个站点也是 SPA 但使用 Angu
  • 单独的 DataGrid 行可见性

    我有一个WPFDataGrid绑定到一个集合Entity Framework父 EF 对象内部的对象 大致如下
  • jQuery text() 调用在 Firefox 中保留换行符,但在 IE 中则不然

    我正在做 alert div text 在这样的事情上 div lt div gt Some text lt div gt div 为什么要转义内容 因为它有时格式错误 我不希望它干扰或破坏文档的其余部分 在 FF 中它显示保留换行符 在
  • 如何在计划查询中显示和更改用户

    Google Cloud Platform 中的一些计划查询突然不再运行 并显示消息 访问被拒绝 用户没有表的 bigquery tables get 权限 首先 是否可以查看计划查询在哪个用户下运行 第二 可以更改用户吗 谢谢 西尔万 我
  • Rails 5 中具有多态关联的嵌套属性

    我创建了一个Address具有多态关联的模型 我试图通过客户端模型的嵌套属性保存到它 但我得到Address addressable must exist in the client errors Models class Client l
  • Android如何使用覆盖图在MapView中徒手绘制绘画?

    在我的应用程序中 在地图视图上徒手绘制油漆 但搜索了大量信息 最终从地图视图上绘制的矩形形状中获得 但我想代替像之字形那样徒手绘制矩形 如何更改我的代码请提供任何帮助 MapOverlay java public class MapOver