微信小程序map組件結(jié)合高德地圖API實(shí)現(xiàn)wx.chooseLocation功能示例
本文實(shí)例講述了微信小程序map組件結(jié)合高德地圖API實(shí)現(xiàn)wx.chooseLocation功能。分享給大家供大家參考,具體如下:
聲明
bug: 頁(yè)面搜索返回的列表在真機(jī)測(cè)試是會(huì)出現(xiàn)不顯示問(wèn)題?
造成原因:在小程序map組件的同一區(qū)域,map組件的視圖層比普通的文本視圖層要高,所以在真機(jī)會(huì)遮擋!
解決辦法:將該文本視圖采用cover-view,放在map中。
感謝: 感謝Lrj_estranged指出問(wèn)題!
效果圖

實(shí)現(xiàn)原理
通過(guò)高德地圖的微信小程序開(kāi)發(fā)API(getInputtips),實(shí)現(xiàn)關(guān)鍵詞獲取對(duì)應(yīng)提示列表,同時(shí)返回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'
}
}]
})
}
})
總結(jié)
1. 輸入框事件獲取關(guān)鍵字,通過(guò)關(guān)鍵字獲取展示列表;
2. 列表選擇事件,獲取對(duì)應(yīng)的location,并通過(guò)map組件的 markers 屬性標(biāo)記該坐標(biāo)。
希望本文所述對(duì)大家微信小程序開(kāi)發(fā)有所幫助。
相關(guān)文章
js使用for循環(huán)與innerHTML獲取選中tr下td值
這篇文章主要與大家分享了js使用for循環(huán)與innerHTML獲取選中tr下td值的方法,很簡(jiǎn)單,但很實(shí)用,有需要的朋友可以參考下2014-09-09
javascript下利用arguments實(shí)現(xiàn)string.format函數(shù)
sitepoint上看到Andrew Tetlaw在08年寫的文章arguments: A JavaScript Oddity,閱讀之后,除了對(duì)arguments溫故知新一遍以外,印象最深刻的還是Andrew的第一個(gè)函數(shù)實(shí)現(xiàn)的string.format功能。2010-08-08
JavaScript截取字符串的2個(gè)函數(shù)介紹
這篇文章主要介紹了JavaScript截取字符串的2個(gè)函數(shù)介紹,它們分別是substring和substr函數(shù),本文用實(shí)例講解了它們的用法,需要的朋友可以參考下2014-08-08
JS中可能會(huì)常用到的一些數(shù)據(jù)處理方法
這篇文章主要給大家介紹了JS中可能會(huì)常用到的一些數(shù)據(jù)處理方法,好多知識(shí)寫下來(lái)也能加深一下自身的記憶,文中給出了詳細(xì)的實(shí)例代碼,對(duì)大家學(xué)習(xí)或者使用JS具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-09-09
javascript時(shí)間戳和日期字符串相互轉(zhuǎn)換代碼(超簡(jiǎn)單)
下面小編就為大家?guī)?lái)一篇javascript時(shí)間戳和日期字符串相互轉(zhuǎn)換代碼(超簡(jiǎn)單)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06
JS辨別訪問(wèn)瀏覽器判斷是android還是ios系統(tǒng)
掃描二維碼之后自動(dòng)分辨出是android還是ios系統(tǒng),因此就要用JS辨別訪問(wèn)瀏覽器針對(duì)于不同的系統(tǒng)進(jìn)行不同的下載,需要的朋友可以參考下2014-08-08
javascript中獲取class的簡(jiǎn)單實(shí)現(xiàn)
下面小編就為大家?guī)?lái)一篇javascript中獲取class的簡(jiǎn)單實(shí)現(xiàn)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07

