微信小程序地图案例

微信小程序作为一种轻量级应用开发平台,广泛应用于各类服务场景中。今天我们将通过一个实际案例,演示如何在微信小程序中实现"查看附近美食餐厅"的功能。

项目概述
我们将开发一个微信小程序,用户可以通过该程序查看自己当前位置附近的美食餐厅,并在地图上显示这些餐厅的位置。点击地图上的标记,可以查看餐厅的详细信息。

功能需求 获取用户当前位置 显示带有当前定位的地图 查询并显示附近的美食餐厅 点击餐厅标记查看餐厅详情 开发步骤

  1. 初始化项目 首先,在微信开发者工具中创建一个新的小程序项目,并配置好基本文件结构:
  • pages/
    • index/
      • index.wxml
      • index.js
      • index.wxss
      • index.json
    • restaurant/
      • restaurant.wxml
      • restaurant.js
      • restaurant.wxss
      • restaurant.json
  • cloudfunctions/
    • getNearbyRestaurants/
      • index.js
      • package.json
  • app.json
  • app.js
  • app.wxss
  1. 配置路由 (app.json)
    在 app.json 中添加页面路径配置:

json

java 复制代码
{
  "pages": [
    "pages/index/index",
    "pages/restaurant/restaurant"
  ],
  "window": {
    "navigationBarBackgroundColor": "#ffffff",
    "navigationBarTextStyle": "black",
    "navigationBarTitleText": "附近美食",
    "backgroundColor": "#eeeeee",
    "backgroundTextStyle": "light"
  }
}
  1. 创建主页界面 (index.wxml)
    在 pages/index/index.wxml 中,我们将使用地图组件来展示地图,并添加一个按钮用于获取附近餐厅:

xml

html 复制代码
<view class="container">
  <map id="map" 
       longitude="{{longitude}}" 
       latitude="{{latitude}}" 
       show-location 
       markers="{{markers}}" 
       bindmarkertap="onMarkerTap"
       style="width: 100%; height: 500px;">
  </map>
  <button bindtap="getNearbyRestaurants">查看附近美食</button>
</view>
  1. 实现主页逻辑 (index.js)
    在 pages/index/index.js 中,我们需要实现获取当前位置和查询附近餐厅的功能:

javascript

javascript 复制代码
Page({
  data: {
    latitude: 0,
    longitude: 0,
    markers: []
  },
  
  onLoad: function() {
    this.getLocation();
  },
  
  getLocation: function() {
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        this.setData({
          latitude: res.latitude,
          longitude: res.longitude
        });
        this.getNearbyRestaurants(); // 自动加载附近餐厅
      },
      fail: () => {
        wx.showToast({
          title: '无法获取地理位置',
          icon: 'none'
        });
      }
    });
  },
  
  getNearbyRestaurants: function() {
    wx.cloud.callFunction({
      name: 'getNearbyRestaurants',
      data: {
        latitude: this.data.latitude,
        longitude: this.data.longitude
      },
      success: (res) => {
        const restaurants = res.result.data;
        const markers = restaurants.map((restaurant, index) => ({
          id: index,
          latitude: restaurant.location.latitude,
          longitude: restaurant.location.longitude,
          title: restaurant.name
        }));
        this.setData({
          markers: markers
        });
      },
      fail: (err) => {
        wx.showToast({
          title: '获取数据失败',
          icon: 'none'
        });
        console.error(err);
      }
    });
  },
  
  onMarkerTap: function(event) {
    const markerId = event.markerId;
    const marker = this.data.markers[markerId];
    wx.navigateTo({
      url: `/pages/restaurant/restaurant?name=${marker.title}`
    });
  }
});
  1. 详情页界面 (restaurant.wxml)
    在 pages/restaurant/restaurant.wxml 中展示餐厅详情:

xml

html 复制代码
<view class="container">
  <text>餐厅名称: {{name}}</text>
</view>
  1. 详情页逻辑 (restaurant.js)
    在 pages/restaurant/restaurant.js 中实现显示餐厅名称:

javascript

javascript 复制代码
Page({
  data: {
    name: ''
  },
  
  onLoad: function(options) {
    if (options.name) {
      this.setData({
        name: options.name
      });
    }
  }
});
  1. 设置样式 (index.wxss 和 restaurant.wxss)
    用简单的CSS样式美化页面:

index.wxss

css 复制代码
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

button {
  margin-top
[好像出错了,请稍后再试。]
相关推荐
_ZeroKing8 小时前
自制智能门锁:NFC 刷卡 + 小程序远程开锁(完整实战记录)
嵌入式硬件·小程序·notepad++·arduino
Code Slacker8 小时前
第八届传智杯AI微信小程序开发挑战赛练习题库(四)
notepad++
金山毒霸电脑医生1 天前
Notepad++下载安装教程(2026 最新版):安全下载、安装步骤及中文界面设置详解
文本编辑器·notepad++·windows软件·notepad++下载安装教程
沛沛老爹1 天前
跨平台Agent Skills开发实战:一次编写,多处运行的终极指南
notepad++
咖啡の猫1 天前
微信小程序案例 - 自定义 tabBar
微信小程序·小程序·notepad++
码农阿豪3 天前
轻断食14+7方案小程序项目 - 完整实现方案
小程序·notepad++
全栈小54 天前
【小程序】微信小程序input设置readonly只读属性无效,这是怎么回事呢
微信小程序·小程序·notepad++
ruxshui5 天前
Notepad++ 正则匹配实战:时间戳及高频场景
linux·服务器·notepad++
咖啡の猫7 天前
微信小程序页面事件
微信小程序·小程序·notepad++
咖啡の猫7 天前
微信小程序案例 - 本地生活(列表页面)
微信小程序·生活·notepad++