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;
  }
}
相关推荐
胡gh2 小时前
页面卡成PPT?重排重绘惹的祸!依旧性能优化
前端·javascript·面试
言兴2 小时前
# 深度解析 ECharts:从零到一构建企业级数据可视化看板
前端·javascript·面试
山有木兮木有枝_2 小时前
TailWind CSS
前端·css·postcss
烛阴3 小时前
TypeScript 的“读心术”:让类型在代码中“流动”起来
前端·javascript·typescript
杨荧3 小时前
基于Python的农作物病虫害防治网站 Python+Django+Vue.js
大数据·前端·vue.js·爬虫·python
Moment4 小时前
毕业一年了,分享一下我的四个开源项目!😊😊😊
前端·后端·开源
程序视点5 小时前
Escrcpy 3.0投屏控制软件使用教程:无线/有线连接+虚拟显示功能详解
前端·后端
silent_missile5 小时前
element-plus穿梭框transfer的调整
前端·javascript·vue.js
专注VB编程开发20年5 小时前
OpenXml、NPOI、EPPlus、Spire.Office组件对EXCEL ole对象附件的支持
前端·.net·excel·spire.office·npoi·openxml·spire.excel
古蓬莱掌管玉米的神5 小时前
coze娱乐ai换脸
前端