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+Spring Cloud +UniApp +MySql技术开发
java·mysql·spring cloud·uni-app·源码·智慧工地·成品系统
木易 士心7 小时前
Uni-App 实现多身份动态切换 TabBar 指南
uni-app
2501_915106321 天前
CDN 可以实现 HTTPS 吗?实战要点、部署模式与真机验证流程
网络协议·http·ios·小程序·https·uni-app·iphone
LoveEate2 天前
uniapp 运行/发版微信小程序
微信小程序·小程序·uni-app
fakaifa2 天前
【高级版】沃德政务招商系统源码+uniapp小程序
小程序·uni-app·源码下载·沃德政务招商系统·招商系统源码
weixin_446938872 天前
uniapp vue-i18n如何使用
前端·vue.js·uni-app
有来技术2 天前
UniApp 自定义导航栏适配指南:微信小程序胶囊遮挡、H5 与 App 全端通用方案
微信小程序·uni-app
卷Java3 天前
违规通知功能修改说明
java·数据库·微信小程序·uni-app
卷Java3 天前
用户权限控制功能实现说明
java·服务器·开发语言·数据库·servlet·微信小程序·uni-app
王佳斌4 天前
sass变量默认
uni-app