uni-app中markers中iconPath如何使用网络地址

 官方文档提到可以使用相对路径和临时地址,那么我们就通过下载网络图片生成临时地址实现。


<template>
  <view class="map">
    <uni-map class="map" :latitude="latitude" :longitude="longitude" :markers="markers" />
  </view>
</template>

<script>
export default {
  data() {
    return {
      latitude: 30.660382,
      longitude: 104.065304,
      markers: [
        {
            latitude: 30.660382,
	        longitude: 104.065304,
			iconPath: ''
        },
      ],
    };
  },
  mounted() {
    this.loadImage(); // 加载网络图片的方法
  },
  methods: {
    loadImage() {
      uni.request({
        url: "https://a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker
-default.png", // 网络图片地址
        responseType: "arraybuffer", 
        success: (res) => {
          // 转换请求到的二进制数据为 base64 格式作为 iconPath
          const base64 = uni.arrayBufferToBase64(res.data);
          this.markers[0].iconPath = "data:image/png;base64," + base64;
        },
      });
    },
  },
};
</script>

<style>
.map {
  width: 750rpx;
  height: 100%;
}
</style>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值