uniapp-微信小程序-map组件-自定义marker

前言

该文章记录一下工作中遇到的一些问题,后续将会逐步增加,所有内容均从网上整理而来,加上自己得理解做一个整合,方便工作中使用。

需求:微信小程序需要自定义地图的marker,来展示不同用户的头像。

问题

ios使用自定义marker,使用网络图片,都没问题;安卓的有问题,加载不出来,而且样式设置也不生效(比如设置一个放大1.2倍,不会生效),在微信开发社区也有很多人提问,管理知道这个bug后也没回复什么时间解决,希望官方后续能够解决。

代码

js 复制代码
<map
  class="map-element"
  ref="mapRef"
  id="myMap"
  :enable-3D="false"
  :min-scale="16"
  :max-scale="20"
  :enable-rotate="false"
  :show-location="true"
  :enable-poi="true"
  :latitude="mapPoint.lttd"
  :longitude="mapPoint.lgtd"
  :markers="markers" //markers
  @tap="onMapTap"//点击地图
  @callouttap="onCalloutTap"//点击自定义marker元素事件
  @regionchange="onRegionChange"//移动地图
>
  //要使用 v-slot:callout 插槽,表示自定义marker
  //要覆盖在地图上,只能用cover-view和cover-image两个组件
  //marker-id 必须是纯数字的
  <template v-slot:callout>
    //ios
    <cover-view
      v-if="osName === 'ios'"
      v-for="(item, i) in markers"
      :key="i"
      class="custom-callout"
      :class="chooseMarkerId === item.id ? 'choose' : ''"
      :marker-id="item.id"
    >
      <cover-image
        class="cover-bg"
        src="../../../static/images/temporarily/markerBg.png"
      ></cover-image>
      <cover-view class="callout-cover">
        <cover-image
          :webp="true"
          @load="load(i)"
          @error="imgError(i)"
          mode="aspectFill"
          class="cover"
          :src="item.customCallout.customIcon"
        ></cover-image>
      </cover-view>
    </cover-view>
    
    //android
    <cover-view
      v-if="osName === 'android'"
      v-for="(item, i) in markers"
      :key="i"
      class="android-custom-callout"
      :class="chooseMarkerId === item.id ? 'choose' : ''"
      :marker-id="item.id"
    >
      <cover-image
        class="cover-bg"
        src="../../../static/images/temporarily/markerBg.png"
      ></cover-image>
      <cover-view class="callout-cover">
        <cover-image
          :webp="true"
          @load="load(i)"
          @error="imgError(i)"
          mode="aspectFill"
          class="cover"
          :src="item.customCallout.customIcon"
        ></cover-image>
      </cover-view>
    </cover-view>
  </template>
  <!-- 中心 -->
  <view class="center-point">
    <image class="center-img" :src="centerPoint"></image>
  </view>
</map>



//判断安卓,我采取异步延时添加。
if (osName.value === "android") {
  data.forEach((item, i) => {
    let newItem = {
      width: 1,
      height: 1,
      iconPath: markerbg,
      id: i,
      realId: item.id,
      latitude: item.latitude,
      longitude: item.longitude,
      customCallout: {
        display: "ALWAYS",
        anchorX: 0,
        anchorY: 51,
        customIcon: baseImgUrl + coverImg(item[coverImageKey]),
      },
    };
    setTimeout(() => {
      markers.value.push(newItem);
    }, 200 + i);
    return newItem;
  });

// 然后再调用图片的@load和@error事件,给marker数据新增一个属性,页面会更新,但是仍然有几率加载不出来。
function load(i) {
  if (i < markers.value.length) {
    markers.value[i].isload = true;
  }
}
function imgError(i) {
  if (i < markers.value.length) {
    markers.value[i].isload = true;
  }
}
相关推荐
郑州光合科技余经理14 小时前
本地生活系统:多业务订单字段怎么分账本导出
java·开发语言·前端·数据库·uni-app·php·ai编程
怕浪猫21 小时前
FDE 如何正确解决现场项目工程性的问题
前端·后端·ai编程
西瓜太郎123421 小时前
外层模型卡片与分组详情成功率不一致,应该怎么排查?
前端·typescript·go·软件工程·react
GreenTea21 小时前
vLLM 与 SGLang KV Cache 底层实现机制深度调研报告
前端·后端·算法
几何心凉1 天前
没有万能模型:我用 AiiOnly Token Plan,把多款大模型的长处拼进同一个项目
前端
kyriewen1 天前
Claude Code 的额度今天缩水了17%——官方公告上写的是"永久提高25%"
前端·ai编程·claude
雪芽蓝域zzs1 天前
vue解构平铺VS对象包裹
前端·javascript·vue.js
风骏时光牛马1 天前
从零到实战,完整AI学习路线
前端
三十而立洋1 天前
深入理解 Monorepo:子包安装依赖
前端·前端工程化
IT_陈寒1 天前
Java Stream处理大集合,我的内存怎么就炸了
前端·人工智能·后端