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>
相关推荐
小恒恒1 天前
2025 Vibe Coding 有感
前端·uni-app·trae
一颗小青松1 天前
uniapp使用uni-im
uni-app
2501_916007471 天前
iPhone APP 性能测试怎么做,除了Instruments还有什么工具?
android·ios·小程序·https·uni-app·iphone·webview
2501_915106321 天前
Windows 环境下有哪些可用的 iOS 上架工具, iOS 上架工具的使用方式
android·ios·小程序·https·uni-app·iphone·webview
一颗小青松1 天前
uniapp vue3中app端使用腾讯云点播上传
uni-app·云计算·腾讯云
玄尺_0071 天前
uniapp h5端使浏览器弹出下载框
前端·javascript·uni-app
2501_915106321 天前
iOS 抓包工具有哪些?不同类型的抓包工具可以做什么
android·ios·小程序·https·uni-app·iphone·webview
web前端神器2 天前
vue、uniapp项目循环中能显示每个列表的内容,但是点击的时候传递的参数却不正确
uni-app
ModyQyW2 天前
HBuilderX 4.87 无法正常读取 macOS 环境配置的解决方案
前端·uni-app
脾气有点小暴2 天前
Uni-app App 端自定义导航栏完整实现指南
uni-app