vue中使用高德地图打多个点,处理在可视区域显示所有点位

一、引入高德地图

javascript 复制代码
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=你的key"></script>

二、dom元素

javascript 复制代码
<div id="mapContainer" style="width: 100%; height: 600px;"></div>

三、方法 李子

javascript 复制代码
//初始化地图
const map = new AMap.Map('mapContainer', {
      resizeEnable: true,
       zoom: 12,
       center: [118.778753', 32.052997]
   });

  // 假设有一个包含点位信息的数组
const  points = [
       { lng: 经度1, lat: 纬度1, title: '标记1' },
       { lng: 经度2, lat: 纬度2, title: '标记2' },
       // ...
   ]; 
 // 找到所有点位的最大经度、最小经度、最大纬度和最小纬度
  const  maxLng = points[0].lng;
  const  minLng = points[0].lng;
  const  maxLat = points[0].lat;
  const  minLat = points[0].lat;



 for (var i = 1; i < points.length; i++) {
      var point = points[i];

       if (point.lng > maxLng) {
           maxLng = point.lng;
       }

       if (point.lng < minLng) {
           minLng = point.lng;
       }

       if (point.lat > maxLat) {
           maxLat = point.lat;
       }

       if (point.lat < minLat) {
           minLat = point.lat;
       }
   }

   // 计算出覆盖所有点位的最小矩形区域
   var bounds = new AMap.Bounds([minLng, minLat], [maxLng, maxLat]);

   // 设置地图视野自动调整到最小矩形区域
   map.setFitView(bounds);

   // 循环遍历所有点位,创建标记并添加到地图上
   for (var i = 0; i < points.length; i++) {
       var point = points[i];
       
       var marker = new AMap.Marker({
           position: [point.lng, point.lat],
           title:'123'
       });

       marker.setMap(map);
   }
相关推荐
小李子呢02113 分钟前
前端八股CSS(1)---响应式布局的方法
前端·css
小李子呢02111 小时前
前端八股Vue(6)---v-if和v-for
前端·javascript·vue.js
程序员buddha1 小时前
ES6 迭代器与生成器
前端·javascript·es6
周周记笔记1 小时前
初识HTML和CSS(一)
前端·css·html
aq55356001 小时前
网页开发四剑客:HTML/CSS/JS/PHP全解析
javascript·css·html
程序员buddha1 小时前
TypeScript详细教程
javascript·ubuntu·typescript
chxii2 小时前
在 IIS 中实现 SSL 证书的自动续期
前端
周星星日记2 小时前
vue3中静态提升和patchflag实现
前端·vue.js·面试
橘子编程2 小时前
React 19 全栈开发实战指南
前端·react.js·前端框架
DanCheOo2 小时前
AI Streaming 架构:从浏览器到服务端的全链路流式设计
前端·agent