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;
  }
}
相关推荐
沐言人生3 小时前
自媒体人的效率神器!监控、采集、下载、发布,这个免费开源工具全拿下!
前端·vue.js·后端
excel4 小时前
为什么 super 中能接收到 resolve 和 reject?
前端
Flynt6 小时前
AI 生成的界面,能放心点吗?我把 Google A2UI 的三层防线拆开测了一遍
前端·google·agent
前端混子7 小时前
我用 AI 做了个「禾月小历」,一套代码跑微信小程序和 H5
前端
IT_陈寒7 小时前
Java实战技巧总结:当你的finally块没有执行时会发生什么?
前端·人工智能·后端
小帅不太帅8 小时前
一年一次黑五,为了买软件真省钱,我做了个开源小站
前端·人工智能·github
JavaGuide9 小时前
刚刚,阿里版 Codex 上线了匿名模型 HungerG,无限 Token!
前端·后端
zhanlang021710 小时前
HTML 零基础入门到企业实战全套教程【第九章 9.3】移动端 HTML 基础适配入门
前端·html
用户693717500138410 小时前
AI 帮我写了大半年的代码,我却越来越不会写代码了
前端·后端·github
skywalk816310 小时前
把「假成功」赶出工具层:lightharness 两天升级复盘(4×5 模式矩阵 + 工具层 5 个缺陷)
开发语言·前端·人工智能·lightharness