import MAPAPP, { minScale, maxScale } from "./MAPAPP"; let map = null; const compassCbs = []; let initScale = 1; Component({ /** * 组件的属性列表 */ properties: { floororder: Number, hidden: Boolean, loc: Array, searchType: Number, }, data: { loaded: false, dpr: wx.getSystemInfoSync().pixelRatio, shops: [], floors: [], pageHidden: false, sliderValue: 0, }, observers: { hidden(val) { if (val) { wx.stopCompass({ fail: console.log }); if (map) { map.shown = false; } } else { wx.startCompass({ fail: console.log }); if (map) { map.shown = true; } } }, floororder(num) { if (map && num !== null && num !== undefined && this.data.loaded) map.changeFloor(num); }, loc([x, y]) { if (map) { const crossPoint = map.cross({ x, y }); map.setXY(crossPoint.x, crossPoint.y); map.updateTheta2(0); try { const { name } = map.getNearestPoint({ x, z: y, floorOrder: this.data.floororder, }); this.triggerEvent("point", name); } catch (error) { console.log(error); } } }, }, lifetimes: { attached() {}, detached() { console.log("map2d detached"); map.dispose(); map = null; compassCbs.forEach((cb) => wx.offCompassChange(cb)); compassCbs.length = 0; }, ready() { this.createSelectorQuery() .select(".map2d") .fields({ node: true, size: true, }) .exec((res) => { map = new MAPAPP(res[0], this); initScale = map.scale; this.setData({ floors: map.mall.floors, sceneIndex: map.mall.groundFloorIndex, }); map.on("loaded", (map) => { this.setData({ loaded: true }); if (typeof this.data.floororder === "number") map.changeFloor(this.data.floororder); this.triggerEvent("map", map); const compassCb = ({ direction }) => { map && map.updateTheta2(-(direction / 180) * Math.PI); }; compassCbs.push(compassCb); wx.onCompassChange(compassCb); }); }); }, moved: function () {}, }, pageLifetimes: { hide() { this.setData({ pageHidden: true }); }, show() { this.setData({ pageHidden: false }); }, }, methods: { handlePinch(e) { if (map) { let newScale = initScale * e.detail.zoom; newScale = Math.max(minScale, newScale); newScale = Math.min(maxScale, newScale); map.scale = newScale; } }, handleMultitouchstart() { initScale = map.scale; }, }, });