百度地图SDK Android版开发 11 覆盖物示例 4 线

news2024/9/20 16:39:51

百度地图SDK Android版开发 11 覆盖物示例 4 线

  • 前言
  • 界面布局
  • MapPolyline类
    • 常量
    • 成员变量
    • 初始值
    • 创建覆盖物
    • 移除覆盖物
    • 设置属性
    • 加载地图和释放地图
  • MapPolylineActivity类
    • 控件响应事件
  • 运行效果图

前言

文本通过创建多个不同线宽的折线和大地曲线,介绍Polyline的使用方法。

线的属性包括是否可点击、是否虚线、分段属性、发光属性和大地曲线。

  • 分段属性包括分段颜色分段纹理渐变色。只能选一或不选。
  • 发光属性包括渐变发光模糊发光。只能选一或不选。
  • 分段属性发光属性大地曲线只能选一或不选。

为了直观显示线的属性,仅使用了CheckBox控件,多种属性只能选一通过代码实现。

界面布局

在这里插入图片描述

  • 布局文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.baidudemo.MapMarkerActivity">

    <com.baidu.mapapi.map.MapView
        android:id="@+id/bmapView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:clickable="true"
        app:layout_constraintBottom_toTopOf="@id/bottomView"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/bottomView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/bmapView">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/background_dark"
            android:gravity="center_horizontal"
            android:orientation="horizontal"
            android:paddingHorizontal="10dp">

            <CheckBox
                android:id="@+id/clickable"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:checked="true"
                android:onClick="setMarkerFlag"
                android:text="点击"
                android:textColor="@color/white"
                android:textStyle="bold" />

            <CheckBox
                android:id="@+id/dottedLine"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="setMarkerFlag"
                android:text="虚线"
                android:textColor="@color/white"
                android:textStyle="bold" />

            <CheckBox
                android:id="@+id/colors"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="setMarkerFlag"
                android:text="分段颜色"
                android:textColor="@color/yellow_800"
                android:textStyle="bold" />

            <CheckBox
                android:id="@+id/textures"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="setMarkerFlag"
                android:text="分段纹理"
                android:textColor="@color/yellow_800"
                android:textStyle="bold" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center"
            android:background="@android:color/background_dark"
            android:orientation="horizontal"
            android:paddingHorizontal="10dp">

            <CheckBox
                android:id="@+id/gradient"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:checked="false"
                android:onClick="setMarkerFlag"
                android:text="渐变色"
                android:textColor="@color/yellow_800"
                android:textStyle="bold" />

            <CheckBox
                android:id="@+id/bloomGradientA"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="setMarkerFlag"
                android:text="渐变发光"
                android:textColor="@color/blue_500"
                android:textStyle="bold" />

            <CheckBox
                android:id="@+id/bloomBlur"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="setMarkerFlag"
                android:text="模糊发光"
                android:textColor="@color/blue_500"
                android:textStyle="bold" />

            <CheckBox
                android:id="@+id/geodesic"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="setMarkerFlag"
                android:text="大地曲线"
                android:textColor="@color/white"
                android:textStyle="bold" />

        </LinearLayout>
    </androidx.appcompat.widget.LinearLayoutCompat>
</androidx.constraintlayout.widget.ConstraintLayout>

MapPolyline类

以下为MapPolyline类部分代码。

常量

public static final String CLICKABLE = "clickable"; // 点击
public static final String DOTTED_LINE = "DottedLine"; // 虚线
public static final String COLORS = "Colors"; // 分段颜色
public static final String TEXTURES = "Textures"; // 分段纹理
public static final String GRADIENT = "Gradient"; // 渐变色
public static final String BLOOM_GRADIENT_A = "BloomGradientA"; // 渐变发光
public static final String BLOOM_BLUR = "BloomBlur"; // 模糊发光
public static final String GEODESIC = "Geodesic"; // 大地曲线
  • 渐变色
final static int COLOR_RED = 0xAAD50000;
final static int COLOR_YELLOW = 0xAAF57F17;
final static int COLOR_BLUE = 0xAA0D47A1;
final static int COLOR_GREEN = 0xAA33691E;
  • 折线线宽:多个折线的宽度依次为5,10, 15, ……

关于折线宽度(来自API参考)

设置折线线宽, 默认为 5, 单位:像素 需要注意的是:Polyline的宽度适配地图当前缩放级别下的像素与地理范围的对应关系

final static int LINE_WIDTH_MIN = 5;
final static int LINE_WIDTH_STEP = 5;

成员变量

// 覆盖物列表
List<Overlay> overlays = new ArrayList<>();
// 选中的状态
List<String> selectedFlags = new ArrayList<>();

List<List<LatLng>> lines = new ArrayList<>(); // 多个折线
List<LatLng> geodesicLine = new ArrayList<>(); // 大地曲线坐标 仅两个点
ArrayList<BitmapDescriptor> bitmaps = new ArrayList<>(); // 纹理
List<Integer> colorValues = new ArrayList<>(); // 渐变色

初始值

  • 默认选中选项
selectedFlags.add(CLICKABLE);
  • 多个折线和大地曲线
double latSpan = 0.05;
for (int i = 0; i < 5; ++i) {
    List<LatLng> points = new ArrayList<>();
    points.add(new LatLng(39.865 + latSpan * i, 116.254));
    points.add(new LatLng(39.865 + latSpan * i, 116.304));
    points.add(new LatLng(39.825 + latSpan * i, 116.354));
    points.add(new LatLng(39.855 + latSpan * i, 116.394));
    points.add(new LatLng(39.805 + latSpan * i, 116.454));
    points.add(new LatLng(39.865 + latSpan * i, 116.504));
    points.add(new LatLng(39.805 + latSpan * i, 116.544));
    lines.add(points);
}

geodesicLine.add(new LatLng(36.53, -121.47));
geodesicLine.add(new LatLng(22.33, 114));
  • 纹理和颜色(纹理图来自官方Demo)
final String[] assetNames = {
        "Icon_road_red_arrow.png",
        "Icon_road_yellow_arrow.png",
        "Icon_road_blue_arrow.png",
        "Icon_road_green_arrow.png",
};
for (String name : assetNames) {
    bitmaps.add(BitmapDescriptorFactory.fromAsset(name));
}

final int[] colors = {COLOR_RED, COLOR_YELLOW, COLOR_BLUE, COLOR_GREEN};
for (int color : colors)
    colorValues.add(color);
  • 点击事件
initEvent();
private void initEvent() {
    map.setOnPolylineClickListener(new BaiduMap.OnPolylineClickListener() {
        @Override
        public boolean onPolylineClick(Polyline polyline) {
            showToast("click polyline");
            return true;
        }
    });
}

创建覆盖物

  • 创建大地曲线或创建多个折线
public void addOverlays() {
    if (selectedFlags.contains(GEODESIC)) {
        // 创建大地曲线
        PolylineOptions polylineOptions = new PolylineOptions()
                .width(LINE_WIDTH_MIN)
                .color(COLOR_RED)
                // 设置Polyline头尾形状类型
                .lineCapType(PolylineOptions.LineCapType.LineCapRound)
                // 设置Polyline拐点衔接的形状类型
                .lineJoinType(PolylineOptions.LineJoinType.LineJoinRound)
                .points(geodesicLine);

        setOption(polylineOptions, selectedFlags);

        // 在地图上绘制折线
        Polyline polyline = (Polyline) map.addOverlay(polylineOptions);
        overlays.add(polyline);
        return;
    }

    // 创建多个折线
    int width = LINE_WIDTH_MIN;
    for (List<LatLng> points : lines) {
        PolylineOptions polylineOptions = new PolylineOptions()
                .width(width)
                .color(COLOR_RED)
                // 设置Polyline头尾形状类型
                .lineCapType(PolylineOptions.LineCapType.LineCapRound)
                // 设置Polyline拐点衔接的形状类型
                .lineJoinType(PolylineOptions.LineJoinType.LineJoinRound)
                .points(points);

        setOption(polylineOptions, selectedFlags);

        // 在地图上绘制折线
        Polyline polyline = (Polyline) map.addOverlay(polylineOptions);
        overlays.add(polyline);

        width += LINE_WIDTH_STEP;
    }
}
  • 设置折线属性
private void setOption(PolylineOptions option, List<String> flags) {
    if (flags.contains(CLICKABLE))
        option.clickable(true);
    else
        option.clickable(false);

    if (flags.contains(DOTTED_LINE)) {
        // 设置折线显示为虚线
        option.dottedLine(true);
        // 设置虚线形状
        // PolylineDottedLineType.DOTTED_LINE_SQUARE
        // PolylineDottedLineType.DOTTED_LINE_CIRCLE
        option.dottedLineType(PolylineDottedLineType.DOTTED_LINE_SQUARE);
    }

    if (flags.contains(COLORS)) {
        // 设置折线每个点的颜色值
        // 每一个点带一个颜色值,绘制时按照索引依次取值 颜色个数 >= points的个数,
        // 若colors越界大于点个数,则取最后一个颜色绘制
        // 注意颜色值得格式为:0xAARRGGBB
        option.colorsValues(colorValues);
    }

    if (flags.contains(TEXTURES)) {
        // 添加纹理索引
        List<Integer> indexList = new ArrayList<>();
        for (int i = 0; i < bitmaps.size(); ++i)
            indexList.add(i);

        //
        option.customTextureList(bitmaps);
        // 设置纹理列表
        option.textureIndex(indexList);
    }

    if (flags.contains(GRADIENT)) {
        // 渐变色折线
        option.isGradient(true);
        option.colorsValues(colorValues);
    }

    if (flags.contains(BLOOM_GRADIENT_A)) {
        // 设置发光模式
        option.bloomType(PolylineOptions.LineBloomType.GradientA);
        // 设置发光下的宽度
        option.bloomWidth(option.getWidth() * 2);
        // 设置发光透明度(0~255) 默认线段透明度
        option.bloomAlpha(0xAA);
        // 设置透明度渐变发光效果的渐变速率(1.0 ~ 10.0)默认5.0f
        option.setBloomGradientASpeed(5.0f);
    }

    if (flags.contains(BLOOM_BLUR)) {
        // 设置发光模式
        option.bloomType(PolylineOptions.LineBloomType.BLUR);
        // 设置发光下的宽度
        option.bloomWidth(option.getWidth() * 2);
        // 设置发光透明度(0~255) 默认线段透明度
        option.bloomAlpha(0xAA);
        // 设置模糊发光效果的模糊次数(1~10次) 默认1次
        option.setBloomBlurTimes(2);
    }

    if (flags.contains(GEODESIC)) {
        // 大地曲线
        option.isGeodesic(true);
        // 折线经度跨180需增加此字段
        option.lineDirectionCross180(PolylineOptions.LineDirectionCross180.FROM_WEST_TO_EAST);
    }
}

移除覆盖物

public void removeOverlay() {
    //map.removeOverLays(overlays);
    for (Overlay overlay : overlays)
        overlay.remove();
    overlays.clear();
}

设置属性

public void setFlags(List<String> flags) {
    selectedFlags.clear();
    selectedFlags.addAll(flags);

    removeOverlay();
    addOverlays();
}

加载地图和释放地图

public void onMapLoaded() {
    initEvent();
    addOverlays();
}

public void onMapDestroy() {
    removeOverlay();

    for (BitmapDescriptor bitmap : bitmaps) {
        bitmap.recycle();
    }
    bitmaps = null;
}

MapPolylineActivity类

以下是MapPolylineActivity类部分代码

控件响应事件

**说明:**为了直观显示线的属性,仅使用了CheckBox控件,多种属性只能选一通过代码实现。

public void setMarkerFlag(View view) {
    // 分段颜色、纹理、渐变、大地曲线
    List<Integer> group1 = Arrays.asList(R.id.colors, R.id.textures, R.id.gradient, R.id.geodesic);
    // 分段颜色、纹理、渐变
    List<Integer> group2 = Arrays.asList(R.id.bloomGradientA, R.id.bloomBlur, R.id.geodesic);

    boolean checked = ((CheckBox) view).isChecked();
    int id = view.getId();
    if (checked) {
        if (group1.contains(id)) {
            for (int checkBoxId : group1) {
                if (checkBoxId != id) {
                    ((CheckBox) findViewById(checkBoxId)).setChecked(false);
                }
            }
        }
        if (group2.contains(id)) {
            for (int checkBoxId : group2) {
                if (checkBoxId != id) {
                    ((CheckBox) findViewById(checkBoxId)).setChecked(false);
                }
            }
        }
    }

    update();
}
private void update() {
    final int[] ids = {R.id.clickable,
            R.id.dottedLine,
            R.id.colors,
            R.id.textures,
            R.id.gradient,
            R.id.bloomGradientA,
            R.id.bloomBlur,
            R.id.geodesic,
    };
    final String[] optins = {MapPolyline.CLICKABLE,
            MapPolyline.DOTTED_LINE,
            MapPolyline.COLORS,
            MapPolyline.TEXTURES,
            MapPolyline.GRADIENT,
            MapPolyline.BLOOM_GRADIENT_A,
            MapPolyline.BLOOM_BLUR,
            MapPolyline.GEODESIC,
    };
    List<String> flags = new ArrayList<>();
    for (int i = 0; i < ids.length; ++i) {
        CheckBox checkBox = findViewById(ids[i]);
        if (checkBox.isChecked())
            flags.add(optins[i]);
    }
    mapPolyline.setFlags(flags);
}

运行效果图

1234
567在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2130084.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

numpy中的比较运算

目录 比较运算符 比较运算符 有两种情况会普遍使用比较运算符&#xff0c;一个是从数组中查询满足条件的元素&#xff0c;另一个是根据判断的结果执行不同的操作。 示例入下&#xff1a; import numpy as np arr7 np.array([[1,2,10],[10,8,3],[7,6,5]]) arr8 np.array([[2,…

整流器制造5G智能工厂物联数字孪生平台,推进制造业数字化转型

整流器制造行业作为制造业的重要组成部分&#xff0c;也在积极探索数字化转型的新路径。整流器&#xff0c;作为电力电子领域的关键元件&#xff0c;广泛应用于通信、工业控制、新能源等多个领域&#xff0c;其制造过程的智能化升级不仅关乎产品性能的提升&#xff0c;更是推动…

opencv使用videocapture打开视频时,依赖opencv_ffmpeg***.dll,默认必须放到执行目录,自定义目录需要重新编译opencv库

1. 找到modules下opencv_highgui模块的cap_ffmpeg.cpp 2. 找到加载opencv_ffmpeg的接口, 修改接口内opencv_ffmpeg的路径即可.

YOLOv10优改系列一:YOLOv10融合C2f_Ghost网络,让YoloV10实现性能的均衡

&#x1f4a5; &#x1f4a5;&#x1f4a5; &#x1f4a5;&#x1f4a5; &#x1f4a5;&#x1f4a5; &#x1f4a5;&#x1f4a5;神经网络专栏改进完整目录&#xff1a;点击 &#x1f497; 只需订阅一个专栏即可享用所有网络改进内容&#xff0c;每周定时更新 文章内容&#x…

基于JavaWeb开发的javaSpringboot+mybatis+layui的装修验收管理系统设计和实现

基于JavaWeb开发的javaSpringbootmybatislayui的装修验收管理系统设计和实现 &#x1f345; 作者主页 网顺技术团队 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f4dd; &#x1f345; 文末获取源码联系方式 &#x1f4dd; &#x1f345; 查看下方微信号获取联系…

【第31章】Spring Cloud之Sentinel控制台推送规则到Nacos

文章目录 前言一、下载源码1. 下载源码 二、规则配置1. Nacos适配1.1 使用数据源1.2 复制官方案例1.3 动态规则配置中心 2. 前端路由配置3. 提示4. 编译和启动 三、测试1. 修改前2. 修改后 总结 前言 前面我们已经完成了通过nacos存储提供者流控配置文件&#xff0c;下面我们来…

腾讯云升级多个云存储解决方案 以智能化存储助力企业增长

9月6日&#xff0c;在腾讯数字生态大会腾讯云储存专场上&#xff0c;腾讯云升级多个存储解决方案&#xff1a;Data Platform 数据平台解决方案重磅发布&#xff0c;数据加速器 GooseFS、数据处理平台数据万象、日志服务 CLS、高性能并行文件存储 CFS Turbo 等多产品全新升级&am…

Nuxt Kit 的使用指南:模块创建与管理

title: Nuxt Kit 的使用指南:模块创建与管理 date: 2024/9/11 updated: 2024/9/11 author: cmdragon excerpt: 摘要:本文是关于Nuxt Kit的使用指南,重点介绍了如何使用defineNuxtModule创建自定义模块及installModule函数以编程方式安装模块,以增强Nuxt 3应用的功能性、…

JD18年秋招笔试疯狂数列python解答

问题如下&#xff1a; 链接&#xff1a;疯狂序列_京东笔试题_牛客网 [编程题]疯狂序列 热度指数&#xff1a;149 时间限制&#xff1a;C/C 1秒&#xff0c;其他语言2秒 空间限制&#xff1a;C/C 32M&#xff0c;其他语言64M 东东从京京那里了解到有一个无限长的数字序列: 1…

空间物联网中的大规模接入:挑战、机遇和未来方向

这篇论文的标题是《Massive Access in Space-based Internet of Things: Challenges, Opportunities, and Future Directions》&#xff0c;作者包括Jian Jiao, Shaohua Wu, Rongxing Lu, 和 Qinyu Zhang。文章发表在2021年10月的IEEE Wireless Communications上。论文主要探讨…

计算机毕业设计 半成品配菜平台的设计与实现 Java实战项目 附源码+文档+视频讲解

博主介绍&#xff1a;✌从事软件开发10年之余&#xff0c;专注于Java技术领域、Python人工智能及数据挖掘、小程序项目开发和Android项目开发等。CSDN、掘金、华为云、InfoQ、阿里云等平台优质作者✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精…

数据结构算法——排序算法

1.排序 1.选择排序 不稳定&#xff0c;一般不用&#xff0c;基本排序 思路&#xff1a;过滤数组&#xff0c;找到最小数&#xff0c;放在前面。 不稳&#xff1a;导致原本在前的数据移动到后面。 int arr[];for(i0;i<arr.length-1;i){int smallesti; for(ji1;j<leng…

计算机毕业设计 网上体育商城系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试

&#x1f34a;作者&#xff1a;计算机编程-吉哥 &#x1f34a;简介&#xff1a;专业从事JavaWeb程序开发&#xff0c;微信小程序开发&#xff0c;定制化项目、 源码、代码讲解、文档撰写、ppt制作。做自己喜欢的事&#xff0c;生活就是快乐的。 &#x1f34a;心愿&#xff1a;点…

加密

一、加密 加密运算需要两个输入&#xff1a;密钥和明文 解密运算也需要两个输入&#xff1a;密钥和密文 密文通常看起来都是晦涩难懂、毫无逻辑的&#xff0c;所以我们一般会通过传输或者存储密文来保护私密数据&#xff0c;当然&#xff0c;这建立在一个基础上&#xff0c;…

局域网windows下使用Git

windows下如何使用局域网进行git部署 准备工作第一步 &#xff0c;ip设置设置远程电脑的ip设置&#xff0c;如果不会设置请点击[这里](https://blog.csdn.net/Black_Friend/article/details/142170705?spm1001.2014.3001.5501)设置本地电脑的ip&#xff1a;验证 第二步&#x…

腾讯云使用

注&#xff1a;本文的所有演示的代码都基于尚硅谷的尚乐代驾项目 对象存储COS 一种云存储器 官方文档&#xff1a; 对象存储 快速入门-SDK 文档-文档中心-腾讯云 (tencent.com) 一 上传文件 1 初始化客户端 官方示例&#xff1a; // 1 传入获取到的临时密钥 (tmpSecret…

一文教你弄懂网络协议栈以及报文格式

文章目录 OSI七层网络协议栈示意图1. 应用层&#xff08;Application Layer&#xff09;2. 表示层&#xff08;Presentation Layer&#xff09;3. 会话层&#xff08;Session Layer&#xff09;4. 传输层&#xff08;Transport Layer&#xff09;5. 网络层&#xff08;Network …

Qt QSerialPort数据发送和接收DataComm

文章目录 Qt QSerialPort数据发送和接收DataComm2.添加 Qt Serial Port 模块3.实例源码 Qt QSerialPort数据发送和接收DataComm Qt 框架的Qt Serial Port 模块提供了访问串口的基本功能&#xff0c;包括串口通信参数配置和数据读写&#xff0c;使用 Qt Serial Port 模块就可以…

【超详细】Plaxis软件简介、 Plaxis Python API环境搭建、自动化建模、Python全自动实现、典型岩土工程案例实践应用

查看原文>>>【案例教程】PLAXIS软件丨自动化建模、典型岩土工程案例解析、模型应用、数据分析、图表制作 目录 第一部分&#xff1a;Plaxis软件简介及 Plaxis Python API环境搭建 第二部分&#xff1a;Plaxis自动化建模-基础案例 第三部分&#xff1a;进阶案例-Pyt…

C# HttpClient 实现HTTP Client 请求

为什么&#xff1f; C# httpclient get 请求和直接浏览器请求结果不一样 为了测试一下HTTP接口的&#xff0c;用C# HttpClient实现了HTTP客户端&#xff0c;用于从服务端获取数据。 但是遇到了问题&#xff1a;C# httpclient get 请求和直接浏览器请求结果不一样 初始代码如…