uniapp 实现高德地图导航

效果图:

 

<template>
	<view>
		<view class="page-body">
			<view class="page-section page-section-gap">
				<map id="map" @markertap="bindTap" @controltap="bindControltap" :controls="controls"
					style="width: 100%;height: 100vh;" :markers="markers" :scale="15" :latitude="latitude"
					:longitude="longitude">
					<cover-view @click="startNav">开始导航</cover-view>
				</map>
			</view>
		</view>
	</view>
</template>

<script>
	import { mapTool } from './mapTool.js'
	export default {
		data() {
			return {
				subNVue: '',
				subNVueShowDetail: '',
				msg: '',

				id: 0, // 使用 marker点击事件 需要填写id
				title: 'map',
				// longitude: 116.39742,  // 默认的是北京经纬度
				// latitude: 39.909,
				longitude:106.220885,  
				latitude: 29.590185,
				controls: [],
				markers: [
					{
						id: 'gyqy',
						latitude: '29.595587852178966',
						longitude: '106.22169495575275',
						title: '企业',
						iconPath: 'logo.png'
					}
				]
			}

		},
		
		onLoad() {
			this.getDeviceLocation()
			
		},
		onReady() {
			this.createMap() //地图创建在页面中需要在onReady调用
		},
		methods: {
			// 创建地图
			createMap() {
				let mapContext = uni.createMapContext('map')
				mapContext.$getAppMap().showUserLocation() //显示用户位置
				let screenWidth =  uni.getSystemInfoSync().screenWidth
				var coefficient=screenWidth/750
				this.controls=[
					{
						id:"mapType",
						controlId:"mapType",
						position:{
							width: 72*coefficient,
							height: 72*coefficient,
							left: 658*coefficient,
							top:110*coefficient
						},
						iconPath:"./logo.png",
						clickable:true
					}
				]
				
			},
			// 获取设备位置
			getDeviceLocation() {
				plus.geolocation.getCurrentPosition(function(p){
					let longitude = p.coords.longitude 
					let latitude = p.coords.latitude
					console.log(`经度:${longitude} 纬度:${latitude}`)
					this.longitude = longitude
					this.latitude = latitude
					}, function(e){
						console.log('Geolocation error: ' + e);
					},{"enableHighAccuracy":"true", // 是否高精确度获取位置信息
						"provider": "system"  // 优先使用的定位模块
					});
			},
			startNav() {
				// 这里需要使用的时wgs8坐标系即GPS定位 设置目标位置坐标点和开始位置坐标点
				var wgs84togcj02=mapTool.wgs84togcj02(this.longitude,this.latitude)
				// latitude: "29.59827298936982" ; longitude: "106.21791710134177"
				var dst = new plus.maps.Point(106.21791710134177,29.59827298936982);// 目的点  
				var src = new plus.maps.Point(wgs84togcj02[0],wgs84togcj02[1]); // 开始点
				// 调用系统地图显示 
				plus.maps.openSysMap( dst, "测试导航到你想去的地方", src );
			},
			bindControltap(e) {
				console.log(e)
			},
			bindTap(e) {
				console.log(e)
			},
		}
	}
</script>

<style>

</style>

原文链接:https://juejin.cn/post/6985149247591022623

### 实现高德地图导航功能 #### 准备工作 为了在 UniApp 项目中集成高德地图导航功能,需完成如下准备工作: - 注册成为高德开放平台开发者并获取 API Key[^1]。 ```javascript // 获取API key后的操作可以是保存到项目的配置文件中以便后续调用 const apiKey = 'your_api_key_here'; ``` #### 创建应用与下载 SDK 前往高德开放平台创建新应用,并申请相应的服务权限。对于 H5 和 App 平台而言,应确保选择了适合的地图服务选项。接着按照官方文档指示下载适用于目标环境(如微信小程序、Web 或 Android/iOS 应用)的最新版 SDK 文件[^2]。 #### 页面设置 在希望展示地图或启动导航的位置添加 `<map>` 组件标签,并通过属性绑定来传递必要的参数给该组件实例。这些参数可能包括但不限于中心点坐标(`latitude` `longitude`)、缩放级别(`scale`)以及标记物数组(`markers`)等。 ```html <template> <view> <!-- 地图容器 --> <map id="myMap" class="map" :latitude="latitude" :longitude="longitude" scale="14" :markers="markers" show-location @markertap="handleMarkerTap"> </map> <!-- 导航按钮或其他交互控件可放置于此处 --> <button type="primary" @click="startNavigation">开始导航</button> </view> </template> ``` #### JavaScript 方法定义 编写用于处理用户点击事件的方法,比如当用户点击某个地点时触发导航行为。这里假设已经有一个方法用来初始化地图对象并且绑定了监听器等待用户的输入动作。 ```javascript export default { data() { return { latitude: 39.9087, // 默认纬度 longitude: 116.3975, // 默认经度 markers: [], // 标记列表 }; }, methods: { handleMarkerTap(e) { /* ... */ }, startNavigation(destinationLatlng) { const originLatLng = `${this.latitude},${this.longitude}`; if (process.env.VUE_APP_PLATFORM === 'h5') { window.location.href = `https://uri.amap.com/navigation?from=${originLatLng}&to=${destinationLatlng.join(',')}&mode=car&plugin=AMap.NaviWalk&src=mypage`; } else if (['app-plus', 'app-nvue'].includes(process.env.UNI_PLATFORM)) { plus.maps.Map.openSysMap({ from: new plus.maps.Point(this.longitude, this.latitude), to: new plus.maps.Point(...destinationLatlng.split(',').reverse()), mode: "drive", success: function () {}, fail: function (e) {} }); } } } } ``` 上述代码片段展示了如何根据不同运行环境选择合适的 URL Scheme 来打开系统的默认导航应用程序或者直接利用原生接口发起请求。
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流氓也是种气质 _Cookie

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值