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>

效果图

相关推荐
中国胖子风清扬4 天前
GPUI 在 macOS 上编译问题排查指南
spring boot·后端·macos·小程序·rust·uni-app·web app
码云数智-园园5 天前
uni-app 实现物流进度跟踪功能:从 UI 到数据驱动的完整方案
ui·uni-app
予你@。7 天前
UniApp + Vue3 实现 Tab 点击滚动定位(微信小程序)
微信小程序·小程序·uni-app
游戏开发爱好者87 天前
完整教程:App上架苹果App Store全流程指南
android·ios·小程序·https·uni-app·iphone·webview
予你@。7 天前
uni-app progress 组件使用详解
uni-app
iOS阿玮7 天前
春节提审高峰来袭!App Store 审核时长显著延长。
uni-app·app·apple
2501_916007477 天前
ios上架 App 流程,证书生成、从描述文件创建、打包、安装验证到上传
android·ios·小程序·https·uni-app·iphone·webview
2501_915106328 天前
iPhone 文件管理,如何进行应用沙盒文件查看
android·ios·小程序·https·uni-app·iphone·webview
2501_915921438 天前
Fastlane 结合 AppUploader 来实现 CI 集成自动化上架
android·运维·ci/cd·小程序·uni-app·自动化·iphone
云游云记8 天前
vue2 vue3 uniapp (微信小程序) v-model双向绑定
微信小程序·uni-app·vue