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 天前
Uniapp开发pc端,小程序和APK
小程序·uni-app
2501_915918412 天前
iOS性能测试工具 Instruments、Keymob的使用方法 不局限 FPS
android·ios·小程序·https·uni-app·iphone·webview
2501_915918412 天前
iOS 混淆流程 提升 IPA 分析难度 实现 IPA 深度加固
android·ios·小程序·https·uni-app·iphone·webview
前端 贾公子2 天前
解决uni-app 输入框,键盘弹起时页面整体上移问题
前端·vue.js·uni-app
Muchen灬2 天前
【uniapp】(5) 创建gitee仓库并推送源码
gitee·uni-app
Muchen灬2 天前
【uniapp】(6) uniapp中使用vuex
uni-app
2501_915909062 天前
React Native 上架 App Store:项目运行与审核构建的流程
android·ios·小程序·https·uni-app·iphone·webview
李庆政3702 天前
uniapp+unicloud打包部署微信小程序
微信小程序·小程序·uni-app
2501_915909062 天前
苹果 Safari 浏览器抓包 页面刷新后的请求分析
android·前端·ios·小程序·uni-app·iphone·safari