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;
  }
}
相关推荐
yantuguiguziPGJ2 分钟前
WPF 联合 Web 开发调试流程梳理(基于 Microsoft.Web.WebView2)
前端·microsoft·wpf
大飞记Python35 分钟前
部门管理|“编辑部门”功能实现(Django5零基础Web平台)
前端·数据库·python·django
tsumikistep2 小时前
【前端】前端运行环境的结构
前端
你的人类朋友2 小时前
【Node】认识multer库
前端·javascript·后端
Aitter2 小时前
PDF和Word文件转换为Markdown的技术实现
前端·ai编程
mapbar_front2 小时前
面试问题—上家公司的离职原因
前端·面试
昔人'3 小时前
css使用 :where() 来简化大型 CSS 选择器列表
前端·css
昔人'3 小时前
css `dorp-shadow`
前端·css
流***陌3 小时前
扭蛋机 Roll 福利房小程序前端功能设计:融合趣味互动与福利适配
前端·小程序
烛阴4 小时前
用 Python 揭秘 IP 地址背后的地理位置和信息
前端·python