myMap.js 4.68 KB
Component({
  /**
   * 组件的属性列表
   */
  options: {
    multipleSlots: true
  },
  properties: {
    markers: Array,
    polyline: Array,
    isLoading: Boolean,
    includePoints: Array // 当前页面是否有marker或路径的左边点
  },

  /**
   * 组件的初始数据
   */
  data: {
    currentLo: 116.363874, // 地图中心点坐标
    currentLa: 39.738269,
    latitude: null, // 定位坐标
    longitude: null,
    isAuthLocation: false,
    enableSatellite: false,
    enableTraffic: false,
    carInfoWindowObj: {},
    isGPS: true,
    scale: 16,
    isCanClick: true
  },
  ready() {
  },
  attached() {
    
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getMapCtx() {
      if (this.mapCtx) {
        return this.mapCtx
      }
      // 创建 map 上下文 MapContext 对象
      this.mapCtx = wx.createMapContext('Mymap', this)
      return this.mapCtx
    },
    bindmarkertap(e) {
      this.triggerEvent('getCarInfoWindowObj', e)
    },
    // 刷新
    refreshClick() {
      this.triggerEvent('refresh')
    },
    // 点击地图
    mapClick(e) {
      this.triggerEvent('mapCallback', e)
    },
    // 移动地图
    moveToLocation: function (latitude, longitude ) {
      this.getMapCtx().moveToLocation({
        longitude: longitude,
        latitude: latitude
      })
    },
    // 图层缩放控制
    layerClick(e) {
      switch(Number(e.currentTarget.dataset.num)) {
        case 1:
          this.setData({
            enableSatellite: !this.data.enableSatellite,
            enableTraffic: this.data.enableTraffic
          })
          break
        case 2:
          this.setData({
            enableSatellite: this.data.enableSatellite,
            enableTraffic: !this.data.enableTraffic
          })
        break
      case 3:
        this.getMapCtx().getScale({
          success: (res) => {
            this.getMapCtx().getCenterLocation({
              success: (e) => {
                this.setData({
                  currentLo: e.longitude,
                  currentLa: e.latitude,
                  scale: res.scale >= 20 ? 20 : res.scale+1
                })
              }
            })
          }
        })
        break
      case 4:
        this.getMapCtx().getScale({
          success: (res) => {
            this.getMapCtx().getCenterLocation({
              success: (e) => {
                this.setData({
                  currentLo: e.longitude,
                  currentLa: e.latitude,
                  scale: res.scale <= 3 ? 3 : res.scale-1
                })
              }
            })
          }
        })
        break
    }
    },
    // 定位
    locateClick(e) {
      let _this = this
      if (!this.data.isCanClick) return
      this.setData({
        isCanClick: false
      })
      setTimeout(() => {
        this.setData({
          isCanClick: true
        })
      }, 30*1000)
      if (_this.data.isAuthLocation) {
        _this.getLocation()
      } else {
        wx.getSetting({
          success: (res) => {
            let authSetting = res.authSetting
            if (authSetting['scope.userLocation']) {
              // 已授权
              _this.getLocation()
              _this.setData({
                isAuthLocation: true
              })
            } else {
              if (typeof(authSetting['scope.userLocation']) !== 'undefined') {
                wx.showModal({
                  title: '您未开启地理位置授权',
                  content: '点击确定去开启地理位置授权',
                  success: res => {
                    if (res.confirm) {
                      wx.openSetting()
                    }
                  }
                })
              } else {
                _this.getLocation()
              }
            }
          }
        })
      }
    },
    getLocation() {
      let _this = this
      wx.getLocation({
        type: 'gcj02',
        success (res) {
          _this.setData({
            longitude: res.longitude,
            latitude: res.latitude
          })
          if (_this.data.includePoints.length > 0) { // 包含其他坐标点 不需将获取到的坐标移动到地图中心
            _this.triggerEvent('locationCallback', {lat: res.latitude, lng: res.longitude})
          } else {
            _this.moveToLocation(res.latitude, res.longitude)
          }
        },
        fail () {
          if (_this.data.includePoints.length > 0) { // 包含其他坐标点 不需将获取到的坐标移动到地图中心
            _this.triggerEvent('locationCallback', {lat: _this.data.latitude, lng: _this.data.longitude})
          } else {
            _this.moveToLocation(_this.data.latitude, _this.data.longitude)
          }
        }
      })
    }
  },
})