微信小程序map组件结合高德地图API实现wx.chooseLocation功能示例


Posted in Javascript onJanuary 23, 2019

本文实例讲述了微信小程序map组件结合高德地图API实现wx.chooseLocation功能。分享给大家供大家参考,具体如下:

声明

bug: 页面搜索返回的列表在真机测试是会出现不显示问题?
造成原因:在小程序map组件的同一区域,map组件的视图层比普通的文本视图层要高,所以在真机会遮挡!
解决办法:将该文本视图采用cover-view,放在map中。
感谢: 感谢Lrj_estranged指出问题!

效果图

微信小程序map组件结合高德地图API实现wx.chooseLocation功能示例

实现原理

通过高德地图的微信小程序开发API(getInputtips),实现关键词获取对应提示列表,同时返回location。

WXML

<view class="map-inputtips-input">
 <input bindinput="bindInput" placeholder="搜索" focus="true" />
</view>
<view class="map_container">
 <map class="map" latitude='{{latitude}}' longitude='{{longitude}}' markers='{{markers}}'>
 <cover-view class="map-search-list {{isShow ? '' : 'map-hide'}}">
  <cover-view bindtouchstart="bindSearch" wx:key="searchId" data-keywords="{{item.name}}" data-location="{{item.location}}" class="map-box" wx:for="{{tips}}">
  {{item.name}}
  </cover-view>
 </cover-view>
 </map>
</view>

WXSS

.map-inputtips-input{
 height: 80rpx;
 line-height: 80rpx;
 width: 100%;
 box-sizing: border-box;
 font-size: 30rpx;
 padding: 0 10px;
 background-color: #fff;
 position: fixed;
 top: 0;
 left: 0;
 z-index: 1000;
 border-bottom:1px solid #c3c3c3;
}
.map-inputtips-input input{
 border: 1px solid #ddd;
 border-radius: 5px;
 height: 60rpx;
 line-height: 60rpx;
 width: 100%;
 box-sizing: border-box;
 padding: 0 5px;
 margin-top: 10rpx;
}
.map-box{
 margin: 0 10px;
 border-bottom:1px solid #c3c3c3;
 height: 80rpx;
 line-height: 80rpx;
}
.map-box:last-child{border: none;}
.map-search-list{
 position: fixed;
 top: 80rpx;
 left: 0;
 width: 100%;
 z-index: 1000;
 background-color: #fff;
}

JS

const app = getApp();
const amap = app.data.amap;
const key = app.data.key;
Page({
 data: {
 isShow: false,
 tips: {},
 longitude: '',
 latitude: '',
 markers: []
 },
 onLoad() {
 var _this = this;
 wx.getLocation({
  success: function(res) {
  if (res && res.longitude){
   _this.setData({
   longitude: res.longitude,
   latitude: res.latitude,
   markers:[{
    id:0,
    longitude: res.longitude,
    latitude: res.latitude,
    iconPath: '../../src/images/ding.png',
    width:32,
    height:32
   }]
   })
  }
  }
 })
 },
 bindInput: function (e) {
 var _this = this;
 var keywords = e.detail.value;
 var myAmap = new amap.AMapWX({ key: key });
 myAmap.getInputtips({
  keywords: keywords,
  location: '',
  success: function (res) {
  if (res && res.tips) {
   _this.setData({
   isShow: true,
   tips: res.tips
   });
  }
  }
 })
 },
 bindSearch: function (e) {
 var keywords = e.target.dataset.keywords;
 var location = e.target.dataset.location.split(',');
 this.setData({
  isShow: false,
  longitude: location[0],
  latitude: location[1],
  markers: [{
  id: 0,
  longitude: location[0],
  latitude: location[1],
  iconPath: '../../src/images/ding.png',
  width: 32,
  height: 32,
  anchor: { x: .5, y: 1 },
  label: {
   content: keywords,
   color: 'blue',
   fontSize: 12,
   borderRadius: 5,
   bgColor: '#fff',
   padding: 3,
   x: 0,
   y: -50,
   textAlign: 'center'
  }
  }]
 })
 }
})

总结

1. 输入框事件获取关键字,通过关键字获取展示列表;

2. 列表选择事件,获取对应的location,并通过map组件的 markers 属性标记该坐标。

希望本文所述对大家微信小程序开发有所帮助。

Javascript 相关文章推荐
javascript事件冒泡实例分析
May 13 Javascript
AngularJS中的过滤器使用详解
Jun 16 Javascript
jQuery实现二级下拉菜单效果
Jan 05 Javascript
Angular使用 ng-img-max 调整浏览器中的图片的示例代码
Aug 17 Javascript
在create-react-app中使用sass的方法示例
Oct 01 Javascript
详解微信小程序用定时器实现倒计时效果
Apr 30 Javascript
jQuery zTree插件快速实现目录树
Aug 16 jQuery
JavaScript在web自动化测试中的作用示例详解
Aug 25 Javascript
vue 解决移动端弹出键盘导致页面fixed布局错乱的问题
Nov 06 Javascript
jQuery HTML设置内容和属性操作实例分析
May 20 jQuery
vue+element table表格实现动态列筛选的示例代码
Jan 14 Vue.js
一起来看看Vue的核心原理剖析
Mar 24 Vue.js
微信小程序使用map组件实现检索(定位位置)周边的POI功能示例
Jan 23 #Javascript
使用javascript做时间倒数读秒功能的实例
Jan 23 #Javascript
大转盘抽奖小程序版 转盘抽奖网页版
Apr 16 #Javascript
javascript json字符串到json对象转义问题
Jan 22 #Javascript
使用JavaScript保存文本文件到本地的两种方法
Jan 22 #Javascript
微信小程序实现九宫格抽奖
Apr 15 #Javascript
200行HTML+JavaScript实现年会抽奖程序
Jan 22 #Javascript
You might like
初次接触php抽象工厂模式(Elgg)
2010/03/21 PHP
php curl常见错误:SSL错误、bool(false)
2011/12/28 PHP
PHP中的str_repeat函数在JavaScript中的实现
2013/09/16 PHP
Yii结合CKEditor实现图片上传功能
2014/06/13 PHP
php cookie中点号(句号)自动转为下划线问题
2014/10/21 PHP
7款风格新颖的jQuery/CSS3菜单导航分享
2013/04/23 Javascript
使用js判断TextBox控件值改变然后出发事件
2014/03/07 Javascript
jQuery实现伸展与合拢panel的方法
2015/04/30 Javascript
谈谈AngularJs中的隐藏和显示
2015/12/09 Javascript
JavaScript实现多种排序算法
2016/02/24 Javascript
JS中的二叉树遍历详解
2016/03/18 Javascript
JS图片左右无缝隙滚动的实现(兼容IE,Firefox 遵循W3C标准)
2016/09/23 Javascript
JavaScript  event对象整理及详细介绍
2016/10/10 Javascript
windows系统下更新nodejs版本的方案
2017/11/24 NodeJs
pace.js和NProgress.js两个加载进度插件的一点小总结
2018/01/31 Javascript
详解如何用webpack4从零开始构建react开发环境
2019/01/27 Javascript
vue中uni-app 实现小程序登录注册功能
2019/10/12 Javascript
[06:07]刀塔密之二:攻之吾命受之吾幸
2014/07/03 DOTA
python中的一些类型转换函数小结
2013/02/10 Python
python正则匹配抓取豆瓣电影链接和评论代码分享
2013/12/27 Python
python利用beautifulSoup实现爬虫
2014/09/29 Python
python通过opencv实现批量剪切图片
2017/11/13 Python
python中bytes和str类型的区别
2019/10/21 Python
python 中值滤波,椒盐去噪,图片增强实例
2019/12/18 Python
pycharm通过ssh连接远程服务器教程
2020/02/12 Python
CSS3 实现侧边栏展开收起动画
2014/12/22 HTML / CSS
无需JS和jQuery代码实现CSS3鼠标浮动放大图片
2016/11/21 HTML / CSS
CSS3 实现的加载动画
2020/12/07 HTML / CSS
ALLSAINTS英国官网:伦敦新锐潮流品牌
2016/09/19 全球购物
优秀学生干部个人的自我评价
2013/10/04 职场文书
中学自我评价
2014/01/31 职场文书
计算机实训报告范文
2014/11/05 职场文书
小学公民道德宣传日活动总结
2015/03/23 职场文书
Python机器学习实战之k-近邻算法的实现
2021/11/27 Python
《艾尔登法环》1.03.3补丁上线 碎星伤害调整
2022/04/07 其他游戏
Java处理延时任务的常用几种解决方案
2022/06/01 Java/Android