微信小程序----map组件实现检索【定位位置】周边的POI
效果图
实现方法
- 地图采用微信小程序提供的map组件;
- 周边的数据坐标点通过高德地图提供的API接口,获取定位位置的周边或者指定位置周边的数据。
WXML
<view class="map_container">
<view class="map-tab-bar">
<view class="map-tab-li {{item.id == status ? 'active' : ''}}" bindtap="getType" data-type="{{item.id}}" wx:key="aroundListId" wx:for="{{aroundList}}">{{item.name}}</view>
</view>
<map class="map" longitude="{{longitude}}" latitude="{{latitude}}" include-points="{{points}}" markers='{{markers}}'></map>
<view class="map-tab-bar map-foot {{isShow ? '' : 'map-hide'}}">
<view class="map-name">{{name}}</view>
<view class="map-address">{{address}}</view>
</view>
</view>
WXSS
.map_container{
width: 100%;
height: 100%;
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.map{
width: 100%;
height: 100%;
}
.map-tab-bar{
position: absolute;
top: 0;
left: 0;
width: 100%;
z-index: 1000;
font-size: 0;
background-color: #fff;
}
.map-hide{display: none;}
.map-foot{
top: auto;
bottom: 0;
padding: 0 10px;
}
.map-name{
height: 80rpx;
line-height: 80rpx;
font-size: 35rpx;
overflow: hidden;
}
.map-address{
height: 60rpx;
line-height: 60rpx;
font-size: 25rpx;
overflow: hidden;
}
.map-tab-li{
display: inline-block;
width: 25%;
overflow: hidden;
height: 70rpx;
line-height: 70rpx;
text-align: center;
font-size: 30rpx;
color: #333;
}
.map-tab-li.active{color: #fff;background-color: lightgreen;border-radius: 5px;}
JS
var app = getApp();
var amap = app.data.amap;
var key = app.data.key;
Page({
data: {
aroundList: [
{
name: '汽车服务',
id: '010000'
},
{
name: '汽车销售',
id: '020000'
},
{
name: '汽车维修',
id: '030000'
},
{
name: '摩托车',
id: '040000'
},
{
name: '餐饮',
id: '050000'
},
{
name: '购物',
id: '060000'
},
{
name: '生活',
id: '070000'
},
{
name: '体育休闲',
id: '080000'
},
{
name: '医疗保健',
id: '090000'
},
{
name: '住宿',
id: '100000'
},
{
name: '风景名胜',
id: '110000'
},
{
name: '商务住宅',
id: '120000'
}
],
status:null,
latitude: null,
longitude: null,
isShow: false,
markers: [],
points: [],
location: '',
name:'',
address: ''
},
onLoad: function () {
// 页面加载获取当前定位位置为地图的中心坐标
var _this = this;
wx.getLocation({
success(data) {
if (data) {
_this.setData({
latitude: data.latitude,
longitude: data.longitude,
markers:[{
id:0,
latitude: data.latitude,
longitude: data.longitude,
iconPath: '../../src/images/ding.png',
width: 32,
height: 32
}]
});
}
}
});
},
getType(e) {//获取选择的附近关键词,同时更新状态
this.setData({ status: e.currentTarget.dataset.type})
this.getAround(e.currentTarget.dataset.keywords,e.currentTarget.dataset.type);
},
getAround(keywords,types) {//通过关键词获取附近的点,只取前十个,同时保证十个点在地图中显示
var _this = this;
var myAmap = new amap.AMapWX({ key: key });
myAmap.getPoiAround({
iconPath: '../../src/images/blue.png',
iconPathSelected: '../../src/images/ding.png',
querykeywords: keywords,
querytypes: types,
location: _this.data.location,
success(data) {
if (data.markers) {
var markers = [], points = [];
for (var value of data.markers) {
if (value.id > 9) break;
if(value.id == 0){
_this.setData({
name: value.name,
address: value.address,
isShow: true
})
}
markers.push({
id: value.id,
latitude: value.latitude,
longitude: value.longitude,
title: value.name,
iconPath: value.iconPath,
width: 32,
height: 32,
anchor: { x: .5, y: 1 },
label: {
content: value.name,
color: 'green',
fontSize: 12,
borderRadius: 5,
bgColor: '#fff',
padding: 3,
x: 0,
y: -50,
textAlign: 'center'
}
});
points.push({
latitude: value.latitude,
longitude: value.longitude
})
}
_this.setData({
markers: markers,
points: points
})
}
},
fail: function (info) {
wx.showToast({title: info})
}
})
}
});
总结
- 由于是移动端,所以人为限制只显示了9条周边数据,防止重叠部分太多。
- 添加指定位置的周边的方法—-添加一个input,将给的关键字进行搜索,然后返回坐标,改变地图中心坐标。
- 改变中心坐标还有采用微信小程序自己的API(wx.chooseLocation),改变地图中心坐标。参考:微信小程序—-map路线规划
- 高德地图提供API和微信小程序提供API的优劣:1、目前高德提供的API返回数据很快,最少目前比微信小程序自己的快很多;2、缺点也很明显就是由于是外部提供的,所以需要进行对应配置,麻烦;3、微信小程序提供的API优势就是属于本身,不用额外配置,如果以后优化了,更好。
实例:
用高德地图提供的 getInputtips 接口,搜索关键字和城市,返回的坐标,然后改变地图中心坐标。
// 页面加载以输入地址为地图的中心坐标
// 假如输入的是:成都 欧尚庭院
myAmap.getInputtips({
keywords: '欧尚庭院',
city: '成都',
success(res) {
var tip = res.tips[0];
var lo = tip.location.split(',')[0];
var la = tip.location.split(',')[1]; _this.setData({
latitude: la,
longitude: lo,
location: tip.location,
markers: [{
id: 0,
latitude: la,
longitude: lo,
iconPath: '../../src/images/ding.png',
width: 32,
height: 32
}]
})
}
})
微信小程序----map组件实现检索【定位位置】周边的POI的更多相关文章
- 微信小程序map组件z-index的层级问题
说起微信小程序的map组件,可以说是良心之作了,一个组件解决了所以接入地图的所有麻烦,但是在实际小程序的试用过程中还是存在点问题的.如下情景:刚开始接入map组件的时候是在微信开发工具的模拟器上预览的 ...
- 微信小程序~map组件z-index无效
因项目需要,以map为背景,上面悬浮有其他组件.微信开发者工具测试时一切正常,但是真机测试时地图组件却把所有的组件覆盖,检查z-index设置,一切正常,地图组件层级也在这些组件的下面,为什么会被覆盖 ...
- 微信小程序Map组件踩坑日记
刚刚又发生一个bug,搞得我头皮发麻,本来该美滋滋的回家准备度过愉快的周末,瞬间变成了日常修bug,来,开始填坑之路 情景再现: 首先说一说我们项目的需求, 点击下方,弹出抽屉 点击对应的地图打开相应 ...
- 关于微信小程序 textarea组件在fixed定位的模块中随页面移动问题
具体的情况: 在模拟器中没问题,可是在真机下就出现以下问题, <textarea />在一个view盒子中,view盒子是固定定位,页面滑动时候,固定定位的盒子会定在屏幕的相对位置,但 ...
- 微信小程序textarea组件在fixed定位中随页面滚动
如果 textarea 是在一个 position:fixed 的区域,需要显示指定属性 fixed 为 true https://developers.weixin.qq.com/miniprogr ...
- 微信小程序的组件总结
本文介绍微信小程序的组件 视图容器 基础内容 表单组件 导航组件 媒体组件 视图容器 view 布局容器 <view hover-class='bg'>222</view> 可 ...
- Wuss Weapp 一款高质量,组件齐全,高自定义的微信小程序 UI 组件库
Wuss Weapp 一款高质量,组件齐全,高自定义的微信小程序 UI 组件库 文档 https://phonycode.github.io/wuss-weapp 扫码体验 使用微信扫一扫体验小程序组 ...
- Wuss Weapp 微信小程序 UI 组件库
微信小程序 UI 组件库 Github地址 https://github.com/phonycode/wuss-weapp 文档 https://phonycode.github.io/wuss-we ...
- 微信小程序image组件binderror使用例子(对应html、js中的onerror)
官方文档 binderror HandleEvent 当错误发生时,发布到 AppService 的事件名,事件对象event.detail = {errMsg: 'something wrong' ...
随机推荐
- js Circle类
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- H5版如何在微信外(非微信浏览器)进行微信支付技术方案
官方是支持在非微信内置浏览器中调起微信支付的!H5支付是基于公众号基础开发的一种非微信内浏览器支付方式(需要单独申请支付权限),可以满足在微信外的手机H5页面进行微信支付的需求.同时,由于H5链接传播 ...
- Python type() 函数
描述 type() 函数如果你只有第一个参数则返回对象的类型,三个参数返回新的类型对象.类似isinstance() isinstance() 与 type() 区别: type() 不会认为子类是一 ...
- Python rstrip() 方法
描述 Python rstrip() 方法用于删除字符串尾部指定的字符,默认字符为所有空字符,包括空格.换行(\n).制表符(\t)等. 语法 rstrip() 方法语法: S.rstrip([cha ...
- SIPp常用脚本之二:UAS
看名字就能猜出来,这是作为SIP消息服务端的存在,启动uas,等着接受SIP消息并且给出响应. 一.uas.xml <?xml version="2.0" encoding= ...
- cocos2dx实现3d拾取注意事项
用的是cocos2dx 3.x,如果是真机测试,glview = cocos2d::GLViewImpl::createWithRect(...)和glview->setDesignResolu ...
- Windows环境下文件的彻底删除与恢复,推荐几个工具(整理)
1. 背景 在Windows(从XP到Win7)中删除文件时,无论是Delete或者是Shift+Delete,都不能真正的删除文件,它做的事情只是从文件分配表中删除了该文件的信息,而并未将文件从存储 ...
- 黑客编程教程(十三)多线程DOS程序
DOS基本原理相信大家都已经很熟悉了,DOS工具大家也用的很熟悉.在群里 经常有人说什么时候去DOS什么东西. 现在我们就自己编写一个DOS工具. #include <winsock2.h> ...
- maven(2)------maven构建项目
一 下载maven 官网地址: http://maven.apache.org/download.cgi 如图: 可以下载历史版本. 二 windows下maven配置 1. 解压下载后的包,解压后 ...
- Zabbix添加自定义监控项(一)
前言:由于Zabbix提供的项目和模板有限,有时我们需要自定义监控项,下面以监控磁盘I/O使用率为例,创建自动发现规则,并配置图形. (1)Zabbix_agentd端自动发现脚本,zabbix要求返 ...