当前位置: 首页 > news >正文

一种基于高德Web API实现沿路画面的实现

概述

本文在mapboxGL框架下,分享一种基于高德Web API实现沿路画面的实现。

实现效果

image.png

实现

1. 实现思路

  1. 通过点击获取路径的起点和终点;
  2. 将多次规划路径的结果连成一条线;
  3. 当鼠标点击回到第一个点的时候结束绘制;
  4. 绘制结束后将路径闭合,形成多边形,并对形成的多边形进行处理;

实现代码

<template><div class="map"><lzugismap-map:onLoad="mapLoaded":maxPitch="0":minPitch="0":pitch="0":style="style":center="[113.91505797606129, 22.536719264512044]":zoom="zoom":interactive="true"></lzugismap-map><div class="map-tools"><button :disabled="isDraw" @click="startDraw">绘制</button></div></div>
</template><script>
import { LzugismapMap } from "@lzugismap/vue-lzugismap";
import * as turf from "@turf/turf";class Geojson {constructor(features = []) {this.features = features;this.type = "FeatureCollection";}
}let points = [],routes = [];export default {components: {LzugismapMap,},data() {return {map: null,zoom: 16,isDraw: false,};},methods: {mapLoaded(map) {this.map = map;map.addSource("route-source", {type: "geojson",data: new Geojson(),});map.addSource("fill-source", {type: "geojson",data: new Geojson(),});map.addSource("point-source", {type: "geojson",data: new Geojson(),});map.addLayer({id: "route-source-fill",source: "fill-source",type: "fill",paint: {"fill-color": "red","fill-opacity": 0.15,},});map.addLayer({id: "route-source-line",source: "route-source",type: "line",paint: {"line-color": "red","line-width": 3.5,},});map.addLayer({id: "route-point",source: "point-source",type: "circle",paint: {"circle-color": "blue","circle-opacity": 0.8,"circle-radius": 5,},});// this.showRoute();},startDraw() {this.isDraw = true;points = [];routes = [];const that = this;that.map.getSource("point-source").setData(new Geojson());that.map.getSource("route-source").setData(new Geojson());that.map.getSource("fill-source").setData(new Geojson());that.map.getCanvas().style.cursor = "crosshair";const mapClickEvt = (e) => {const point = e.lngLat.toArray();points.push(point);that.map.getSource("point-source").setData(new Geojson(points.map((p) => turf.point(p))));if (points.length > 1) {const p0 = points[0];const start = points[points.length - 2];let end = points[points.length - 1];const dis = turf.distance(turf.point(p0), turf.point(end)) * 1000;const isEnd = dis < 10;if (isEnd) {end = p0;}// driving walkingconst url = `https://restapi.amap.com/v3/direction/walking?origin=${start.join(",")}&destination=${end.join(",")}&output=JSON&key={你申请的key}`;fetch(url).then((res) => res.json()).then((res) => {const paths = res?.route?.paths || [];paths.forEach(({ steps }) => {steps.forEach(({ polyline }) => {const _points = polyline.split(";").map((item) => item.split(",").map(Number));routes = [...routes, ..._points];});});if (isEnd) {const polygon = turf.lineToPolygon(turf.lineString(routes));const geojson = turf.polygonSmooth(polygon, { iterations: 10 });that.map.getSource("route-source").setData(geojson);that.map.getSource("fill-source").setData(geojson);that.isDraw = false;this.map.off("click", mapClickEvt);that.map.getCanvas().style.cursor = "";} else {const line = turf.lineString(routes);that.map.getSource("route-source").setData(line);}});}};this.map.on("click", mapClickEvt);},},
};
</script><style scoped lang="scss">
.map {width: 100%;height: 100%;
}
.map-tools {position: absolute;right: 2rem;top: 2rem;z-index: 99;
}
</style>

相关文章:

  • 54.WEB渗透测试-信息收集- 端口、目录扫描、源码泄露(2)
  • 从 URL 中获取数据并创建列表解析
  • ch5链路层和局域网
  • java基础filter servlet interceptor controller之间的关系
  • Python 怎么注释大段
  • 操作失败——后端
  • 华为、华三交换机、路由器启用基于端口的环回检测功能配置
  • Python怎么翻译:探索Python在翻译领域的无限可能
  • gitlab将本地文件项目上传至gitlab服务
  • 微电子加速迈向后摩尔时代!复旦大学梅永丰课题组集成 DNN 与纳米薄膜技术,精准分析入射光角度
  • 计算机基础(5)——进制与进制转换
  • 明天15点!如何打好重保预防针:迎战HVV经验分享
  • python11 序列的相关操作
  • Unity 性能优化之LOD技术(十)
  • vue 将echart 下载为base64图片
  • “大数据应用场景”之隔壁老王(连载四)
  • 【108天】Java——《Head First Java》笔记(第1-4章)
  • 【node学习】协程
  • 【跃迁之路】【444天】程序员高效学习方法论探索系列(实验阶段201-2018.04.25)...
  • CSS实用技巧干货
  • JavaScript 是如何工作的:WebRTC 和对等网络的机制!
  • k个最大的数及变种小结
  • Laravel5.4 Queues队列学习
  • leetcode388. Longest Absolute File Path
  • PaddlePaddle-GitHub的正确打开姿势
  • SQLServer之索引简介
  • Vue.js源码(2):初探List Rendering
  • 得到一个数组中任意X个元素的所有组合 即C(n,m)
  • 汉诺塔算法
  • 硬币翻转问题,区间操作
  • 400多位云计算专家和开发者,加入了同一个组织 ...
  • AI算硅基生命吗,为什么?
  • 容器镜像
  • ​LeetCode解法汇总2304. 网格中的最小路径代价
  • #QT项目实战(天气预报)
  • #多叉树深度遍历_结合深度学习的视频编码方法--帧内预测
  • #微信小程序:微信小程序常见的配置传值
  • #中国IT界的第一本漂流日记 传递IT正能量# 【分享得“IT漂友”勋章】
  • $emit传递多个参数_PPC和MIPS指令集下二进制代码中函数参数个数的识别方法
  • (1)SpringCloud 整合Python
  • (2)MFC+openGL单文档框架glFrame
  • (二)c52学习之旅-简单了解单片机
  • (二)构建dubbo分布式平台-平台功能导图
  • (附源码)spring boot智能服药提醒app 毕业设计 102151
  • (附源码)springboot车辆管理系统 毕业设计 031034
  • (六)软件测试分工
  • (五)网络优化与超参数选择--九五小庞
  • (续)使用Django搭建一个完整的项目(Centos7+Nginx)
  • (转)原始图像数据和PDF中的图像数据
  • .gitignore文件---让git自动忽略指定文件
  • .NET Core 将实体类转换为 SQL(ORM 映射)
  • .NET Core6.0 MVC+layui+SqlSugar 简单增删改查
  • .NET CORE使用Redis分布式锁续命(续期)问题
  • .NET IoC 容器(三)Autofac
  • .NET 编写一个可以异步等待循环中任何一个部分的 Awaiter