小程序map(地图)组件

文档

wxml

<!--index.wxml-->
<view class="map-container">
  <map
    id="map"
    class="map"
    longitude="{{longitude}}"
    latitude="{{latitude}}"
    scale="{{scale}}"
    markers="{{markers}}"
    polyline="{{polyline}}"
    circles="{{circles}}"
    controls="{{controls}}"
    show-location="{{true}}"
    enable-overlooking="{{true}}"
    show-compass="{{true}}"
    enable-rotate="{{true}}"
    enable-traffic="{{true}}"
    enable-building="{{true}}"
    >
    <!-- 控制层 -->
    <view class="controls-btn">
      <view class="btn">+</view>
      <view class="btn">-</view>
    </view>
    </map>
</view>

wxss

/**index.wxss**/
.map {
  width: 100vw;
  height: 100vh;
  position: relative;
}
.controls-btn {
  position: absolute;
  top: 0;
  right: 0;
  display: flex;
}
.btn {
  padding: 10rpx;
  font-size: 40rpx;
}

js

const app = getApp()

Page({
  data: {
    longitude: 108.971553,
    latitude: 34.35888,
    longitude1: 108.977103,
    latitude1: 34.35948,
    markers: [], // 标记点
    scale: 16, // 缩放级别
    polyline: [], // 路线
    circles: [], // 圆,范围
    controls: [], // 控制层
    includePoints: [], // 缩放视野展示所有经纬度
  },
  onLoad() {
    const mapCtx = wx.createMapContext("map", this);
    console.log(mapCtx);
    const { latitude, longitude, latitude1, longitude1 } = this.data;
    // 标记点
    const markers = [{
      id: "1",
      latitude,
      longitude,
      width: 30,
      height: 30,
      joinCluster: true,
      // iconPath: "/assests/imgs/my.png",
      iconPath: 'https://img-crs.vchangyi.com//standard/order-map-user.png',
      rotate: 15,
      alpha: 0.5,
      label: {
        content: '标记点',
        color: '#f00',
        fontSize: 12,
        borderRadius: 2,
        padding: 4,
        bgColor: 'transparent'
      },
      // title: "地图标记点",
      callout: {
        content: '取货点',
        color: '#666',
        fontSize: 12,
        borderRadius: 6,
        padding: 8,
        display: "ALWAYS"
      }
    },
    {
      id: "1",
      latitude: latitude1,
      longitude: longitude1,
      width: 30,
      height: 30,
      joinCluster: true,
      // iconPath: "/assests/imgs/my.png",
      iconPath: 'https://img-crs.vchangyi.com/standard/order-driver-icon-1.png',
      rotate: 15,
      alpha: 0.5,
      // title: "地图标记点",
      callout: {
        content: '骑手',
        color: '#666',
        fontSize: 12,
        borderRadius: 6,
        padding: 8,
        display: "ALWAYS"
      }
    }];
    // 路线
    const polyline = [{
      points: [{ latitude, longitude }, { latitude: latitude1, longitude: longitude1 }],
      colorList: ['#f00'],
      width: 1,
      dottedLine: false,
      arrowLine: false,
      level: "abovelabels"
    }];
    // 圆,范围
    const circles = [{
      latitude,
      longitude,
      color: '#fff',
      fillColor: '#7cb5ec88',
      radius: 100,
      strokeWidth: 1,
      level: 'abovelabels'
    }];
    // 控制层
    const controls = [{
      id: 11,
      iconPath: 'https://img-crs.vchangyi.com//standard/order-map-user.png',
      position: {
        left: 120,
        top: 50,
        width: 50,
        height: 50
      },
      clickable: true
    }];
    // 缩放视野展示所有经纬度,确保显示所有点
    mapCtx.includePoints({
      points: [{ latitude, longitude }, { latitude: latitude1, longitude: longitude1 }],
      padding: [100, 100, 100, 100],
      success: function (e) {
        console.log(e)
      }
    });
    this.setData({
      markers,
      polyline,
      circles,
      controls
    })
  }
})

效果图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值