微信小程序地图

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

项目概述

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

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

初始化项目 首先,在微信开发者工具中创建一个新的小程序项目,并配置好基本文件结构:

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

配置路由 (app.json)

在 app.json 中添加页面路径配置:

json

复制代码
{
  "pages": [
    "pages/index/index",
    "pages/restaurant/restaurant"
  ],
  "window": {
    "navigationBarBackgroundColor": "#ffffff",
    "navigationBarTextStyle": "black",
    "navigationBarTitleText": "附近美食",
    "backgroundColor": "#eeeeee",
    "backgroundTextStyle": "light"
  }
}

3.创建主页界面 (index.wxml)

在 pages/index/index.wxml 中,我们将使用地图组件来展示地图,并添加一个按钮用于获取附近餐厅:

wxml

复制代码
<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>

4.实现主页逻辑 (index.js)

在 pages/index/index.js 中,我们需要实现获取当前位置和查询附近餐厅的功能:

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}`
    });
  }
});

5.详情页界面 (restaurant.wxml)

在 pages/restaurant/restaurant.wxml 中展示餐厅详情:

wxml

复制代码
<view class="container">
  <text>餐厅名称: {{name}}</text>
</view>

6.详情页逻辑 (restaurant.js)

在 pages/restaurant/restaurant.js 中实现显示餐厅名称:

javascript

复制代码
Page({
  data: {
    name: ''
  },
  
  onLoad: function(options) {
    if (options.name) {
      this.setData({
        name: options.name
      });
    }
  }
});

7.设置样式 (index.wxss 和 restaurant.wxss)

用简单的CSS样式美化页面:

index.wxss

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

button {
  margin-top
相关推荐
三天不学习5 小时前
UniApp三端实时通信实战:SignalR在H5、APP、小程序的差异与实现
微信小程序·uni-app·signalr
qq_12498707535 小时前
基于springboot归家租房小程序的设计与实现(源码+论文+部署+安装)
java·大数据·spring boot·后端·小程序·毕业设计·计算机毕业设计
说私域6 小时前
留量为王,服务制胜:开源链动2+1模式、AI智能名片与S2B2C商城小程序的协同创新路径
人工智能·小程序·开源
游戏开发爱好者86 小时前
如何使用 AppUploader 提交上传 iOS 应用
android·ios·小程序·https·uni-app·iphone·webview
百锦再6 小时前
AI视频生成模型从无到有:构建、实现与调试完全指南
人工智能·python·ai·小程序·aigc·音视频·notepad++
恩创软件开发20 小时前
创业日常2026-1-8
java·经验分享·微信小程序·小程序
腾讯云云开发1 天前
微信发布AI小程序成长计划:免费云开发资源+1亿token额度!
微信小程序·ai编程·小程序·云开发
开发加微信:hedian1161 天前
推客与分销场景下的系统架构实践:如何支撑高并发与复杂业务规则
小程序
游九尘1 天前
在小程序中实现横竖屏切换的配置方法,实时监听页面宽度
小程序
weixin_lynhgworld1 天前
[特殊字符]旧物焕新颜,二手变宝藏——小程序系统开发开启绿色生活新篇章[特殊字符]
小程序·生活