uniapp 地图(map)加载大量自定义坐标卡顿优化

事情原因是做项目遇到一个需求,地图上定位描点,但是由于数据量暴增,直接导致地图卡死。

所以想到的解决方案是,只加载当前屏幕中的 地图描点信息。

实现方法是使用 regionChange 方法

但是还有问题就是 万一缩放到 又要加载全部数据的定位一样会卡死

我的解决办法就是优化同屏显示数量,我这里是默认最多显示 200个

javascript 复制代码
<template>
  <view>
    <map id="myMap" :markers="visibleMarkers" :latitude="latitude" :longitude="longitude" :scale="scale" @regionchange="regionChange"></map>
  </view>
</template>

<script>
export default {
  data() {
    return {
      latitude: 39.909,
      longitude: 116.3974,
      scale: 12,
      allMarkers: [
        // 这里是所有的标注点数据,包含经纬度等信息
        { id: 1, latitude: 39.91, longitude: 116.39, name: 'Marker 1' },
        { id: 2, latitude: 39.92, longitude: 116.40, name: 'Marker 2' },
        // 更多的标注点...
      ],
      visibleMarkers: [] // 存储当前可见区域的标注点
    };
  },
  methods: {
    regionChange(event) {
      // 仅在用户结束拖动或缩放操作时更新标记点
      if (event.type === 'end') {
        const mapContext = uni.createMapContext('myMap');
        mapContext.getRegion({
          success: (res) => {
            const { southwest, northeast } = res;
            const visibleMarkers = this.allMarkers.filter(marker => {
              return marker.latitude >= southwest.latitude && marker.latitude <= northeast.latitude &&
                     marker.longitude >= southwest.longitude && marker.longitude <= northeast.longitude;
            });
            // 限制 visibleMarkers 的数量为 200 个
            this.visibleMarkers = visibleMarkers.slice(0, 200);
          }
        });
      }
    }
  }
};
</script>
相关推荐
2501_9160088911 小时前
iOS开发APP上架全流程解析:从开发到App Store的完整指南
android·ios·小程序·https·uni-app·iphone·webview
酷酷的鱼11 小时前
2026 跨平台开发终极选型:Flutter, React Native 与 uni-app x 深度博标与规划指南
flutter·react native·uni-app
biyezuopinvip11 小时前
基于uni-app和Express的问答对战小程序的设计与实现(论文)
小程序·uni-app·毕业设计·论文·express·毕业论文·问答对战小程序的设计与实现
2501_915909061 天前
Charles 抓不到包怎么办?iOS 调试过程中如何判断请求路径
android·ios·小程序·https·uni-app·iphone·webview
2501_916007471 天前
iOS和iPadOS文件管理系统全面解析与使用指南
android·ios·小程序·https·uni-app·iphone·webview
卜锦元1 天前
EchoChat搭建自己的音视频会议系统01-准备工作
c++·golang·uni-app·node.js·音视频
敲敲了个代码1 天前
让 Vant 弹出层适配 Uniapp Webview 返回键
前端·javascript·vue.js·学习·面试·uni-app
2501_915921431 天前
iOS App 开发阶段性能优化,观察 CPU、内存和日志变化
android·ios·性能优化·小程序·uni-app·iphone·webview
木子啊1 天前
UNIAPP国内房贷计算器
uni-app·房贷·房贷计算器·房贷利率·公积金贷款·商业贷款
游戏开发爱好者81 天前
在 iOS 开发、测试与上架过程中 如何做证书管理
android·ios·小程序·https·uni-app·iphone·webview