前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >微信小程序----map组件实现(高德地图API实现wx.chooseLocation(OBJECT))

微信小程序----map组件实现(高德地图API实现wx.chooseLocation(OBJECT))

作者头像
Rattenking
发布2021-02-01 11:20:53
发布2021-02-01 11:20:53
82100
代码可运行
举报
文章被收录于专栏:RattenkingRattenking
运行总次数:0
代码可运行

声明

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

效果图

实现原理

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

WXML

代码语言:javascript
代码运行次数:0
运行
复制
<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

代码语言:javascript
代码运行次数:0
运行
复制
.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

代码语言:javascript
代码运行次数:0
运行
复制
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 属性标记该坐标。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018/02/08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 声明
  • 效果图
  • 实现原理
  • WXML
  • WXSS
  • JS
  • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档