这是本人毕业设计中其中一个功能点,就是用户在微信小程序(使用uniapp框架)中的运动模块中有个选项卡可以查询附近健身房。我尝试过使用腾讯地图、百度地图和高德地图的api来实现此功能,前两者(腾讯、百度)都以失败告终(无法显示出健身房位置标记)。
为了使读者们避免踩更多的坑以及记录一下自己怎么实现这个功能,我有必要总结一下。
上图,可以看到显示我当前位置的附近有哪些健身房,我自定义了最多显示10家,点击黄色标记后会变成红色,并且在底部显示健身房信息和导航按钮。
这是做以下步骤的前提条件,你需要获取Key和添加 js 文件
(1)引用高德地图微信小程序JSAPI模块
(2)获取自己当前位置
(3)使用myAmapFun.getPoiAround根据关键词获取周边信息
<template>
<view class="gymsMap">
<view class="map_container">
<map class="map" id="map" :longitude="longitude" :latitude="latitude" scale="14" :show-location="true" :markers="markers"
@markertap="makertap">map>
view>
<view class="map_bottom">
<view class="map_text">
<view class="row1">
<text class="h1">{{poisdatas.name}}text>
<view class="h2" v-if="poisdatas.name != null">距您 {{poisdatas.distance}} mview>
view>
<text>{{poisdatas.desc}}text>
view>
<view class="luxian" v-if="poisdatas.name != null" @click="daoGym">
<image src="../../static/daohang.png" mode="aspectFill">image>
<view class="daohang">导航view>
view>
view>
view>
template>
<script>
// 引用高德地图微信小程序JSAPI模块
var amapFile = require('../../common/amap-wx.130.js');
var markersData = [];
export default {
data() {
return {
markers: [],
poisdatas: {},
latitude: '',
longitude: '',
address: '',
name: ''
}
},
onLoad() {
var _self = this;
// 获取位置
uni.getLocation({
type: 'gcj02',
success: function(res) {
_self.longitude = res.longitude;
_self.latitude = res.latitude;
}
});
// 高德地图的key
var myAmapFun = new amapFile.AMapWX({
key: '你自己获取的Key'
});
// 根据关键词获取周边信息
myAmapFun.getPoiAround({
iconPathSelected: '/static/marker_red.png',
iconPath: '/static/marker_yellow.png',
querykeywords: '健身',
// 看高德地图POI分类编码 https://lbs.amap.com/api/javascript-api/download/
querytypes: '080111',
offset: 10,
success: function(data) {
// 标记点 https://developers.weixin.qq.com/miniprogram/dev/component/map.html#map
_self.markers = data.markers;
// 信息点数据 https://lbs.amap.com/api/webservice/guide/api/search/#around
_self.poisdatas = data.poisData;
var markers_new = [];
_self.markers.forEach(function(item, index) {
markers_new.push({
id: item.id,
width: item.width,
height: item.height,
iconPath: item.iconPath,
latitude: item.latitude,
longitude: item.longitude,
address: item.address,
name: item.name,
distance: _self.poisdatas[index].distance,
//自定义标记点上方的气泡窗口
callout: {
padding: 2,
fontSize: 15,
bgColor: '#199d33',
color: '#ffffff',
borderRadius: 5,
display: 'ALWAYS',
content: _self.poisdatas[index].name
}
})
})
_self.markers = markers_new;
},
fail: function(info) {
//失败回调
console.log("info", info)
}
})
},
methods: {
// 点击标记点
makertap(e) {
var id = e.detail.markerId;
this.showMarkerInfo(this.markers, id);
this.changeMarkerColor(this.markers, id);
},
// 展示标记点信息
showMarkerInfo(data, i) {
this.poisdatas = {
name: data[i].name,
desc: data[i].address,
distance: data[i].distance
}
},
// 改变标记点颜色
changeMarkerColor(data, i) {
var markers = [];
for (var j = 0; j < data.length; j++) {
if (j == i) {
data[j].iconPath = "/static/marker_red.png"; //如:../../img/marker_checked.png
} else {
data[j].iconPath = "/static/marker_yellow.png"; //如:../../img/marker.png
}
markers.push({
id: data[j].id,
latitude: data[j].latitude,
longitude: data[j].longitude,
iconPath: data[j].iconPath,
width: data[j].width,
height: data[j].height,
address: data[j].address,
name: data[j].name,
distance: data[j].distance,
callout: {
padding: 2,
fontSize: 15,
bgColor: '#199d33',
color: '#ffffff',
borderRadius: 5,
display: 'ALWAYS',
content: data[j].name
}
})
}
this.address = data[i].address;
this.name = data[i].name;
this.markers = markers;
},
// 打开微信小程序内置导航界面
daoGym() {
uni.openLocation({
latitude: this.latitude,
longitude: this.longitude,
name: this.name,
address: this.address
});
}
}
}
script>
<style lang="scss" scoped>
.gymsMap {
.map_container {
position: absolute;
top: 0;
bottom: 90px;
left: 0;
right: 0;
.map {
width: 100%;
height: 100%;
}
}
.map_bottom {
height: 90rpx;
.map_text {
position: absolute;
left: 0;
right: 0;
bottom: 0px;
margin: 20rpx 0;
background: #fff;
padding: 0 15px;
width: 600rpx;
.row1 {
display: flex;
align-items: center;
text {
margin: 5px 0;
display: block;
font-size: 16px;
display: -webkit-box;
-webkit-line-clamp: 1;
-webkit-box-orient: vertical;
overflow: hidden;
}
.h1 {
margin: 15px 0;
font-size: 18px;
}
.h2 {
margin-left: 15rpx;
font-size: 14px;
color: grey;
}
}
}
.luxian {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
position: absolute;
right: 0;
bottom: 5px;
margin: 20rpx 45rpx;
image {
margin-top: 30rpx;
width: 65rpx;
height: 65rpx;
}
.daohang {
font-size: 32rpx;
letter-spacing: 2rpx;
margin-top: 10rpx;
}
}
}
}
style>
总结一下后,发现这个并不太难实现,只是当时自己不知道如何将那些标记点可视化出来,之前看过一篇文章忘了点收藏,他的思路就是上面那样回调成功后forEach添加markers,是那篇文章才让我实现了这个功能,在此感谢他!!!
uniapp小程序查询附近健身房