当前位置: 首页 > news >正文

微信小程序 点击地图后弹出一个模态框

展示效果:

html:

<!-- WXML部分 -->
<view class="container"><map id="myMap" longitude="{{longitude}}" latitude="{{latitude}}" scale="{{scale}}" style="width: 100%; height: 100vh" bindtap="onMapTap"markers="{{markers}}"polyline="{{polyline}}"polygons="{{polygons}}"bindregionchange="onRegionChange"show-locationbindmarkertap="handleMarkerTap"bindlabeltap="handleMarkerTap"  ></map><!-- 底部模态框 --><view class="modal-overlay {{showModal ? 'active' : ''}}" bindtap="closeModal"></view><view class="modal-content {{showModal ? 'active' : ''}}"><view class="modal-header"><text class="modal-title">位置详情</text><text class="close-btn" bindtap="closeModal">×</text></view><view class="modal-body"><text class="location-name">{{selectedMarker ? selectedMarker.title : ''}}</text><text class="location-desc">这是一个示例位置描述,您可以在这里添加关于此位置的详细信息。</text><view class="location-info"><text class="info-item">地址: 示例街道123号</text><text class="info-item">电话: 123-456-7890</text><text class="info-item">营业时间: 09:00-18:00</text></view></view><view class="modal-footer"><button class="btn-secondary" bindtap="closeModal">取消</button><button class="btn-primary" bindtap="navigateToDetail">查看详情</button></view></view>
</view>

css:

/* WXSS部分 */
.container {display: flex;flex-direction: column;height: 100%;position: relative;
}/* 模态框遮罩层 */
.modal-overlay {position: fixed;top: 0;left: 0;width: 100%;height: 100%;background-color: rgba(0, 0, 0, 0.5);opacity: 0;visibility: hidden;transition: all 0.3s ease;z-index: 999;
}.modal-overlay.active {opacity: 1;visibility: visible;
}/* 模态框内容 */
.modal-content {position: fixed;bottom: 0;left: 0;width: 100%;background-color: #fff;border-radius: 20rpx 20rpx 0 0;transform: translateY(100%);transition: transform 0.3s ease;z-index: 1000;box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.1);
}.modal-content.active {transform: translateY(0);
}.modal-header {display: flex;justify-content: space-between;align-items: center;padding: 30rpx;border-bottom: 1rpx solid #f0f0f0;
}.modal-title {font-size: 36rpx;font-weight: bold;color: #333;
}.close-btn {font-size: 48rpx;color: #999;line-height: 1;
}.modal-body {padding: 30rpx;
}.location-name {display: block;font-size: 32rpx;font-weight: bold;color: #333;margin-bottom: 20rpx;
}.location-desc {display: block;font-size: 28rpx;color: #666;line-height: 1.5;margin-bottom: 30rpx;
}.location-info {display: flex;flex-direction: column;gap: 15rpx;
}.info-item {font-size: 26rpx;color: #888;
}.modal-footer {display: flex;justify-content: space-between;padding: 30rpx;gap: 20rpx;
}.btn-primary, .btn-secondary {flex: 1;padding: 20rpx;border-radius: 10rpx;font-size: 28rpx;border: none;
}.btn-primary {background-color: #07c160;color: white;
}.btn-secondary {background-color: #f0f0f0;color: #333;
}

js:

// JS部分
Page({data: {longitude: 116.397428, // 默认经度latitude: 39.90923,    // 默认纬度scale: 14,             // 地图缩放级别markers: [{id: 1,latitude: 39.90923,longitude: 116.397428,title: '天安门广场',iconPath: '/images/marker.png',width: 30,height: 30},{id: 2,latitude: 39.91423,longitude: 116.407428,title: '故宫博物院',iconPath: '/images/marker.png',width: 30,height: 30}],polyline: [],polygons: [],showModal: false,selectedMarker: null},onLoad: function() {// 页面加载时的初始化逻辑},onMapTap: function(e) {// 地图点击事件console.log('地图被点击', e);},onRegionChange: function(e) {// 区域变化事件console.log('地图区域变化', e);},// 处理标记点击事件handleMarkerTap: function(e) {const markerId = e.markerId;const marker = this.data.markers.find(m => m.id === markerId);if (marker) {this.setData({selectedMarker: marker,showModal: true});}},// 关闭模态框closeModal: function() {this.setData({showModal: false});},// 跳转到详情页面navigateToDetail: function() {// 先关闭模态框this.closeModal();// 跳转到详情页面wx.navigateTo({url: `/pages/locationDetail/locationDetail?id=${this.data.selectedMarker.id}`});}
});

http://www.dtcms.com/a/577661.html

相关文章:

  • 3.6.6【2021统考真题】
  • 《道德经》第五十章
  • 分类问题的基石:逻辑回归(Logistic Regression)
  • 机器学习实践项目(二)- 房价预测增强篇 - 特征工程二
  • Jenkins自动部署CI/CD
  • 【unity】PowerVR GE8320系列GPU渲染问题分析
  • 做网站设计需要哪些知识网页游戏排行榜回合制
  • 从理论到实践:深度解析昇腾CANN训练营中的Ascend C编程模型
  • Java TreeMap与HashTable深度解析:有序映射与线程安全映射
  • 什么是大数据,为什么它很重要?
  • asp网站配置伪静态做网站的
  • 顺序表vector--------杨辉三角
  • 阿里云 RDS PostgreSQL 可观测最佳实践
  • JVM堆的管理者——CodeCache
  • 目前哪个网站建设的最好wordpress 模板引入文件
  • Data+AI 时代,对象存储为 AI 应用注入全局动力
  • linux:io基础
  • WSL+openEuler云原生实践:Docker全流程部署与多容器编排深度评测
  • 个人笔记|单臂路由,子接口,VLAN标签
  • 罗湖商城网站设计推荐小程序服务开发公司
  • 赣州网站建设jx25网页开发用到的技术
  • 企业服务在产业平台领域的渗透率现状和发展未来
  • 【P27 回归算法及应用实践】有监督的机器学习、分类与回归、一元线性回归、最小二乘法、多元回归与梯度下降、学习率
  • Spring Boot 如何支持国际化
  • Excel斜线表头怎么做?合并单元格后添加对角线+两侧输入文字,新手也能秒会!
  • ara::core——Adaptive AUTOSAR
  • 大语言模型训推一体机:AI算力革命的“新引擎”,2031年市场规模突破123亿的黄金赛道
  • 百度网站降级的原因计算机一级考试网站怎么做
  • 复数的矩阵表示 | 欧拉恒等式的复数矩阵变换
  • Linux 系统调用在 ARM 上的实现与工作机制