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>
相关推荐
云起SAAS4 小时前
私域直播系统UniApp源码 多商户商城+直播带货 微信小程序+H5+安卓iOS
android·微信小程序·uni-app·私域直播系统
专科3年的修炼2 天前
uni-app移动应用开发第四章
开发语言·javascript·uni-app
q5507071772 天前
uniapp/uniappx实现原生图片编辑涂鸦、贴图、滤镜、旋转、裁剪等
uni-app
计算机学姐3 天前
基于微信小程序的校园失物招领管理系统【uniapp+springboot+vue】
java·vue.js·spring boot·mysql·信息可视化·微信小程序·uni-app
2501_915921433 天前
HTTPS前端劫持 新一代流量劫持解决方案
前端·网络协议·ios·小程序·https·uni-app·iphone
爱怪笑的小杰杰3 天前
优化 UniApp 日历组件的多语言切换:告别 setLocale 引起的 App 重启
java·前端·uni-app
计算机学姐3 天前
基于微信小程序的宠物服务系统【uniapp+springboot+vue】
java·vue.js·spring boot·mysql·微信小程序·uni-app·宠物
2501_915909063 天前
iOS应用签名的三种方法全解析:从官方到第三方工具
android·ios·小程序·https·uni-app·iphone·webview
心中无石马4 天前
uniapp引入tailwindcss4.x
前端·css·uni-app
fix一个write十个4 天前
【uniApp开发】微信小程序 web-view 内嵌 H5 跳转支付踩坑实录
微信小程序·uni-app