微信小程序地图polyline坐标太多异常显示BUG

描述

微信小程序map地图上显示polyline线,点位超过1250个出现bug,(仅真机上出现,模拟器上正常)

这里以加载四川省边界为例, 以下是示例代码

ini 复制代码
// 读取geojson数据
uni.request({
  url: 'https://geo.datav.aliyun.com/areas_v3/bound/510000.json',
  success: ({ data: geojsonData }) => {
    console.log(geojsonData);
​
    // 取出坐标点位数组
    const coordinates = geojsonData.features[0].geometry.coordinates;
​
    const polylineArr = [];
    for (let coordinate of coordinates) {
      polylineArr.push({
        points: coordinate[0].map(v => ({
          latitude: v[1],
          longitude: v[0]
        })),
        width: 2,
        color: '#FF6600',
        dottedLine: false
      });
    }
    this.polylines = polylineArr; // map组件上的polylines
  }
});

在复现问题的时候,我发现只要设置了 width:2 的情况下才会出现该BUG !!!!离谱!莫名其妙!!!

解决

  1. 设置线宽width为2以上
  2. 通过分割数组的方式,绘制出多个polyline
ini 复制代码
// 读取geojson数据
uni.request({
  url: 'https://geo.datav.aliyun.com/areas_v3/bound/510000.json',
  success: ({ data: geojsonData }) => {
    console.log(geojsonData);
​
    // 取出坐标点位数组
    const coordinates = geojsonData.features[0].geometry.coordinates;
​
    const polylineArr = [];
    for (let coordinate of coordinates) {
      const rings = coordinate[0];
      // 将数组按1000拆分成多个(测试了, 1200仍然有问题, 这里尽量调低一点)
      const newRings = splitArrayIntoChunks(rings, 1000);
​
      // 遍历拆分后的数组
      for (let ring of newRings) {
        polylineArr.push({
          points: ring.map(v => ({
            latitude: v[1],
            longitude: v[0]
          })),
          width: 2,
          color: '#FF6600',
          dottedLine: false
        });
      }
    }
    this.polylines = polylineArr; // map组件上的polylines
  }
});
​
​
/**
 * 切割数组
 * @param {Object} array  原数组
 * @param {Object} chunkSize  切割大小
 * @returns 新数组
 */
function splitArrayIntoChunks(array, chunkSize) {
  let result = [];
  for (let i = 0; i < array.length; i += chunkSize) {
    let chunk = array.slice(i, i + chunkSize);
    result.push(chunk);
  }
  return result;
}

注意:切割后仍然有bug,那就再切细一点(也就是将chunkSize值传小一点 ,比如1000/900/800等等)

相关推荐
MyFreeIT36 分钟前
Page光标focus在某个控件
前端·javascript·vue.js
通往曙光的路上36 分钟前
day8_elementPlus
前端·javascript·vue.js
Simon_He36 分钟前
最强流式渲染,没有之一
前端·面试·ai编程
你真的可爱呀37 分钟前
uniapp学习【路由跳转 +数据请求+本地存储+常用组件】
前端·学习·uni-app
Jeffrey__Lin38 分钟前
解决ElementPlus使用ElMessageBox.confirm,出现层级低于el-table的问题
前端·javascript·elementui·vue·elementplus
咖啡の猫40 分钟前
Vue-MVVM 模型
前端·javascript·vue.js
xvmingjiang40 分钟前
Element Plus el-table 默认勾选行的方法
前端·javascript·vue.js
野生yumeko2 小时前
伪静态WordPress/Vue
前端·javascript·vue.js
爱因斯坦乐2 小时前
【vue】I18N国际化管理系统
前端·javascript·vue.js·笔记·前端框架
一只游鱼2 小时前
vue集成dplayer
前端·javascript·vue.js·播放器·dplayer