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

展示效果:

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-location
    bindmarkertap="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}`
    });
  }
});
相关推荐
云起SAAS2 小时前
患者随访管理抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·患者随访管理
2501_916008892 小时前
HTTPS 请求抓包,从原理到落地排查的工程化指南(Charles / tcpdump / Wireshark / Sniffmaster)
ios·小程序·https·uni-app·wireshark·iphone·tcpdump
金梦人生2 小时前
UniApp + Vue3 + TS 工程化实战笔记
前端·微信小程序
2501_9159090615 小时前
WebView 调试工具全解析,解决“看不见的移动端问题”
android·ios·小程序·https·uni-app·iphone·webview
说私域16 小时前
“开源链动2+1模式AI智能名片S2B2C商城小程序”在拉群营销中的应用与效果
人工智能·小程序
2501_9151063217 小时前
App 怎么上架 iOS?从准备资料到开心上架(Appuploader)免 Mac 上传的完整实战流程指南
android·macos·ios·小程序·uni-app·iphone·webview
环信即时通讯云19 小时前
实现小程序 uniApp 输入框展示自定义表情包
小程序·uni-app
特严赤傲20 小时前
uniappx 开发微信小程序 腾讯地图偏移量计算
微信小程序·uts·uniappx·地图偏移量
2501_9159214321 小时前
iOS 抓不到包怎么办?工程化排查与替代抓包方案(抓包/HTTPS/Charles代理/tcpdump)
android·ios·小程序·https·uni-app·iphone·tcpdump