微信小程序 map地图(轨迹)

news/2024/7/20 2:44:39 标签: 微信小程序, 小程序

allMarkers效果图

 

废话少说直接上马(最后是我遇到的问题)

cover-view是气泡弹窗,可以自定义弹窗,要配合js:customCallout,如果是非自定义的话:callout(可以修改颜色、边框宽度、圆角、边距等)有些属性我没加,还是看官方文档(虽然写的。。)

<!--index.wxml-->
<view class="page-section page-section-gap">
  <map id="myMap" style="width: 100%; height: 450px;" latitude="{{latitude}}" longitude="{{longitude}}" bindmarkertap="markertap" bindcallouttap="callouttap" bindlabeltap="labeltap" markers="{{markers}}" scale="16" polyline="{{polyline}}">
    <cover-view slot="callout">
      <block wx:for="{{customCalloutMarkerIds}}" wx:key="*this">
        <cover-view class="customCallout" marker-id="{{item}}">
          <!-- <cover-image class="icon" src="../img/point.png"></cover-image> -->
          <cover-view class="content">
            <view style="background-color: #fff;">
              <view>设备名称</view>
              <view>设备名称</view>
              <view>设备名称</view>
            </view>
          </cover-view>
        </cover-view>
      </block>
    </cover-view>
  </map>
  <button bindtap="addMarker" class="page-body-button" type="primary">添加marker</button>
</view>

字段说明:

allMarkers:模拟位置点

markers:地图显示位置点

polyline:地图点连线位置点

mapCtx:初始化地图

 customCallout:气泡自定义使用(建议使用方法)

callout:气泡简易使用

const normalCallout = {
  id: 1,
  latitude: 23.098994,
  longitude: 113.322520,
  iconPath: '../img/point.png',
  callout: {
    content: '文本内容',
    color: '#ff0000',
    fontSize: 14,
    borderWidth: 2,
    borderRadius: 10,
    borderColor: '#000000',
    bgColor: '#fff',
    padding: 5,
    display: 'ALWAYS',
    textAlign: 'center'
  },
}

const customCallout1 = {
  id: 2,
  latitude: 23.097994,
  longitude: 113.323520,
  iconPath: '../img/point.png',
  callout: {
    content: '文本内容',
    color: '#ff0000',
    fontSize: 14,
    borderWidth: 2,
    borderRadius: 10,
    borderColor: '#000000',
    bgColor: '#fff',
    padding: 5,
    display: 'ALWAYS',
    textAlign: 'center'
  },
}

const customCallout2 = {
  id: 3,
  latitude: 23.096994,
  longitude: 113.324520,
  iconPath: '../img/point.png',
  customCallout: {
    anchorY: 10,
    anchorX: 0,
    display: 'ALWAYS',
  },
}

const customCallout3 = {
  id: 4,
  latitude: 23.095994,
  longitude: 113.325520,
  iconPath: '../img/point.png',
  customCallout: {
    anchorY: 0,
    anchorX: 20,
    display: 'ALWAYS',
  },
}

const allMarkers = [normalCallout, customCallout1, customCallout2, customCallout3]

Component({
  data: {
    mapCtx: '',
    latitude: 23.098994,
    longitude: 113.322520,
    customCalloutMarkerIds: '',
    markers: [],
    polyline: []
  },
  onReady: function (e) {
    // 使用 wx.createMapContext 获取 map 上下文
    this.mapCtx = wx.createMapContext('myMap')

  },
  methods: {
    addMarker() {
      var list = []
      var that = this
      for (const i in allMarkers) {
        var point = {
          latitude: allMarkers[i].latitude,
          longitude: allMarkers[i].longitude
        }
        list.push(point)
      }
      console.log(this.data.polyline.points)
      this.setData({
        polyline:[{
          points: list,
          color: '#58c16c',
          width: 6,
          borderColor: '#2f693c',
          borderWidth: 1,
          arrowLine: true
        }],
        markers: allMarkers,
        customCalloutMarkerIds: [2, 3, 4],
      })
    },

  },
})

xml和js直接复制就可以运行了没有其他配置

所遇到问题:

1.初始化时我在data直接给markers和polyline赋值了,导致不能显示气泡,解决是加了一个按钮等map初始化结束在添加点就正常了

2.marker-id必须是number

3.我没写,但是我感觉自定义气泡是可以加点击事件的


http://www.niftyadmin.cn/n/4925796.html

相关文章

【Express.js】集成Websocket

集成websocket 本节我们介绍在如何在 express 中集成 websocket。 WebSocket 服务器可以主动向客户端推送信息&#xff0c;客户端也可以主动向服务器发送信息&#xff0c;是真正的双向平等对话&#xff0c;属于服务器推送技术的一种。 准备工作 创建一个 express.js 项目&a…

50吨收费站生活一体化污水处理设备厂家价格低

50吨收费站生活一体化污水处理设备厂家价格低 设备工艺说明 污水处理设备主要用于生活污水和与之类似的工业有机废水的处理&#xff0c;其主要处理方法是采用目前较为成熟的生化处理技术—生物接触氧化&#xff0c;水质设计按一般生活污水水质设计计算&#xff0c;按BOD5平均20…

[QCM6125][Android13] 修复PRODUCT_COPY_FILES无法拷贝so

文章目录 开发平台基本信息问题描述解决方法 开发平台基本信息 芯片: QCM6125 版本: Android 13 kernel: msm-4.14 问题描述 在进行系统移植时&#xff0c;经常会把一些自己开发的c或者c程序编译成so库&#xff0c;然后在系统服务中去调用这些库。所以在进行新代码开发时&am…

【Mac】mac 系统下格式化U盘或移动硬盘为ext4格式

1. 打开终端&#xff0c;安装 homebrew /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"2. 安装之后再次运行此命令 /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"…

腾讯云COS的快速接入

背景 最近在研究一个剪贴板粘贴工具&#xff0c;实现粘贴图片&#xff0c;返回可访问的地址&#xff0c;这个在我的哔哩哔哩上有出一期视频&#x1f92d;。但是&#xff0c;我发现部分博客平台不能正常的转载我的图片链接&#xff0c;于是研究了一下腾讯云的COS&#xff08;阿…

一文了解Java序列化和反序列化:对象的存储与传输

一文了解Java序列化和反序列化&#xff1a;对象的存储与传输 作者&#xff1a;Stevedash 发布时间&#xff1a;2023年8月9日 21点30分 前言 Java序列化是一项强大而重要的技术&#xff0c;它允许我们将对象转换为字节流&#xff0c;以便在存储、传输和重建时使用。在本文中&…

SpringBoot 依赖管理

Spring Boot 依赖管理 1. 父项目做依赖管理 无需关注版本号&#xff0c;自动版本仲裁机制 <!-- 依赖管理 --> <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version&g…

【PCL-6】PCL基于凹凸型的分割算法(LCCP)

凹凸型分割算法适用于颜色类似、棱角分明的物体场景分割。LCCP方法不依赖点云颜色&#xff0c;只使用空间信息和法线信息。 算法流程&#xff1a; 1、基于超体聚类的过分割&#xff1b; 2、在超体聚类的基础上再聚类。 算法思路&#xff1a; 1、基于CC和SC判断凹凸性&…