寫在開頭: 關于Marker產品的需求是想要在地圖同時展示marker和一些信息 剛開始覺得這個功能用markers 和 markers的屬性label 可以很容易實現 代碼如下: initMarkerData(){ let _this = this; let markerList = []; for (let i in _this.data.listData){ let poiInfo = _this.data.listData[i]; let singleMarker = {}; let labelInfo = {}; singleMarker['iconPath'] = "../../image/oilCallout.png"; singleMarker['width'] = 40; singleMarker['height'] = 40; singleMarker['id'] = poiInfo.id; singleMarker['latitude'] = poiInfo.placeLatitude; singleMarker['longitude'] = poiInfo.placeLongitude; singleMarker['label'] = {}; singleMarker['label']['content'] = poiInfo.placeName + '\n' + poiInfo.placeAddress; singleMarker['label']['x'] = 20; singleMarker['label']['y'] = -40; singleMarker['label']['padding'] = 3; singleMarker['label']['bgColor'] = '#ffffff'; singleMarker['label']['borderRadius'] = 5; markerList.push(singleMarker); } console.log(markerList); _this.setData({ markers : markerList }) return markerList; }, onLoad() {//頁面加載時 let _this = this; //初次進入頁面獲取到我的位置,方便設置map wx.getLocation({//獲取當前的地理位置、速度 type: 'wgs84', //返回可以用于wx.openLocation的經緯度 success: res => { _this.setData({ markers: _this.initMarkerData(),//請求后端獲取所有markers longitude: res.longitude, latitude: res.latitude, scale: 8 //首次地圖比例改這里 }) } }); }, 然后,問題就出現了,在微信開發者工具里展示是沒有OK的,但是!!!在真機預覽的時候,我發現label會有偶發性的不展示,而且不展示的情況也沒有任何章法。具體分為以下幾類:
a: 只展示一部分
我的內心:fn9#bei*jvjc%vjsvdj#dbcjscbv&sbcd%% Marker的label有兼容性問題,目前我自己驗證過的機型:(后續會根據項目進度做補充)
小米5: 沒問題,數據加載和樣式都OK 作為一個孜孜不倦的程序員,我在小程序的社區提問了,在提問的時候發現,我不是一個人!
然后就查小程序API看下有沒有什么可替代的方案于是就發現了marker還有一個callout屬性可以做替代,但是!!callout屬性只是單獨的氣泡展示,里面只能展示文字,不能展示圖片,還不能設置背景圖。 我的內心:fn9#bei*jvjc%vjsvdj#dbcjscbv&sbcd%% 最后,一邊等待騰訊官方修bug一邊找最優方案 比如:把label的背景透明,在IOS下偏移會比較不明顯(適用于數字展示) 小程序API挺多的,很意外,但是還需要不斷優化,還是很不錯的~ |