需求:项目需要做一个救援动画效果
技术栈:react、openlayer、高德导航接口、天地图导航接口

零、选择导航接口

老大最开始建议使用天地图导航接口,测试后接口响应时间大约为30秒以上,达不到商用条件。因此选择高德的接口。

一、调用高德接口

1、1申请key

在高德官网注册用户,创建应用,申请key
地址:https://lbs.amap.com/api/webservice/summary
点击【获取key】,按提示进入控制台,创建应用,创建key


创建应用,在应用下边添加key。我需要调用webapi中的【路径规划API】所以选择的是Web服务

1.2测试接口

https://restapi.amap.com/v3/direction/driving?origin=[出发经度],[出发纬度]&destination=[目标经度],[目标纬度]&extensions=all&output=json&key=[key]

二、实现导航

2.1、拾取爆点坐标

爆点的样式:对点要素的样式设置为pipeBroken图片

const pipeBrokenStyle = new Style({image: new Icon({anchor: [0.5, 20],anchorXUnits: 'fraction',anchorYUnits: 'pixels',src: pipeBroken,}),
});

处理点击地图事件。获取鼠标点击的坐标,构建点要素,绘制到地图上。同时写入state。构建查询资源的参数

 beginGetFeatureInfo = (event) => {const {map} = this.props;const {featuresVectorSource, clickFeature} = this.state;if (clickFeature) {if (featuresVectorSource.getFeatures().length > 0) {try {clearMap(map);} catch (e) {console.log(e);}}}const pipeBrokenFeature = new Feature({geometry: new Point(event.coordinate),});pipeBrokenFeature.setStyle(pipeBrokenStyle);const pointInfo = `point(${event.coordinate[0]} ${event.coordinate[1]})`;this.setState({clickFeature: pipeBrokenFeature, pointInfo,}, () => {featuresVectorSource.addFeature(pipeBrokenFeature);this.getResources();});};

2.2、获取资源坐标

调用业务系统接口查询资源的坐标,此部分按自己的业务系统处理即可,不做展示。

2.3、坐标转换

此时具备了资源坐标和爆点的坐标,构造高德导航参数即可。但是项目中使用的是3857坐标系,需要进行转换。
3857转4326使用的是proj4组件
4326转gcj02使用的是coordtransform组件(
https://www.npmjs.com/package/coordtransform
https://github.com/wandergis/coordtransform)
从3857转到4326再转换到gcj02(高德使用的是gcj02)

   trans3857To4326 = (coordinate) => {return proj4('EPSG:3857', 'EPSG:4326', coordinate);}// 进行坐标转换 3857转wgs84const coordClick = this.trans3857To4326(clickFeature.getGeometry().getCoordinates());  // 84再转gcj02const clickCoord = coordtransform.wgs84togcj02(coordClick[0], coordClick[1]);

2.4、调用接口

调用接口。建议使用axios而不是Fetch。最开始使用fetch总是调用失败,卡了一天。同事说试试axios,一下就通了

  /*** 导航*/navigationRequest = (origin, destination) => {// 高德const url = `https://restapi.amap.com/v3/direction/driving?origin=${origin[0]},${origin[1]}&destination=${destination[0]},${destination[1]}&extensions=all&output=json&key=[自己的key]`return axios.get(url).then(function (response) {return response;}).catch(function (error) {console.log(error)return error;});}

2.5、解析数据

返回数据的结构参见接口说明。我只需要【行驶距离】、【预计行驶时间】和路径坐标。

返回的数据需要坐标转换。从gcj02转4326,再转3857.转换函数如下

trans4326To3857 = (coordinate) => {return proj4('EPSG:4326', 'EPSG:3857', coordinate);}
// 生成轨迹要素drawLineFeature = (steps) => {// 取出所有polyline拼接为键值对let polylines = "";for (let i = 0; i < steps.length; i++) {polylines += steps[i].polyline;polylines += ";";}const points = polylines.split(";");const pointList = [];const pointList3857 = [];for (let i = 0; i < points.length - 1; i++) {pointList.push(points[i].split(","));pointList3857.push(this.trans4326To3857(coordtransform.gcj02towgs84(points[i].split(",")[0], points[i].split(",")[1])));}return new Feature({geometry: new LineString(pointList3857)});}

2.6、生成动效

动效需要路径要素:reliefSuppliesDriveLine、移动的点:reliefSuppliesCarFeatureTemp。在图层reliefSuppliesCarVL中对要素reliefSuppliesCarFeatureTemp添加动画路径anim,获得控制器reliefSuppliesPlayController。
控制器可以用于终止动画

/*** 开始物资动画*/startReliefSuppliesAnimation = () => {const {selectedReliefSupplies, reliefSuppliesDriveLine, reliefSuppliesCarVS, reliefSuppliesCarVL, reliefSuppliesCarFeature,} = this.state;const anim = new Path({path: reliefSuppliesDriveLine,rotate: true,easing: linear,duration: 0.25 * 60 * 1000,revers: false,repeat: 99999999,});reliefSuppliesCarVS.addFeature(reliefSuppliesDriveLine);if (!reliefSuppliesCarFeature) {const reliefSuppliesCarFeatureTemp = new Feature({zIndex: 999999999999,geometry: new Point(selectedReliefSupplies.getGeometry().getCoordinates()),});reliefSuppliesCarVS.addFeature(reliefSuppliesCarFeatureTemp);// 动画const reliefSuppliesPlayController = reliefSuppliesCarVL.animateFeature(reliefSuppliesCarFeatureTemp, anim);this.setState({reliefSuppliesCarFeature: reliefSuppliesCarFeatureTemp, reliefSuppliesPlayController});}}

有效代码:reliefSuppliesPlayController.stop();

  /*** 清除救援物资动画*/clearReliefSuppliesAnimation = () => {const {reliefSuppliesCarVS, reliefSuppliesDriveLine, reliefSuppliesCarFeature, reliefSuppliesPlayController,} = this.state;if (reliefSuppliesPlayController) {reliefSuppliesPlayController.stop();}if (reliefSuppliesCarFeature) {reliefSuppliesCarVS.removeFeature(reliefSuppliesCarFeature);this.setState({reliefSuppliesCarFeature: null});}if (reliefSuppliesDriveLine) {reliefSuppliesCarVS.removeFeature(reliefSuppliesDriveLine);this.setState({reliefSuppliesDriveLine: null});}reliefSuppliesCarVS.clear();}

OpenLayers使用高德导航接口实现动画animate相关推荐

  1. Android系统联系人全特效实现(上),分组导航和挤压动画

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/9033553 记得在我刚接触Android的时候对系统联系人中的特效很感兴趣,它会根 ...

  2. 闲着无聊,撸个微信导航栏的动画吧!

    /   开始   / 微信自发布以来,底部导航栏的动画一直让开发者津津乐道,而且伴随着版本更新,底部导航栏的动画也一直在改进.我最近在闲暇之余,看了下微信的底部导航栏动画,于是思考了下这个动画的原理, ...

  3. Android 泽宇高德导航驾车规划路线

    高德导航 如若转载请标明转载处:https://blog.csdn.net/zeyu_rensheng/article/details/81122719 开发者文档:https://lbs.amap. ...

  4. Android高德导航自定义UI

    今天接近一整天都在搞高德导航自定义UI,晚上下班前,终于搞好了,大致的界面就这这样咯, 高德导航官方有一部分教程,http://lbs.amap.com/api/android-navi-sdk/gu ...

  5. iOS 高德导航按返回后报错 解决

    最近项目要添加导航功能,用了高德导航SDK,很郁闷每次从地图界面返回前一页面都报错,弄了很久,最终从高德开发者论坛找到一解决方法,可以试一下. 在导航的ViewController的viewWillD ...

  6. jQuery动画---自定义动画animate()

    版权声明:本文为博主原创文章,转载请注明出处. https://blog.csdn.net/twilight_karl/article/details/73511662 同步动画 animate(参数 ...

  7. 自定义动画 animate || 案例:王者荣耀手风琴效果分析

    自定义动画 animate <!DOCTYPE html> <html lang="en"><head><meta charset=&qu ...

  8. play版 高德地图google_iOS 12 发布,苹果 CarPlay 终于开始支持谷歌、高德导航

    然而车机手机互联已经不是最佳车载互联解决方案. 一年一度的苹果 WWDC 开发者大会昨天又来了,没有新的硬件产品发布,一切更新都是和软件相关的. 但是有一个关于汽车的重大更新可能被很多人忽略了:在最新 ...

  9. Android 7.1 高德导航和蓝牙音乐卡顿问题 蓝牙电话和高德语音播报混音问题

    此文章主要解决三个问题 1.高德导航的时候打电话会出现混音问题. 2.蓝牙音乐在播放的时候导航界面语音播报蓝牙音乐会暂停,播报结束会恢复播放不能同时输出问题. 3.蓝牙音乐在播放的时候和导航界面的语音 ...

  10. linux版高德导航软件下载,【高德导航离线地图包下载】高德地图离线包 -ZOL软件下载...

    高德导航离线地图包正宗好地图正宗好应用,欢迎使用由pc操作系统领导企业微软为您冠名的windows8中国好地图--高德导航离线地图包. Win8系统第一款支持3d的地图应用,7500万用户的选择. 高 ...

最新文章

  1. java: http请求和响应
  2. 一件代发系统php网站源码_靠谱礼品代发平台网站一件代发(0.5元)
  3. android 判断是否正在扫描蓝牙_判断蓝牙是否连接
  4. 使用rapid-framework自动生成struct2
  5. 制定交叉编译工具_配置交叉编译工具链-嵌入式Linux
  6. 算法五——字符串匹配(上)
  7. educoder平台_22个在线平台,2.4万门网课
  8. zip版mysql5.6_mysql 5.6 压缩包版安装方法
  9. 应用管理系列 — 怎么管理Sybase数据库
  10. Java期末设计(十三周)
  11. HDU4565(SummerTrainingDay05-C 矩阵快速幂)
  12. 传说中的Markov不过如此”
  13. 计算机应用基础任务式教程 素材,计算机应用基础任务化教程教学大纲
  14. Java菜鸡的学习日常——2021华为软挑(练手)
  15. Android开发网上的一些重要知识点[经验分享]
  16. 学日语就是一种煎熬!
  17. ffmpeg批量m4a转mp3
  18. 京东有鸿蒙系统app,刘强东力挺华为鸿蒙,安卓版京东与鸿蒙版京东,差距不是一点点...
  19. 2022漂亮有质感的SummerAdmin后台模板+Layui内核
  20. akoj-1291-决战21点(C#)

热门文章

  1. 清华牛人总结的数据分析笔记
  2. 【Linux】Shell脚本:while read line无法读取最后一行???
  3. 三十岁仍一事无成,一个失败工程师的自白
  4. 品牌公寓+电子合同,开拓年轻人租房市场
  5. 3D全景图php代码,HTML5 Canvas实现360度全景图的示例代码
  6. python网易云音乐爬虫歌词_一篇文章带你用Python网络爬虫实现网易云音乐歌词抓取...
  7. html行内样式选择器怎么写,巧用CSS伪类选择器实现九种样式的九宫格
  8. django踩坑记录
  9. 【基于Android的连连看游戏的设计与实现】
  10. Windows基础 主机加固