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_9159090612 小时前
iOS 反编译防护工具全景解析 从底层符号到资源层的多维安全体系
android·安全·ios·小程序·uni-app·iphone·webview
lvha14 小时前
uniapp BLE低功耗蓝牙插件 支持安卓 iOS 鸿蒙NEXT 微信小程序
uni-app·蓝牙
Redundantº19 小时前
Uniapp 适配安卓与 iOS 的 PDF、DOC 文件上传
android·ios·pdf·uni-app·webview
Harry技术19 小时前
UniApp H5 代理失效的终极替代方案
uni-app
2501_9159184121 小时前
iOS 应用如何防止破解?从逆向链路还原攻击者视角,构建完整的反破解工程实践体系
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_9160074721 小时前
iOS 压力测试的工程化体系 构建多工具协同的极限稳定性验证方案
android·ios·小程序·uni-app·压力测试·iphone·webview
2501_9160074721 小时前
iOS 应用上架流程的工程化拆解 从签名体系到提交审核的全过程管控
android·ios·小程序·https·uni-app·iphone·webview
一殊酒1 天前
【前端开发】Uniapp:Android/IOS云打包
android·ios·uni-app
yqcoder1 天前
uni-app 之 uni.showLoading
uni-app
2501_915918411 天前
构建可靠的 iOS 日志导出体系,从真机日志到系统行为的多工具协同实践
android·ios·小程序·https·uni-app·iphone·webview