作者: hust_a 发布时间: 已于 2026-04-16 16:38:52 修改
来源: https://blog.csdn.net/hust_a/article/details/160208822
不管是腾讯地图还是高德地图,现在的地图app现在做的都很复杂,地点搜索,路径规划都需要分开一步步操作,即先要搜索你需要的地点位置,查看位置,点击搜索,再跳出来路线图,操作很是繁琐。查找一些公共设施是我们生活中必不可少的,特别是到一些模式的地方,如果有一个能够快速展示这些地点并且在地图标记出来,展示路线的工具对生活来说非常方便。下面是我做的小程序的演示视频。
📹 嵌入式视频: https://live.csdn.net/v/embed/521996
周边搜索演示视频

本项目是一款基于微信小程序平台开发的「附近查找」应用,旨在帮助用户快速定位周边的公共设施和生活服务场所。通过集成高德地图和腾讯地图的API服务,实现了精准的位置定位、周边搜索和路线规划功能。
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ 视图层 (WXML) │ │ 逻辑层 (JS) │ │ 数据层 │
├─────────────────┤ ├─────────────────┤ ├─────────────────┤
│ index.wxml │ │ index.js │ │ app.globalData │
│ setting.wxml │ │ setting.js │ │ wx.setStorage │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │ │
▼ ▼ ▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ 样式层 (WXSS) │ │ 配置层 │ │ 第三方服务 │
├─────────────────┤ ├─────────────────┤ ├─────────────────┤
│ index.wxss │ │ config.js │ │ 高德地图API │
│ setting.wxss │ │ app.json │ │ 腾讯地图API │
└─────────────────┘ └─────────────────┘ └─────────────────┘
文件路径:app.js:1-113
该模块实现了地图API调用的智能管理,主要功能包括:
文件路径:pages/index/index.js:1-681
该模块实现了核心的周边搜索功能,主要包括:
文件路径:pages/index/index.js:150-180
实现了收藏功能的完整生命周期管理:
toggleFavorite(e) {
const item = e.target.dataset.item;
const key = this.getKey(item); // 生成唯一标识
const idx = favorites.findIndex(f => f.key === key);
if (idx !== -1) {
// 取消收藏
favorites.splice(idx, 1);
} else {
// 添加收藏(最多30条)
if (favorites.length >= 30) {
wx.showToast({ title: '最多收藏30个', icon: 'none' });
return;
}
favorites.push({ ...item, key });
}
app.saveFavorites(favorites); // 全局+本地存储
}
wx.getLocation({
type: 'gcj02', // 国测局坐标系
success: (res) => {
this.setData({
latitude: res.latitude,
longitude: res.longitude
});
this.getAddress(res.latitude, res.longitude);
}
});
采用高德地图API进行逆地理编码,优先使用POI名称作为地址描述:
nixiangWithGaode(lat, lon) {
wx.request({
url: config.GAPDE_MAP_LOCAL_URL,
data: {
key: config.GAODE_MAP_KEY,
location: lon + ',' + lat
},
success: (geoRes) => {
const regeocodeData = geoRes.data.regeocode;
let desc = regeocodeData.roads[0]?.name;
// 优先使用POI名称
if (regeocodeData.pois && regeocodeData.pois[0]) {
const poi = regeocodeData.pois[0];
desc = poi.name;
}
this.setData({ address: desc });
}
});
}
markers.push({
id: index,
latitude: parseFloat(lat),
longitude: parseFloat(lng),
iconPath: "/images/" + curType + "_mark.svg",
width: 30,
height: 30,
name: item.name,
distance: item.distance + "m",
rating: rating,
opentime: opentime,
addr: item.address || "暂无地址",
callout: {
content: item.distance + "m",
borderRadius: "30rpx",
fontSize: 14,
padding: 8,
display: "ALWAYS",
color: "#333",
bgColor: "#ffffff95"
}
});
showAllMarkers() {
const mapCtx = wx.createMapContext('map');
const points = [
...markers,
{ latitude, longitude } // 当前位置
];
mapCtx.includePoints({
points: points,
padding: [36, 20, 360, 20] // 上、右、下、左
});
}
wx.request({
url: config.WX_MAP_ROUTE_WALK_URL,
data: {
from: `${startLat},${startLng}`,
to: `${endLat},${endLng}`,
key: config.WX_MAP_KEY
},
success: res => {
// 解析路线数据并绘制
}
});
if (this.data.curType == "charge") {
driveurl = config.WX_MAP_ROUTE_DRIVE_URL;
}
this.setData({
polyline: [{
points: pl, // 路线坐标点
color: this.data.brandIcon.active_color,
width: 6,
arrowLine: true,
level: 'abovebuildings',
segmentTexts: [{
name: walkname,
startIndex: 0,
endIndex: pl.length - 1
}]
}]
});
{
"permission": {
"scope.userLocation": {
"desc": "需要获取你的位置以展示附近信息"
}
},
"requiredBackgroundModes": ["location"],
"requiredPrivateInfos": ["getLocation"]
}
解决方案:
解决方案:
解决方案:
优化方案:
优化方案: