vue3 移动端h5 加载高德地图 封装地图组件

效果展示:

准备工作:

首先,需要注册成为高德开放平台的开发者,并申请 Web 平台(JS API)的 key 和安全密钥(在 2021 年 12 月 2 日之后申请的 key 需要配合安全密钥一起使用)
https://console.amap.com/dev/key/app





安装高德地图 API:

使用 npm 安装高德地图 API 的加载器

javascript 复制代码
npm i @amap/amap-jsapi-loader --save

创建地图组件:

在项目中新建一个 MapContainer.vue 文件,用作地图组件

javascript 复制代码
<script setup>
import { ref, toRefs, onMounted, onUnmounted, defineProps, defineEmits } from 'vue';
import AMapLoader from '@amap/amap-jsapi-loader';

// 定义组件的 props
const props = defineProps({
    height: {
        type: String,
        default: '800px'
    },
    width: {
        type: String,
        default: '100%'
    },
    options: {
        type: Object,
        default: () => ({
            zoom: 14,
            resizeEnable: true,
            viewMode: '2D',
            mapStyle: 'amap://styles/normal',
            lang: 'zh_cn'
        })
    }
});

// 解构 props
const { height, width, options } = toRefs(props);

// 定义事件
const emit = defineEmits(["map"]);

// 地图实例引用
const map = ref(null);

// 初始化地图
const initMap = () => {
    // 设置高德地图的安全配置
    window._AMapSecurityConfig = {
        securityJsCode: '你的安全密钥'
    };

    // 加载高德地图 SDK
    AMapLoader.load({
        key: '你的key',
        version: '2.0',
        plugins: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale'],
    }).then((AMap) => {
        // 创建地图实例
        map.value = new AMap.Map('container', { ...options.value });
        // 触发 map 事件,传递地图实例
        emit('map', map.value);
    }).catch((e) => {
        console.error('AMap 未找到,请确保正确引入了高德地图 SDK', e);
    });
};

// 组件挂载时初始化地图
onMounted(() => {
    initMap();
});

// 组件卸载时销毁地图实例
onUnmounted(() => {
    if (map.value) {
        map.value.destroy();
        map.value = null;
    }
});
</script>

<template>
    <div id="container" :style="{ height: height, width: width }"></div>
</template>

<style scoped>
#container {
    width: 100%;
    height: 100%;
}
</style>

使用地图组件:

在你需要的页面引入地图组件

javascript 复制代码
<script setup>
import { ref, onMounted } from 'vue';
import MapContainer from '@/components/MapContainer.vue';

const height = ref(`${window.innerHeight}px`);
const amap = ref(null);
const amapOptions = {
    zoom: 14,
    resizeEnable: true,
    viewMode: '3D',
    mapStyle: 'amap://styles/macaron',
    center: [121.492156, 31.233462],
    lang: 'zh_cn'
};
const marker = ref(null);
const getAMapData = (map) => {
    amap.value = map;
    marker.value = new AMap.CircleMarker({
        center: [121.492156, 31.233462],
        radius: 20,
        strokeColor: "white",
        strokeWeight: 2,
        strokeOpacity: 0.5,
        fillColor: "rgba(255,0,175,1)",
        fillOpacity: 0.5,
        zIndex: 10,
        map: amap.value
    });
};
</script>

<template>
    <div class="map-container">
        <MapContainer :height="height" :options="amapOptions" @map="getAMapData" />
    </div>
</template>

<style lang="scss" scoped>
.map-container {
    height: 100%;
    width: 100%;
}
</style>
相关推荐
551只玄猫1 天前
【海洋空间信息工程概论 实验报告2】海洋空间数据管理
arcgis·课程设计·实验报告·海洋·地理·海洋空间工程
薛定猫AI3 天前
【一键配置】优雅使用Claude Code:从安装到上手的完整教程
人工智能·arcgis·编辑器·vim
NULIWEIMENXIANG3 天前
ArcPy 程序调用 QGIS 进程实现几何拓扑检查
python·arcgis·gis
三*一3 天前
基于 Turf.js 实现高精度多边形修整工具(模拟 ArcGIS 修整功能)
开发语言·前端·javascript·arcgis·maobox gl·turf.js
智航GIS3 天前
ArcGIS大师之路500技---074ArcGIS数据压缩:解决大文件卡顿的实用技巧
arcgis
城数派4 天前
我国省市县三级的餐饮服务设施数量(57类餐饮服务/Excel/Shp格式)2025年
arcgis·信息可视化·数据分析·excel
b_xinjun11204 天前
树莓派 Ubuntu 24.04.3 LTS 安装 OpenClaw 操作说明
linux·ubuntu·arcgis
科研online5 天前
基于ArcGIS的区域碳排放精细核算及空间格局分析
arcgis
asyxchenchong8885 天前
RWEQ 模型:ArcGIS+Arcpy 全流程解析风蚀模数估算与归因分析
arcgis
木易GIS5 天前
使用arcpy,批量读取多个文件夹的*.shp中的图层,统计提取图层的个数和要素总个数
python·arcgis