vue 高德地图实现自定义多标记点功能

本文详细介绍了如何使用Amap JS API在Web应用中实现单个和多个标记的添加,包括3D视图下定位、内容定制及批量坐标处理,适合前端开发者学习和实践地图组件开发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先来完成后的效果图
多标记点自定义样式

单标记点自定义样式
首先安装 :npm i @amap/amap-jsapi-loader --save
在使用地图的页面 引入:import AMapLoader from ‘@amap/amap-jsapi-loader’;
处理单标记:

import AMapLoader from '@amap/amap-jsapi-loader';
export default {
      data() {
            return {
                  map: null,
            }
      },
      mounted() {
          this.initMap();
      },
      methods: {
            // 初始化地图
            initMap() {
                  AMapLoader.load({
                        key: "XXXX",             // 申请好的Web端开发者Key,首次调用 load 时必填
                        version: "2.0",      // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
                        plugins: [''],       // 需要使用的的插件列表,如比例尺'AMap.Scale'等
                  }).then((AMap) => {
                        this.map = new AMap.Map("container", {  //设置地图容器id
                              viewMode: "3D",    //是否为3D地图模式
                              zoom: 18,           //初始化地图级别
                              center: [113.932497, 22.540517], //初始化地图中心点位置
                        });
                        var marker = new AMap.Marker({
                              position: new AMap.LngLat(113.932497,
                                    22.540517), // 经纬度对象,也可以是经纬度构成的一维数组[116.39, 39.9]
                              content: `
                                    <div class="my_marker">
                                          <div class="showLog">
                                               SOS 
                                          </div>
                                          <div class="marker">
                                                <div class="title">
                                                      <img src="https://img.vinua.cn/images/jkGv.png" alt="">
                                                </div>
                                          </div>
                                         
                                          
                                    </div>`,

                              // icon: 'https://s1.ax1x.com/2022/05/27/XeLFGn.png', // 添加 Icon 图标 URL
                              offset: new AMap.Pixel(-15, -20),

                        });
                        // var circle = new AMap.Circle({
                        // 		center: new AMap.LngLat("113.934497", "22.540517"), // 圆心位置
                        // 		radius: 10, //半径
                        // 		strokeColor: "#1aec1a", //线颜色
                        // 		strokeOpacity: 1, //线透明度
                        // 		strokeWeight: 1, //线粗细度
                        // 		fillColor: "#1aec1a", //填充颜色
                        // 		fillOpacity: 0.35 //填充透明度
                        // 	});
                        // 将创建的点标记添加到已有的地图实例:
                        this.map.add([marker]);
                  }).catch(e => {
                        console.log(e);
                  })
            },
      }
}

处理多标记:

   // 初始化地图
    initMap() {
       let arr = [[117.517694,32.414674],[111.145623,27.583686],[113.474725,30.692175]];
      AMapLoader.load({
        key: "xxxx",              // 申请好的Web端开发者Key,首次调用 load 时必填
        version: "2.0",                                       // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        plugins: [''],                                         // 需要使用的的插件列表,如比例尺'AMap.Scale'等
      }).then((AMap) => {
        this.map = new AMap.Map("container", {                   //设置地图容器id
          viewMode: "3D",                                       //是否为3D地图模式
          zoom: 5,                                               //初始化地图级别
          center: [112.886125, 28.130778],                         //初始化地图中心点位置
        });
        // 循环所有的标记点
        for( let i =0; i <  arr.length; i++) {
          var marker = new AMap.Marker({
          position: arr[i],                                        // 经纬度对象,也可以是经纬度构成的一维数组[116.39, 39.9]
          map:this.map,
          content: `
                  <div class="my_marker">
                        <div class="marker">
                            ${this.winBox}所
                        </div>
                        
                  </div>`,
          offset: new AMap.Pixel(-15, -20),

        });
          // 将创建的点标记添加到已有的地图实例:
         this.map.add([marker]);
       
        }
        
      //矩形
              // var circle = new AMap.Circle({
              // 		center: new AMap.LngLat("113.934497", "22.540517"), // 圆心位置
              // 		radius: 10, //半径
              // 		strokeColor: "#1aec1a", //线颜色
              // 		strokeOpacity: 1, //线透明度
              // 		strokeWeight: 1, //线粗细度
              // 		fillColor: "#1aec1a", //填充颜色
              // 		fillOpacity: 0.35 //填充透明度
              // 	});
       
      
       
      }).catch(e => {
        console.log(e);
      })
    },
Vue高德地图自定义标记可以通过以下步骤实现: 1.Vue组件中引入高德地图API: ``` import AMapLoader from '@amap/amap-jsapi-loader'; ``` 2.Vue组件中定义地图容器,并在mounted钩子函数中加载地图: ``` <template> <div id="map-container"></div> </template> <script> export default { data() { return { map: null, }; }, mounted() { AMapLoader.load({ key: 'your-api-key', version: '2.0', plugins: ['AMap.Marker'], }).then(() => { this.initMap(); }); }, methods: { initMap() { this.map = new AMap.Map('map-container', { center: [116.397428, 39.90923], zoom: 13, }); }, }, }; </script> ``` 3.Vue组件中定义自定义标记的样式,并使用AMap.Marker类创建自定义标记对象: ``` <template> <div id="map-container"></div> </template> <script> export default { data() { return { map: null, markerStyle: { width: '24px', height: '24px', background: 'url("your-marker-icon.png") no-repeat', backgroundSize: '100% 100%', }, }; }, mounted() { AMapLoader.load({ key: 'your-api-key', version: '2.0', plugins: ['AMap.Marker'], }).then(() => { this.initMap(); this.addMarker(); }); }, methods: { initMap() { this.map = new AMap.Map('map-container', { center: [116.397428, 39.90923], zoom: 13, }); }, addMarker() { const marker = new AMap.Marker({ position: [116.397428, 39.90923], map: this.map, icon: new AMap.Icon(this.markerStyle), }); }, }, }; </script> ``` 4. 根据需要,可以在自定义标记上添加事件监听器: ``` <template> <div id="map-container"></div> </template> <script> export default { data() { return { map: null, markerStyle: { width: '24px', height: '24px', background: 'url("your-marker-icon.png") no-repeat', backgroundSize: '100% 100%', }, }; }, mounted() { AMapLoader.load({ key: 'your-api-key', version: '2.0', plugins: ['AMap.Marker'], }).then(() => { this.initMap(); this.addMarker(); }); }, methods: { initMap() { this.map = new AMap.Map('map-container', { center: [116.397428, 39.90923], zoom: 13, }); }, addMarker() { const marker = new AMap.Marker({ position: [116.397428, 39.90923], map: this.map, icon: new AMap.Icon(this.markerStyle), }); marker.on('click', () => { alert('Marker clicked!'); }); }, }, }; </script> ``` 以上就是Vue高德地图自定义标记的实现方法。需要注意的是,自定义标记的样式需要根据实际情况进行调整。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值