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

展示效果:

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}`
    });
  }
});
相关推荐
fxss5 小时前
宝宝学识屋有小程序了
微信小程序
拖孩8 小时前
又又又用 AI 写了个小程序:我站起来猛蹬 AI,使劲薅羊毛
前端·后端·微信小程序
daguanren12 小时前
await nextTick() 之后,页面为什么还没变?读 weapp-vite / Wevu 的三段更新链路
微信小程序·vite
FX1317887KP_1 天前
KET/PET/FCE 口语答案说多长才合适?长度参考与展开方法
小程序·ket口语·pet口语·fce备考·剑桥英语
律宏阔1 天前
微信小程序 + Taro + TanStack Query 缓存与页面返回刷新踩坑实录
前端·微信小程序·taro
千寻技术帮1 天前
微信小程序+Spring Boot 烘焙商城系统(附完整源码+数据库+部署文档)50053
数据库·spring boot·微信小程序·烘焙商城
码云之上2 天前
微信小程序设备指纹实践,删了重装还能认出这台手机吗?
前端·安全·微信小程序
源码手记2 天前
基于微信小程序的相亲交友智能匹配系统-附源码
微信小程序·小程序·交友
源码手记2 天前
基于微信小程序的图书馆座位预约系统(程序+文档)
微信小程序·小程序
aimartec3 天前
企业小程序与APP定制源码买断工程实践:三层解耦架构、状态机防呆与平滑迁移清单
小程序·架构