Vue2 中使用 gmap - marker 和 GmapCluster 实现谷歌地图标点与聚合

Vue2 中使用 gmap - marker 和 GmapCluster 实现谷歌地图标点与聚合

一、安装与配置依赖

在项目开始前,请确保已安装vue2-google-maps依赖。若尚未安装,在项目根目录下运行以下命令:

复制代码
npm install vue2-google-maps

随后,在main.js文件中进行基础配置,引入你的谷歌地图 API 密钥(可在谷歌云平台申请):

javascript 复制代码
import Vue from 'vue';

import App from './App.vue';

import { VueGoogleMaps } from 'vue2-google-maps';

Vue.use(VueGoogleMaps, {

 load: {

   key: 'YOUR\_API\_KEY',

   libraries: 'places' // 若需使用地点相关功能,可添加此选项

 }

});

new Vue({

 render: h => h(App)

}).$mount('#app');

二、创建地图组件

src/components目录下创建地图组件,例如GoogleMapWithCluster.vue

xml 复制代码
<template>

 <gmap-map

   :center="{lat: 37.7749, lng: -122.4194}"

   :zoom="12"

   map-type-id="roadmap"

   style="width: 100%; height: 500px;"

 >

   <gmap-cluster>

     <gmap-marker

       v-for="(marker, index) in markers"

       :key="index"

       :position="marker.position"

       :title="marker.title"

     />

   </gmap-cluster>

 </gmap-map>

</template>

<script>

import { GmapMap, GmapMarker } from 'vue2-google-maps';

import GmapCluster from 'vue2-google-maps/src/components/cluster';

export default {

 components: {

   GmapMap,

   GmapMarker,

   GmapCluster

 },

 data() {

   return {

     markers: [

       {

         position: { lat: 37.7749, lng: -122.4194 },

         title: 'Marker 1'

       },

       {

         position: { lat: 37.7761, lng: -122.4185 },

         title: 'Marker 2'

       },

       {

         position: { lat: 37.7755, lng: -122.4178 },

         title: 'Marker 3'

       }

       // 可按需添加更多标记数据

     ]

   };

 }

};

</script>

<style scoped>

/\* 地图样式可依需求调整 \*/

</style>

代码说明:

模板部分通过gmap-map组件创建地图,设置了地图的中心坐标、缩放级别和地图类型。

gmap-cluster作为gmap-map的子组件,在gmap-cluster内部通过v-for循环渲染gmap-marker组件,每个gmap-marker对应一组标记数据,以此实现标点和聚合的展示。

脚本部分引入所需的GmapMapGmapMarkerGmapCluster组件,并在components选项中进行局部注册。同时,在data中定义markers数组,用于存储标记数据。

三、使用地图组件

App.vue文件中使用刚创建的GoogleMapWithCluster.vue组件:

xml 复制代码
<template>

 <div id="app">

   <GoogleMapWithCluster />

 </div>

</template>

<script>

import GoogleMapWithCluster from './components/GoogleMapWithCluster.vue';

export default {

 components: {

   GoogleMapWithCluster

 }

};

</script>

<style>

#app {

 font-family: Avenir, Helvetica, Arial, sans-serif;

 -webkit-font-smoothing: antialiased;

 -moz-osx-font-smoothing: grayscale;

 text-align: center;

 color: #2c3e50;

 margin-top: 60px;

}

</style>

下图是效果(样式是经过修改):

相关推荐
独泪了无痕6 分钟前
Vue3动态组件Component的深度解析与应用
前端·vue.js·web components
lbh8 小时前
当我开始像写代码一样和AI对话,一切都变了
前端·openai·ai编程
We་ct9 小时前
LeetCode 918. 环形子数组的最大和:两种解法详解
前端·数据结构·算法·leetcode·typescript·动态规划·取反
wefly20179 小时前
m3u8live.cn 在线M3U8播放器,免安装高效验流排错
前端·后端·python·音视频·前端开发工具
C澒10 小时前
微前端容器标准化 —— 公共能力篇:通用打印
前端·架构
德育处主任Pro10 小时前
前端元素转图片,dom-to-image-more入门教程
前端·javascript·vue.js
木斯佳10 小时前
前端八股文面经大全:小红书前端一二面OC(下)·(2026-03-17)·面经深度解析
前端·vue3·proxy·八股·响应式
陈天伟教授11 小时前
人工智能应用- 预测新冠病毒传染性:04. 中国:强力措施遏制疫情
前端·人工智能·安全·xss·csrf
zayzy11 小时前
前端八股总结
开发语言·前端·javascript
今天减肥吗11 小时前
前端面试题
开发语言·前端·javascript