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;
  }
}
相关推荐
蓝倾4 分钟前
京东批量获取商品SKU操作指南
前端·后端·api
JSLove11 分钟前
常见 npm 报错问题
前端·npm
sunbyte11 分钟前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | ContentPlaceholder(背景占位)
前端·javascript·css·vue.js·tailwindcss
爱学习的茄子12 分钟前
React Hooks进阶:从0到1打造高性能Todo应用
前端·react.js·面试
知性的小mahua16 分钟前
vue3+canvas实现摄像头ROI区域标记
前端
嘗_32 分钟前
暑期前端训练day5
前端
uncleTom66638 分钟前
前端布局利器:rem 适配全面解析
前端
谦哥41 分钟前
Claude4免费Vibe Coding!目前比较好的Cursor替代方案
前端·javascript·claude
LEAFF1 小时前
如何 测试Labview是否返回数据 ?
前端
Spider_Man1 小时前
🚀 从阻塞到丝滑:React中DeepSeek LLM流式输出的实现秘籍
前端·react.js·llm