迷你马拉松线路规划,前端,lbs

2023-10-24 03:21

本文主要是介绍迷你马拉松线路规划,前端,lbs,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

我设计了迷你拉松路线规划工具。在微信小程序里,使用qqmap-wx-jssdk.js接口,通过在地图上加入途径的坐标,把每个节点串成一个完整的线路。

同时用POI在点击的时候显示每个节点真实的地名,并计算总路程。

首先要定义初始变量和onLoad函数

var app = getApp();
const QQMapWX = require('../../../../we7/js/qqmap-wx-jssdk.js');
var qqmapsdk;Page({data: {banner: [],url: [],navTile: "创建线路",curIndex: 0,nav: ["组队中", "已组队"],curList: [],oldList: [],page: 1,oldpage: 1,group: [1],usrgroup: [1, 2, 3],lat: 28.65857270312306,lng: 121.41610499450682,markers: [],teamnum:[3,4,5,6,7,8,9,10],objectteamnum: [{id: 3,name: '3人'},{id: 4,name: '4人'},{id: 5,name: '5人'},{id: 6,name: '6人'},{id: 7,name: '7人'},{id: 8,name: '8人'},{id: 9,name: '9人'},{id: 10,name: '10人'},],index: 0,time: '18:00',longitude:0,latitude:0,multiIndex: [0, 0, 0],polyline:[],pointer: [{num: 0,key: 0,color: 'none'}],node:[],beforenode:0,pl:[],distance:[],address:'',finish_line:''},onLoad: function(t) {var a = this;wx.setNavigationBarTitle({title: a.data.navTile});},

为了突出重点我省略了表单提交部分代码,直奔主题

//  qqmap api 加入坐标
putdot(e) {var that = thisthat.mapCtx = wx.createMapContext("map4select");that.mapCtx.getCenterLocation({type: 'gcj02',success: function(e) {//放入坐标点位var markers = {iconPath: "/style/images/mapine.png",id: that.data.markers.length,latitude: e.latitude,longitude: e.longitude,width: 35,height: 35}//创建路径qqmapsdk = new QQMapWX({key: '我的api密钥'});// 只能在市民广场范围内5公里的跑步qqmapsdk.calculateDistance({from: '28.65857270312306,121.41610499450682' || '',to: e.latitude + ',' + e.longitude, //终点坐标success: function (res) {//成功后的回调var res = res.result;var dis = [];for (var i = 0; i < res.elements.length; i++) {dis.push(res.elements[i].distance); //将返回数据存入dis数组,}var long = that.formatDistance2(dis[0])console.log(long)if(long>3){wx.showToast({title: '圆圈内选择线路',icon: '',image: '',duration: 1000,mask: true,success: function(res) {that.setData({circles: [{latitude: 28.65857270312306,longitude: 121.41610499450682,color: '#78daf5',fillColor: '#ffffff00',radius: 2600, //定位点半径strokeWidth: 1}],})},fail: function(res) {},complete: function(res) {},})return}else{}var pl = that.data.plvar latitude=that.data.latitudevar longitude=that.data.longitudeif(latitude){that.aquadrs2(e)qqmapsdk.direction({mode: 'walking',//可选值:'driving'(驾车)、'walking'(步行)、'bicycling'(骑行),不填默认:'driving',可不填//from参数不填默认当前地址from: latitude+','+longitude,to: e.latitude + ',' + e.longitude, //终点坐标success: function (fe) {console.log(12345)console.log(fe);var distance=that.data.distancedistance.push(fe.result.routes[0].distance)//   合计距离var distancesum=that.sum(distance)/1000var ret = fe;var coors = ret.result.routes[0].polyline, pl = that.data.pl,node=that.data.node;//坐标解压(返回的点串坐标,通过前向差分进行压缩)var kr = 1000000;for (var i = 2; i < coors.length; i++) {coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;}//将解压后的坐标放入点串数组pl中for (var i = 0; i < coors.length; i += 2) {pl.push({ latitude: coors[i], longitude: coors[i + 1] })}var beforenode=that.data.beforenodevar afternodeif(beforenode&&node!=''){afternode=pl.lengthnode.push(pl.length-beforenode)}else{afternode=pl.lengthnode.push(pl.length)}console.log(pl)//设置polyline属性,将路线显示出来,将解压坐标第一个数据作为起点that.setData({latitude:pl[pl.length-1].latitude,longitude:pl[pl.length-1].longitude,polyline: [{points: pl,color: '#FF0000DD',arrowLine:true,width: 5}],node:node,pl:pl,beforenode:afternode,distance:distance,distancesum:distancesum})},fail: function (error) {console.error(error);},});}else{that.aquadrs(e)that.setData({latitude:e.latitude,longitude:e.longitude})}console.log(that.data.markers)that.data.markers.push(markers)that.setData({markers: that.data.markers,})},});}})//console.log(that.data.pointer)},

从上往下说明,putdot(放置坐标点),makers(坐标点对象),calculateDistance(线路只能在市民广场5公里范围内进行),dis(距离),formatDistance2(计算公里数),long>3(超过范围,绘制圆圈提示),latitude(获取初始经纬度,如果非空开始规划路线),aquadrs2(获取终点地址),walking(选择的是步行计算方式)  ,e.latitude和e.longitud(获得的坐标),distance(初始化距离,数组每个元素都是一段距离),distancesum(计算总长度),pl(已有的路径),node(每次路径的节点数存入数组),coors(最新的路径),pl.push(新路径的传入数组),beforenode(之前的节点长度),afternode(之后的节点长度,如果beforenode为空就进行),pl.length-beforenode(当前路径的节点数量),setData(把最新的经纬度保存一下,设置路径,node每个节点长度的数组,路径的数组,用新的afternode替换beforenode,distance每段距离的数组,distancesum距离的统计数值)

else里,latitude(确定起点坐标),aquadrs(获取起点的地址名称),markers(一开始回调得到的定位),that.data.markers(默认坐标)

补充撤销操作函数

  sum(arr){var a=0for(var s in arr){a+=arr[s]}console.log(a)return a},
//   获取起点地址aquadrs(e) {var _this = this;qqmapsdk.reverseGeocoder({location: e.latitude + ',' + e.longitude || '', //获取表单传入的位置坐标,不填默认当前位置,示例为string格式//get_poi: 1, //是否返回周边POI列表:1.返回;0不返回(默认),非必须参数success: function(res) {//成功后的回调console.log(res);var res = res.result;var mks = [];mks.push({ // 获取返回结果,放到mks数组中title: res.address,id: 0,latitude: res.location.lat,longitude: res.location.lng,iconPath: '/style/images/5.png',//图标路径width: 20,height: 20,//   callout: {//     content: res.address,//     color: '#000',//     display: 'ALWAYS'//   }});_this.setData({ //设置markers属性和地图位置poi,将结果在地图展示markers: mks,address:res.address,poi: {latitude: res.location.lat,longitude: res.location.lng}});},fail: function(error) {console.error(error);},complete: function(res) {console.log(res);}})},//   获取地址aquadrs2(e) {var _this = this;qqmapsdk.reverseGeocoder({location: e.latitude + ',' + e.longitude || '', //获取表单传入的位置坐标,不填默认当前位置,示例为string格式//get_poi: 1, //是否返回周边POI列表:1.返回;0不返回(默认),非必须参数success: function(res) {//成功后的回调console.log(res);var res = res.result;var mks = [];mks.push({title: res.address,id: 0,latitude: res.location.lat,longitude: res.location.lng,iconPath: '/style/images/hklogo.png',width: 20,height: 20,});_this.setData({ //设置markers属性和地图位置poi,将结果在地图展示//   markers: mks,finish_line:res.address,poi: {latitude: res.location.lat,longitude: res.location.lng}});},fail: function(error) {console.error(error);},complete: function(res) {console.log(res);}})},undo2(){var that=this//start deletenumber splice//start end slicevar node = that.data.nodethat.data.markers.splice(-1)that.data.pl.splice(-node[node.length-1])that.data.node.splice(-1)that.data.distance.splice(-1)//node和pl同时清空,marker点比他们多一个,所以最后起始点应该再撤销一次console.log('undo '+node[node.length-1])that.setData({node: that.data.node,polyline: [{points: that.data.pl,color: '#FF0000DD',arrowLine:true,width: 5}],markers: that.data.markers,distance:that.data.distance})console.log(that.data.markers.length-1)//marker点比路径数组多一个值if(that.data.markers.length>0){that.setData({latitude:that.data.markers[that.data.markers.length-1]['latitude'],longitude:that.data.markers[that.data.markers.length-1]['longitude']    })}else{that.setData({latitude:0,longitude:0})}},formatDistance(num) {if (num < 1000) {return num.toFixed(0) + 'm';} else if (num > 1000) {return (num / 1000).toFixed(1) + 'km';}},formatDistance2(num) {return (num / 1000).toFixed(1)}
});

通过修改markers,pl,node规划路线

这篇关于迷你马拉松线路规划,前端,lbs的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/272382

相关文章

HTML5的input标签的`type`属性值详解和代码示例

《HTML5的input标签的`type`属性值详解和代码示例》HTML5的`input`标签提供了多种`type`属性值,用于创建不同类型的输入控件,满足用户输入的多样化需求,从文本输入、密码输入、... 目录一、引言二、文本类输入类型2.1 text2.2 password2.3 textarea(严格

SpringBoot返回文件让前端下载的几种方式

《SpringBoot返回文件让前端下载的几种方式》文章介绍了开发中文件下载的两种常见解决方案,并详细描述了通过后端进行下载的原理和步骤,包括一次性读取到内存和分块写入响应输出流两种方法,此外,还提供... 目录01 背景02 一次性读取到内存,通过响应输出流输出到前端02 将文件流通过循环写入到响应输出流

SpringBoot+Vue3整合SSE实现实时消息推送功能

《SpringBoot+Vue3整合SSE实现实时消息推送功能》在日常开发中,我们经常需要实现实时消息推送的功能,这篇文章将基于SpringBoot和Vue3来简单实现一个入门级的例子,下面小编就和大... 目录前言先大概介绍下SSE后端实现(SpringBoot)前端实现(vue3)1. 数据类型定义2.

前端Visual Studio Code安装配置教程之下载、汉化、常用组件及基本操作

《前端VisualStudioCode安装配置教程之下载、汉化、常用组件及基本操作》VisualStudioCode是微软推出的一个强大的代码编辑器,功能强大,操作简单便捷,还有着良好的用户界面,... 目录一、Visual Studio Code下载二、汉化三、常用组件1、Auto Rename Tag2

vite搭建vue3项目的搭建步骤

《vite搭建vue3项目的搭建步骤》本文主要介绍了vite搭建vue3项目的搭建步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1.确保Nodejs环境2.使用vite-cli工具3.进入项目安装依赖1.确保Nodejs环境

Nginx搭建前端本地预览环境的完整步骤教学

《Nginx搭建前端本地预览环境的完整步骤教学》这篇文章主要为大家详细介绍了Nginx搭建前端本地预览环境的完整步骤教学,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录项目目录结构核心配置文件:nginx.conf脚本化操作:nginx.shnpm 脚本集成总结:对前端的意义很多

前端缓存策略的自解方案全解析

《前端缓存策略的自解方案全解析》缓存从来都是前端的一个痛点,很多前端搞不清楚缓存到底是何物,:本文主要介绍前端缓存的自解方案,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、为什么“清缓存”成了技术圈的梗二、先给缓存“把个脉”:浏览器到底缓存了谁?三、设计思路:把“发版”做成“自愈”四、代码

通过React实现页面的无限滚动效果

《通过React实现页面的无限滚动效果》今天我们来聊聊无限滚动这个现代Web开发中不可或缺的技术,无论你是刷微博、逛知乎还是看脚本,无限滚动都已经渗透到我们日常的浏览体验中,那么,如何优雅地实现它呢?... 目录1. 早期的解决方案2. 交叉观察者:IntersectionObserver2.1 Inter

Vue3视频播放组件 vue3-video-play使用方式

《Vue3视频播放组件vue3-video-play使用方式》vue3-video-play是Vue3的视频播放组件,基于原生video标签开发,支持MP4和HLS流,提供全局/局部引入方式,可监听... 目录一、安装二、全局引入三、局部引入四、基本使用五、事件监听六、播放 HLS 流七、更多功能总结在 v

JS纯前端实现浏览器语音播报、朗读功能的完整代码

《JS纯前端实现浏览器语音播报、朗读功能的完整代码》在现代互联网的发展中,语音技术正逐渐成为改变用户体验的重要一环,下面:本文主要介绍JS纯前端实现浏览器语音播报、朗读功能的相关资料,文中通过代码... 目录一、朗读单条文本:① 语音自选参数,按钮控制语音:② 效果图:二、朗读多条文本:① 语音有默认值:②