微信小程序開(kāi)發(fā)之callout自定義氣泡的實(shí)現(xiàn)
2020-09-28
導(dǎo)讀:微信小程序中callout自定義氣泡的實(shí)現(xiàn),是地圖里的屬性,那么下面小編為大家來(lái)介紹要怎么實(shí)現(xiàn)。...
微信小程序中callout自定義氣泡的實(shí)現(xiàn),是地圖里的屬性,那么下面小編為大家來(lái)介紹要怎么實(shí)現(xiàn)。
微信小程序里面地圖 callout 這個(gè)屬性
callout呢,是在標(biāo)記的點(diǎn)上面顯示 一個(gè) 氣泡,作為提示用
最后展示下 效果 可以展示 頂部氣泡以及監(jiān)聽(tīng)被點(diǎn)擊
廢話不多說(shuō),直接上代碼
wxml
[AppleScript] 純文本查看 復(fù)制代碼
?
1
2
3
4
|
<map id="map" scale="{{scale}}" bindcontroltap="controltap" markers="{{markers}}" bindmarkertap="markertap"
latitude="{{latitude}}" bindcallouttap = "bindcallouttap"
longitude ="{{longitude}}"
controls="{{controls}}" show-location bindregionchange="regionchange" style="width: 100%; height: {{view.Height}}px;"></map>
|
js
[AppleScript] 純文本查看 復(fù)制代碼
?
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
var app = getApp();
Page({
data: {
url: '',[color=#0800][backcolor=rgb(255, 255, 254)][font=Consolas]//測(cè)試的時(shí)候把下面listData換成身邊的點(diǎn)就行[/font][/backcolor][/color]
listData: [{
"id": 1,
"placeName": "測(cè)試地址1",
"placeImageUrl": "",
"placeOpenTime": 1506200400,
"placeCloseTime": 1506265200,
"placeAddress": "測(cè)試地址1啊",
"placeLongitude": "114.08190678985596",
"placeLatitude": "22.544808209639864"
}, {
"id": 2,
"placeName": "測(cè)試地址2",
"placeImageUrl": "",
"placeOpenTime": 1506200400,
"placeCloseTime": 1506265200,
"placeAddress": "測(cè)試地址2啊",
"placeLongitude": "114.0938372555542",
"placeLatitude": "22.53953655390022"
}, {
"id": 3,
"placeName": "測(cè)試地址3",
"placeImageUrl": "",
"placeOpenTime": 1506243600,
"placeCloseTime": 1506265200,
"placeAddress": "測(cè)試地址3啊",
"placeLongitude": "114.05454",
"placeLatitude": "22.52291"
}],
scale: '15',
Height: '0',
controls: '40',
latitude: '',
longitude: '',
markers: [],
},
onReady: function(e) {
// 使用 wx.createMapContext 獲取 map 上下文
this.mapCtx = wx.createMapContext('myMap')
},
onLoad: function() {
var that = this;
that.setData({
url: app.globalData.url
})
// var data = JSON.stringify({
// page: 1,
// pageSize: 10,
// request: {
// placeLongitude: app.globalData.longitude,
// placeLatitude: app.globalData.latitude,
// userId: app.globalData.userId
// }
// })
wx.getLocation({
type: 'gcj02', //返回可以用于wx.openLocation的經(jīng)緯度
success: (res) => {
console.log(res);
that.setData({
markers: that.getSchoolMarkers(),
scale: 12,
longitude: res.longitude,
latitude: res.latitude
})
}
});
wx.getSystemInfo({
success: function(res) {
//設(shè)置map高度,根據(jù)當(dāng)前設(shè)備寬高滿屏顯示
that.setData({
view: {
Height: res.windowHeight
}
})
}
})
},
bindcallouttap: function(e) {
console.log("頭上文字被點(diǎn)擊", e)
},
markertap: function(e) {
console.log("定位的點(diǎn)被點(diǎn)擊", e)
},
controltap: function(e) {
console.log("111")
this.moveToLocation()
},
getSchoolMarkers() {
var market = [];
for (let item of this.data.listData) {
let marker1 = this.createMarker(item);
market.push(marker1)
}
console.log("market===========", market)
return market;
},
moveToLocation: function() {
this.mapCtx.moveToLocation()
},
strSub: function(a) {
var str = a.split(".")[1];
str = str.substring(0, str.length - 1)
return a.split(".")[0] + '.' + str;
},
createMarker(point) {
let latitude = this.strSub(point.placeLatitude);
let longitude = point.placeLongitude;
let marker = {
iconPath: "../images/here.png",
id: point.id || 0,
name: point.placeName || '',
title: point.placeName || '',
latitude: latitude,
longitude: longitude,
label: {
x: -24,
y: -26,
content: point.placeName
},
width: 50,
height: 50,
callout: {
content: point.placeName || '',
fontSize: 14,
bgColor: "#FFF",
borderWidth: 1,
borderColor: "#CCC",
padding: 4,
display: "ALWAYS",
textAlign: "center"
}
};
return marker;
}
})
|
wxss
[AppleScript] 純文本查看 復(fù)制代碼
?
1
2
3
4
5
6
7
8
9
|
.map_container{
height: 300px;
width: 100%;
}
.map {
height: 100%;
width: 100%;
}
|
第二部分:如何開(kāi)通一個(gè)小商店