123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import { randomNum } from '@/utils/util'
- const app = getApp()
- Page({
- data: {
- latitude: 23.099994,
- longitude: 113.324520,
- markers: [],
- userInfo: null,
- },
- wsListener({ data = '' }) {
- const { biz_code, data: body } = JSON.parse(data) || {}
- if (biz_code === 'ling_xiu_warning') this.addMarkers(body)
- },
- connectWs() {
- const { baseUrl } = app.globalData
- const { token } = this.data.userInfo || []
- if (token) {
- this.socketCli = wx.connectSocket({
- url: `wss://${baseUrl}/dockWebsocket?x-auth-token=${encodeURIComponent(token)}&userType=1`,
- })
- this.socketCli.onMessage(this.wsListener)
- }
- },
- addMarkers(newMarker) {
- const { markers } = this.data
- const { longitude, latitude } = newMarker
- const [lng, lat] = app.coordtransform.wgs84togcj02(longitude, latitude)
- newMarker.longitude = lng
- newMarker.latitude = lat
- if (markers.length >= 10) markers.splice(0, 1)
- markers.unshift({
- id: randomNum(5),
- width: 32,
- height: 32,
- iconPath: '/static/img/marker_alarm.png',
- customCallout: {
- anchorY: 0,
- anchorX: 0,
- display: 'ALWAYS'
- },
- show: true,
- ...newMarker,
- longitude: lng,
- latitude: lat,
- })
- this.mapCtx.moveToLocation({
- longitude: lng,
- latitude: lat,
- })
- this.setData({
- markers
- })
- },
- removeMarkers() {
- this.setData({
- markers: []
- })
- },
- onMarkerTap(e) {
- const { markerId } = e.detail
- const item = this.data.markers.find(r => r.id === markerId)
- const idx = this.data.markers.findIndex(r => r.id === markerId)
- this.setData({
- [`markers[${idx}]`]: {
- ...item,
- show: !item.show
- }
- })
- },
- onShow() {
- const userInfo = wx.getStorageSync('userInfo')
- if (userInfo) {
- this.setData({ userInfo })
- this.connectWs()
- }
- },
- onLoad: function () {
- this.mapCtx = wx.createMapContext('map')
- wx.getLocation({
- type: 'gcj02',
- success: (res) => {
- const { longitude, latitude } = res;
- this.setData({
- longitude, latitude
- })
- }
- })
- },
- onHide() {
- this.socketCli && this.socketCli.close()
- }
- })
|