uniapp H5端使用高德地图完成路线规划

本文详细介绍了如何在uniapp项目中使用高德地图API实现地图显示、定位、选择地点及路线规划等功能,包括引入地图SDK、初始化地图、获取定位、选择起点终点和规划路线的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本项目是H5端,APP端地图的使用方法请参考我的另一篇博客uniapp,map地图组件打包原生APP

首先到高德地图API,申请web端key

在这里插入图片描述

参考高德H5端教程开始写代码高德地图JS API

1、准备工作,会提示你先引入下边这块代码

<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script> 

我的项目是uniapp项目,在自定义的html引入会报错,个人感觉可能是加载顺序的问题,最后修改成

import maps from '../../static/maps.js' 

关于maps.js文件是怎么来的,第一步准备工作的src链接,直接打开,将这个js文件保存到本地maps.js并引用就可以。

2、定义dom

<view id="container"></view>

3、为地图容器指定宽高

#container {
	width: 750rpx;
	height: 750rpx;
}

4、初始化地图控件,需要放在this.$nextTick函数内,否则会报错(之前是按照官方的写法window.onLoad,测试也可以,过了段时间不行了,不知道什么原因)

this.$nextTick(() => {
	let _this = this;
	_this.map = new AMap.Map('container', {
		zoom: 15, //缩放级别
		resizeEnable: true, //自动定位
	});
})

初始化完成如下图
在这里插入图片描述

uniapp data中的变量,下边的操作会向变量中赋值

data() {
	return {
		map: null,
		address0: {}, //起点信息
		address1: {}, //终点信息
		star: [], //起点
		end: [], //终点
		markers: [], //点标记
		routes: {}, //路线距离时间信息记录
	}
},

5、地图已经调出来了,现在应该获取定位,并在在地图上定位标记点

AMap.plugin('AMap.Geolocation', function() {
	uni.showLoading({
		title: '系统正在定位'
	});
	var geolocation = new AMap.Geolocation({
		enableHighAccuracy: true, //是否使用高精度定位,默认:true
		timeout: 10000, //超过10秒后停止定位,默认:5s
		buttonPosition: 'RB', //定位按钮的停靠位置
		buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
		zoomToAccuracy: true, //定位成功后是否自动调整地图视野到定位点
	
	});
	_this.map.addControl(geolocation);
	geolocation.getCurrentPosition(function(status, result) {
		if (status == 'complete') {
			//这个地方的result,可能会出现报错:获得地理定位时间。得到ipLocation成功。获取地址失败,请检查您的密钥或网络。
			//可能是密匙申请错了,重新申请密匙,生成maps.js文件。
			console.log(result)
			uni.hideLoading();
			uni.showToast({
				title: '定位成功',
			});
			let res = {
				name: result.formattedAddress,
				latitude: result.position.lat,
				longitude: result.position.lng,
			}
			
			let marker0 = new AMap.Marker({
				map: _this.map,
				position: [res.longitude, res.latitude], //位置
				icon: '/static/star.png', // 添加 Icon 图标 URL
				offset: new AMap.Pixel(-15, -42),//偏移量
			});
			_this.markers[0] = marker0; //添加 起点 标记
	
			_this.address0 = res; //起点数据
			_this.star = [res.longitude, res.latitude]; //起点经纬度
	
		} else {
			uni.hideLoading();
			uni.showToast({
				title: '定位失败,请手动选择出发地',
			});
			console.log(result)
		}
	});
});

如下图,定位标记,和数据都已获取到
在这里插入图片描述
6、下面是选择出发地和目的地事件,使用uni.chooseLocation()打开地图选择完成,

<view class="item" @click="chooseAddress(1)">
	<text class="title">请选择出发地</text>
</view>
<view class="item">
	<text class="color">{{address0.name}}</text>
</view>
<view class="item" @click="chooseAddress(2)">
	<text class="title">请选择目的地</text>
</view>
<view class="item">
	<text class="color">{{address1.name}}</text>
</view>
<view class="item">
	<text class="color" v-if="routes.distance">{{routes.distance/1000}}千米,</text>
	<text class="color" v-if="routes.policy == '速度最快'">出行模式:驾车,</text>
	<text class="color" v-if="routes.time">大约需要{{routes.time/60}}分钟。</text>
</view>
chooseAddress(type) {
	uni.chooseLocation({
		success: res => {
			if (type == 1) {
				this.markers[0].setMap(null); //删除 起点 标记
				let marker0 = new AMap.Marker({
					map: this.map,
					position: [res.longitude, res.latitude], //位置
					icon: '/static/star.png', // 添加 Icon 图标 URL
					offset: new AMap.Pixel(-15, -42),//偏移量
				});
				this.markers[0] = marker0; //覆盖 起点 标记

				this.star = [res.longitude, res.latitude]; //起点经纬度
				this.address0 = res;//起点数据
				if (this.markers.length == 2) {
					this.map.clearMap();//先清除地图覆盖物
					this.add_Driving();//在重新规划路线
				} else {
					this.map.setFitView(); //自动缩放地图
				}
			} else {
				let marker1 = new AMap.Marker({
					map: this.map,
					position: [res.longitude, res.latitude], //位置
					icon: '/static/end.png', // 添加 Icon 图标 URL
					offset: new AMap.Pixel(-15, -42),//偏移量
				});
				this.address1 = res;//终点数据
				this.end = [res.longitude, res.latitude]; //终点经纬度
				this.map.clearMap();//先清除地图覆盖物
				this.markers[1] = marker1; //添加 终点 标记
				this.add_Driving();//规划路线
			}
		}
	});
},

7、地址选择完毕后规划路线

add_Driving() {
	let _this = this;
	this.map.plugin('AMap.Driving', () => {
		var driving = new AMap.Driving({
			// 驾车路线规划策略,AMap.DrivingPolicy.LEAST_TIME是最快捷模式
			map: this.map,
			policy: AMap.DrivingPolicy.LEAST_TIME
		})
		driving.search(this.star, this.end, function(status, result) {
			console.log(result)
			_this.routes = result.routes[0]
			_this.markers[0].setMap(null);
			_this.markers[1].setMap(null);
		})
	})
},

完成后的效果图如下
在这里插入图片描述

附上项目完整代码,注意你的key (web端 js API)
这里你只需要把你的maps.js放在static目录,
star.png,end.png 起点终点图标放在static目录,
完成后可以直接使用本代码,

<template>
	<view class="login">
		<view id="container"></view>
		
		<view class="item" @click="chooseAddress(1)">
			<text class="title">请选择出发地</text>
		</view>
		<view class="item">
			<text class="color">{{address0.name}}</text>
		</view>
		<view class="item" @click="chooseAddress(2)">
			<text class="title">请选择目的地</text>
		</view>
		<view class="item">
			<text class="color">{{address1.name}}</text>
		</view>
		<view class="item">
			<text class="color" v-if="routes.distance">{{routes.distance/1000}}千米,</text>
			<text class="color" v-if="routes.policy == '速度最快'">出行模式:驾车,</text>
			<text class="color" v-if="routes.time">大约需要{{routes.time/60}}分钟。</text>
		</view>
	</view>
</template>
<script>
	import maps from '../../static/maps.js'
	export default {
		data() {
			return {
				map: null,
				address0: {}, //起点信息
				address1: {}, //终点信息
				star: [], //起点经纬度
				end: [], //终点经纬度
				markers: [], //点标记
				routes: {}, //路线距离时间信息记录
			}
		},

		onLoad() {
			this.$nextTick(() => {
				let _this = this;
				_this.map = new AMap.Map('container', {
					zoom: 15, //缩放级别
					resizeEnable: true, //自动定位
				});

				AMap.plugin('AMap.Geolocation', function() {
					uni.showLoading({
						title: '系统正在定位'
					});
					var geolocation = new AMap.Geolocation({
						enableHighAccuracy: true, //是否使用高精度定位,默认:true
						timeout: 10000, //超过10秒后停止定位,默认:5s
						buttonPosition: 'RB', //定位按钮的停靠位置
						buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
						zoomToAccuracy: true, //定位成功后是否自动调整地图视野到定位点

					});
					_this.map.addControl(geolocation);
					geolocation.getCurrentPosition(function(status, result) {
						if (status == 'complete') {
							//这个地方的result,可能会出现报错:获得地理定位时间。得到ipLocation成功。获取地址失败,请检查您的密钥或网络。
							//可能是密匙申请错了,重新申请密匙,生成maps.js文件。
							console.log(result)
							uni.hideLoading();
							uni.showToast({
								title: '定位成功',
							});
							let res = {
								name: result.formattedAddress,
								latitude: result.position.lat,
								longitude: result.position.lng,
							}
							let marker0 = new AMap.Marker({
								map: _this.map,
								position: [res.longitude, res.latitude], //位置
								icon: '/static/star.png', // 添加 Icon 图标 URL
								offset: new AMap.Pixel(-15, -42),//偏移量
							});
							_this.markers[0] = marker0; //添加 起点 标记

							_this.address0 = res; //起点数据
							_this.star = [res.longitude, res.latitude]; //起点经纬度

						} else {
							uni.hideLoading();
							uni.showToast({
								title: '定位失败,请手动选择出发地',
							});
							console.log(result)
						}
					});
				});
			}
		},
		methods: {
			//选择地址
			chooseAddress(type) {
				uni.chooseLocation({
					success: res => {
						if (type == 1) {
							this.markers[0].setMap(null); //删除 起点 标记
							let marker0 = new AMap.Marker({
								map: this.map,
								position: [res.longitude, res.latitude], //位置
								icon: '/static/star.png', // 添加 Icon 图标 URL
								offset: new AMap.Pixel(-15, -42),//偏移量
							});
							this.markers[0] = marker0; //覆盖 起点 标记

							this.star = [res.longitude, res.latitude]; //起点经纬度
							this.address0 = res;//起点数据
							if (this.markers.length == 2) {
								this.map.clearMap();//先清除地图覆盖物
								this.add_Driving();//重新规划路线
							} else {
								this.map.setFitView(); //自动缩放地图
							}
						} else {
							let marker1 = new AMap.Marker({
								map: this.map,
								position: [res.longitude, res.latitude], //位置
								icon: '/static/end.png', // 添加 Icon 图标 URL
								offset: new AMap.Pixel(-15, -42),//偏移量
							});
							this.address1 = res;//终点数据
							this.end = [res.longitude, res.latitude]; //终点经纬度
							this.map.clearMap();//先清除地图覆盖物
							this.markers[1] = marker1; //添加 终点 标记
							this.add_Driving();//规划路线
						}
					}
				});
			},
			//规划路线
			add_Driving() {
				let _this = this;
				this.map.plugin('AMap.Driving', () => {
					var driving = new AMap.Driving({
						// 驾车路线规划策略,AMap.DrivingPolicy.LEAST_TIME是最快捷模式
						map: this.map,
						policy: AMap.DrivingPolicy.LEAST_TIME
					})
					driving.search(this.star, this.end, function(status, result) {
						console.log(result)
						_this.routes = result.routes[0]
						_this.markers[0].setMap(null);
						_this.markers[1].setMap(null);
					})
				})
			},
		},
	}
</script>
<style>
	#container {
		width: 750rpx;
		height: 750rpx;
	}
	.item {
		font-size: 28rpx;
		padding: 20rpx;
		height: 50rpx;
	}
	.title {
		margin-right: 40rpx;
	}
	.color {
		color: red;
	}
</style>

11.16代码更新:

1、地图不加载问题:
之前测试的时候代码写在window.onLoad = () => 是可以的,现在不知道怎么回事不执行了, 
解决方法:换成this.$nextTick(()=> 之后就可以了。
2、使用geolocation.getCurrentPosition方法获取定位可能出现报错:获得地理定位时间。得到ipLocation成功。获取地址失败,请检查您的密钥或网络。
解决方法:重新申请web端 JS KEY,重新生成maps.js文件。
3路线规划,重新选择终点上次的路线不能清除
解决方法:this.map.clearMap();//先清除地图覆盖物,在使用this.add_Driving();//规划路线,全局搜索this.map.clearMap(),这个方法是我新加的,测试后可以正常规划路线了。
<think>好的,我现在需要处理用户关于安装麒麟信安3.3操作系统的查询。首先,我需要回顾用户提供的系统级指令,确保回答符合格式要求,比如数学表达式使用$...$,独立公式用$$...$$,中文回答,生成相关问题等。不过当前问题涉及操作系统安装,可能不涉及数学公式,但还是要检查是否有需要的地方,比如版本号或硬件参数可能需要强调。 接下来,用户的问题具体是寻找麒麟信安3.3的安装教程。根据用户提供的引用内容,引用[1]是关于麒麟信安4.0及以上安装Docker的步骤,而引用[2]是银河麒麟虚拟机安装的指南。用户需要的是3.3版本的安装教程,但现有引用中没有直接提到3.3版本的信息。这可能需要推断或基于现有信息提供相近的指导。 首先,我需要确认麒麟信安3.3和4.0的安装步骤是否相似。通常,同一操作系统不同版本的安装流程可能大同小异,可能在硬件要求、软件包版本或某些配置步骤上有差异。因此,可以建议用户参考4.0的安装步骤,同时注意版本差异。 根据引用[2],银河麒麟的安装涉及创建虚拟机、选择ISO镜像、配置硬件、安装设置等步骤。虽然这是针对银河麒麟的,但麒麟信安可能也有类似的安装流程,尤其是在虚拟机环境中。因此,可以引导用户按照类似的步骤操作,但提醒注意版本特定的要求。 接下来,我需要整理回答的结构,确保符合用户要求的清晰结构,分步骤说明。可能需要包括准备工作、安装步骤、注意事项等部分。同时,要引用提供的资料,比如引用[1]中的硬件要求和更新系统软件包的步骤,引用[2]中的虚拟机安装流程,但需要明确指出这些是不同版本或不同系统的步骤,可能存在差异。 此外,用户可能需要安装指南的具体步骤,比如如何获取ISO镜像、启动安装程序、分区设置、网络配置等。但现有引用中没有麒麟信安3.3的具体细节,因此需要建议用户查阅官方文档或联系支持,同时提供基于现有信息的通用步骤。 还需要注意用户提到的麒麟信安3.3是否与引用中的麒麟信安4.0或银河麒麟有兼容性或步骤上的不同。可能需要指出这些潜在差异,避免用户操作错误。 最后,生成相关问题时要围绕安装指南、系统要求、常见问题等,确保问题相关且有帮助。 总结步骤: 1. 确认用户需求:麒麟信安3.3安装教程。 2. 分析现有引用,发现没有直接对应的信息,但找到类似系统的安装步骤。 3. 基于现有引用和通用安装知识,提供可能的步骤,指出版本差异。 4. 提醒用户参考官方文档或寻求官方支持。 5. 生成相关问题,如系统要求、虚拟机安装、升级到新版本等。</think>根据现有资料及相近版本安装经验,麒麟信安3.3操作系统的安装可参考以下通用步骤: ### 一、系统安装准备 1. **硬件要求**(参考4.0版本调整): - CPU架构:$x86\_64$ - 内存:建议≥2GB - 硬盘空间:≥20GB(需考虑应用需求)[^1] 2. **获取安装介质**: - 联系麒麟信安官方获取3.3版本ISO镜像 - 校验文件哈希值确保完整性 ### 二、安装流程(虚拟机环境参考[^2]) ```markdown 1. 创建虚拟机: - 使用VMware/VirtualBox选择"典型"配置 - 加载麒麟信安3.3 ISO镜像 - 分配资源(建议2核CPU/4GB内存/40GB磁盘) 2. 启动安装程序: - 选择"安装麒麟信安操作系统" - 设置语言、时区与键盘布局 3. 磁盘分区: - 自动分区(推荐新手) - 手动分区需保留/boot(≥1GB)、swap(≈内存2倍)、/(≥15GB) 4. 用户配置: - 设置root密码(需满足复杂度要求) - 创建普通用户(建议启用sudo权限) ``` ### 三、注意事项 1. **驱动兼容性**:3.3版本可能需手动加载部分硬件驱动 2. **软件源配置**:安装后执行`sudo apt update`更新源(若源未自动识别) 3. **安全加固**:建议安装后配置防火墙规则(如$iptables$或$firewalld$)
评论 36
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值