uniapp使用高德地图路径规划之map组件

uniapp使用高德地图路径规划之map组件

1、项目前准备

1.1、首先你需要去申请一个属于自己的高德地图key,怎么申请暂不多说需要的去官网看
1.2、链接: 高德地图申请key直通车,点击前往
有一个uniapp项目。

2、页面创建

新建一个uniapp的空白页 使用map组件渲染地图

  	<map id='map' :longitude="118.045616" :latitude="24.366646" :markers="covers"></map>
  	
    const covers = [{
		latitude: 24.366646,
		longitude: 118.045616,
		width: 40,
		height: 40,
		callout: {
			content: '点标记',
			display:'ALWAYS'
		},
		iconPath: '../../../../static/addrs.png'
	}]

就完成了 基础操作
在这里插入图片描述

3、路径规划

3.1、我们需要借助高德地图的路径规划查询获取到全部的路径规划 链接: 高德文档,点击前往

   const getDriving = () => {
       	let params =
   	    	`origin=118.045616,24.366646&destination=118.099481,24.583817&key=你的key`
   	    uni.request({
   		   url: `https://restapi.amap.com/v3/direction/driving?${params}`,
   		   success: (res) => {
   		   	   let data = res.data.route
   			   if (data.paths && data.paths[0] && data.paths[0].steps) {
   				   washData(data.paths[0].steps)
   			   }
   		   }
   	    });
   }

3.2、我们就获取到全部路径规划的数据,但是还是要洗一下数据。 ( ps:v3 和 v5返回的数据格式不一样)
在这里插入图片描述

   let planList = ref([])
   const washData = (steps) => {
   	    let points = []
   	    steps.forEach(item => {
   		    const polen = item.polyline.split(';')
   		    polen.forEach(vv => {
   			    let splits = vv.split(',')
   			    points.push({
   				    longitude: parseFloat(splits[0]),
   				    latitude: parseFloat(splits[1])
   			    })
   		    })
   	   })

   	    planList.value = [{
   		    points: points,
   		    width: 3,
   		    arrowLine: true,
   		    arrowIconPath: true,
   		    color: '#204CF1',
   	    }]
   }
   
    // template 里面的map组件使用
 	<map id='map' :longitude="118.045616" :latitude="24.366646" :markers="covers" :polyline="planList"></map>

3.2、然后就大功告成啦!! 其他的按照官网参数变成自己想要的样子就好了
在这里插入图片描述

4、最后 上代码~
<template>
   <map id='map' :longitude="118.045616" :latitude="24.366646" :markers="covers" :polyline="planList"></map>
</template>

<script setup>
   onLoad(() => {
   	getDriving()
   })

   const covers = [{
   	latitude: 24.366646,
   	longitude: 118.045616,
   	width: 40,
   	height: 40,
   	callout: {
   		content: '点标记',
   		display: 'ALWAYS'
   	},
   	iconPath: '../../../../static/addrs.png'
   },{
   	latitude: 24.583817,
   	longitude: 118.099481,
   	width: 40,
   	height: 40,
   	callout: {
   		content: '点标记',
   		display: 'ALWAYS'
   	},
   	iconPath: '../../../../static/addrss.png'
   }]
   
   const getDriving = () => {
   	let params =
   		`origin=118.045616,24.366646&destination=118.099481,24.583817&key=你的key`
   	uni.request({
   		url: `https://restapi.amap.com/v3/direction/driving?${params}`,
   		success: (res) => {
   			let data = res.data.route
   			if (data.paths && data.paths[0] && data.paths[0].steps) {
   				washData(data.paths[0].steps)
   			}
   		}
   	});
   }

   let planList = ref([])
   const washData = (steps) => {
   	let points = []
   	steps.forEach(item => {
   		const polen = item.polyline.split(';')
   		polen.forEach(vv => {
   			let splits = vv.split(',')
   			points.push({
   				longitude: parseFloat(splits[0]),
   				latitude: parseFloat(splits[1])
   			})
   		})
   	})

   	planList.value = [{
   		points: points,
   		width: 3,
   		arrowLine: true,
   		arrowIconPath: true,
   		color: '#204CF1',
   	}]
   }
</script>

<style scoped>
   #map {
   	width: 100%;
   	height: 100%;
   }
</style>
### 实现路径规划功能 在 UniApp 中利用 `map` 组件实现路径规划主要依赖于所集成的地图服务提供商的能力。对于腾讯地图而言,在使用前需确认已在项目中的 `manifest.json` 文件里配置好相应的权限设置并获取必要的密钥[^3]。 为了完成路径规划的功能,除了基本的地图显示外还需要调用特定的 API 接口来计算两点或多点之间的最优路径。下面给出一段简单的代码片段作为示例: ```html <template> <view class="container"> <!-- 地图组件 --> <map id="myMap" :polyline="polyline" :markers="markers" style="width:100%; height:80vh;"></map> <!-- 起始位置输入框 --> <input type="text" v-model="startLocation" placeholder="请输入起点"/> <!-- 结束位置输入框 --> <input type="text" v-model="endLocation" placeholder="请输入终点"/> <!-- 查询按钮 --> <button @click="getRoute">查询路线</button> </view> </template> <script> export default { data() { return { startLocation: '', endLocation: '', polyline: [], // 存储路径坐标数组 markers: [] // 标记起始点和结束点的位置 } }, methods: { getRoute() { const that = this; uni.request({ url: 'https://apis.map.qq.com/ws/direction/v1/driving/', method: 'GET', data: { from: `${this.startLocation}`, to: `${this.endLocation}`, key: '您的腾讯地图key' // 替换成自己的key }, success(res) { let result = res.data.result.routes[0]; if (result !== undefined){ that.polyline = [{ points: result.points, color:"#FF0000", width:6 }]; // 设置起点和终点标记 that.markers=[ {latitude:result.from.location.lat, longitude:result.from.location.lng,title:'起点'}, {latitude:result.to.location.lat, longitude:result.to.location.lng,title:'终点'} ]; }else{ console.log('未找到路径'); } } }); } } } </script> ``` 此段代码实现了通过用户输入两个地点名称后点击“查询路线”,向腾讯地图服务器发送请求获得这两地间的驾车导航数据,并将得到的结果绘制到页面上的地图上。注意这里仅展示了基于驾驶模式(`driving`) 的路径查找方式;实际上还有步行(`walking`) 和公交(`transit`) 等其他出行方案可供选择[^4]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值