地理位置和地图功能在小程序开发中被广泛应用,例如外卖配送、打车服务、旅游导航、社交签到等。微信小程序提供了 wx.getLocation 和 wx.openLocation 等 API 获取用户位置,并支持 腾讯地图 或 高德地图 进行地图显示和交互。本文将详细介绍 小程序地理位置的获取、地图组件的使用、标记点和路线规划的实现,并结合示例代码进行分析。
微信提供了 wx.getLocation
API 供开发者获取用户当前的 经纬度、速度 和 精确度。
使用 wx.openLocation
API 可直接调用 微信内置地图,显示特定位置。
在调用 wx.getLocation
之前,必须在 app.json 中添加权限:
{
"permission": {
"scope.userLocation": {
"desc": "用于显示您的当前位置"
}
}
}
示例代码:
wx.getLocation({
type: 'wgs84', // wgs84 返回 GPS 坐标,gcj02 返回国测局坐标
success(res) {
console.log('经纬度:', res.latitude, res.longitude);
wx.showToast({ title: '获取成功', icon: 'success' });
},
fail(err) {
console.error('获取位置失败:', err);
wx.showToast({ title: '获取失败', icon: 'error' });
}
});
如果希望直接跳转到微信地图,可使用 wx.openLocation
:
wx.openLocation({
latitude: 39.9088, // 目标地点纬度(例如天安门广场)
longitude: 116.3975, // 目标地点经度
scale: 15,
name: "天安门广场",
address: "北京市东城区长安街"
});
map
小程序提供了 map
组件,可用于地图展示、标记点、路线规划等功能。以下是一个基本示例:
3.1 在 wxml
文件中使用 map
组件
<map
id="map"
longitude="{{longitude}}"
latitude="{{latitude}}"
scale="15"
markers="{{markers}}"
style="width: 100%; height: 500px;">
</map>
3.2 在 js
文件中设置数据
Page({
data: {
latitude: 39.9088,
longitude: 116.3975,
markers: [
{
id: 1,
latitude: 39.9088,
longitude: 116.3975,
title: "天安门广场",
iconPath: "/images/location.png", // 自定义图标
width: 30,
height: 30
}
]
}
});
地图标记(Markers)用于在地图上标注特定位置,例如商店、公交站点等。
示例:添加多个标记点
Page({
data: {
latitude: 39.9,
longitude: 116.4,
markers: [
{
id: 1,
latitude: 39.9088,
longitude: 116.3975,
title: "天安门广场",
iconPath: "/images/marker.png",
width: 30,
height: 30
},
{
id: 2,
latitude: 39.915,
longitude: 116.4039,
title: "故宫博物院",
iconPath: "/images/marker.png",
width: 30,
height: 30
}
]
}
});
5.1 使用 polyline
显示路线 polyline
可用于在地图上绘制两点之间的路线。
<map
longitude="{{longitude}}"
latitude="{{latitude}}"
scale="12"
polyline="{{polyline}}"
style="width: 100%; height: 500px;">
</map>
Page({
data: {
latitude: 39.9,
longitude: 116.4,
polyline: [{
points: [
{ latitude: 39.9, longitude: 116.4 },
{ latitude: 39.92, longitude: 116.42 }
],
color: "#FF0000DD",
width: 5
}]
}
});
如果需要更精准的导航和路线规划,可以调用 腾讯地图 API 获取行驶路线。
示例(后端调用腾讯地图 API 获取路线):
const axios = require('axios');
const key = "你的腾讯地图 API Key";
async function getRoute(start, end) {
const url = `https://apis.map.qq.com/ws/direction/v1/driving/?from=${start}&to=${end}&key=${key}`;
const res = await axios.get(url);
return res.data;
}
前端拿到路线后可以通过 polyline
绘制路径。
wx.getSetting
预检查用户是否授予了位置权限:
wx.getSetting({ success(res) { if (!res.authSetting['scope.userLocation']) { wx.authorize({ scope: 'scope.userLocation', success() { console.log("授权成功"); } }); } } });
wx.getLocation
每次调用都需要访问 GPS,建议缓存最近一次的位置信息:
const location = wx.getStorageSync("lastLocation"); if (location) { this.setData({ latitude: location.latitude, longitude: location.longitude }); } else { wx.getLocation({ success: res => wx.setStorageSync("lastLocation", res) }); }
scale
过大会导致视野狭窄,建议设置 scale=14~16
。
本文介绍了小程序的地理位置与地图功能,包括:
wx.getLocation
)wx.openLocation
)map
组件显示地图原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。