uniapp接入高德地图

下面代码兼容安卓APP和H5

高德地图官网:我的应用 | 高德控制台 ,绑定服务选择《Web端(JS API)》

/utils/map.js 需要设置你自己的key和安全密钥

复制代码
export function myAMap() {
	return new Promise(function(resolve, reject) {
		if (typeof window.onLoadMyAMap === 'function') {
			resolve(window.onLoadMyAMap)
			return
		}
		window.onLoadMyAMap = function() {
			resolve(window.onLoadMyAMap)
		}
		window._AMapSecurityConfig = {
			securityJsCode: '你的安全密钥',
		};
		var script = document.createElement('script')
		script.type = 'text/javascript'
		script.src =
			`https://webapi.amap.com/maps?v=2.0&key=你的key&callback=onLoadMyAMap&plugin=AMap.Geocoder,AMap.AutoComplete,AMap.PlaceSearch`
		script.onerror = reject
		document.head.appendChild(script)
	})
}

页面代码

复制代码
<template>
	<view class="gaodeMap">
		<view class="map" v-bind:style="{ height: windowHeight * 2 + 'rpx'}" id="container"></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				windowHeight: "",
			}
		},
		methods: {},
		mounted() {
			//默认高度
			uni.getSystemInfo({
				success: res => {
					this.windowHeight = res.windowHeight;
				}
			});
		}
	}
</script>

<script module="allmap" lang="renderjs">
	import {
		myAMap
	} from "@/utils/map.js";

	let amap;
	const _window = window;

	export default {
		data() {
			return {
				latitude: 22.543648,
				longitude: 114.057923,
				zoom: 5,
			}
		},
		methods: {
			//获取地图信息
			initMap() {
				myAMap().then(() => {
					// 创建地图实例
					amap = new AMap.Map('container', {
						zoom: 5, // 地图缩放比例
						zooms: [4, 20], // 地图缩放区间
						center: [114.057923, 22.543648], //设置经纬度为地图中心
						resizeEnable: true, // 开启地图缩放
					});

					//  设置个性化地图
					// var styleName = "amap://styles/" + 'darkblue'
					// amap.setMapStyle(styleName);

					// 地图缩放事件
					amap.on('zoomend', (e) => {

					});

					// 地图拖拽事件
					amap.on('dragend', (e) => {

					});
				})
			},
		},
		mounted() {
			this.initMap()
		},
		beforeDestroy() {
			// 离开页面销毁地图
			amap && amap.destroy();
			amap = null
		}
	}
</script>

<style lang="scss" scoped>
</style>

效果图

相关推荐
郑州光合科技余经理1 小时前
本地生活服务系统:模块边界与结算字段怎么拆
java·开发语言·前端·后端·系统架构·uni-app·php
lhldsg1 天前
家校托管互通系统开发实战:从需求分析到部署全指南
小程序·uni-app·需求分析
paopaokaka_luck1 天前
基于springboot3+vue3+uniapp的河南非遗数字图谱小程序(协同过滤算法、数字图谱展示、ECharts 图形化分析)
java·前端·spring boot·学习·小程序·uni-app·echarts
宠友信息2 天前
社区类源码开发实践中的仿小红书系统技术要点分析
java·spring boot·redis·mysql·uni-app·vue·内容运营
郑州光合科技余经理2 天前
本地生活平台搭建:统一订单表与多后台切换怎么拆
java·开发语言·前端·系统架构·uni-app·php·ai编程
lhldsg2 天前
树洞交友系统开发实战:从匿名社交需求到技术落地
java·小程序·uni-app·交友
lhldsg3 天前
家政派单系统开发实战:从需求分析到智能调度全流程指南
java·junit·小程序·uni-app·需求分析
PedroQue993 天前
v2.6.0 新增全局返回守卫与 iOS 侧滑拦截,补齐跨端路由拦截能力
前端·uni-app
蜡台3 天前
Uniapp 开发H5 页面跳转拦截
前端·javascript·uni-app