const app = getApp();
Page({
  data: {
    latitude: 0,
    longitude: 0,
    markers: [],
    polyline: []
  },
  onLoad(e) {
    const {
      lat,
      lnt,
    } = e;
    this.setData({
      lnt,
      lat,
      markers: [{
        id: 1,
        latitude: lat - 0,
        longitude: lnt - 0,
        width: 30,
        height: 30,
      }]
    })
  },
  onReady: function (e) {
    const that =this;
    this.mapCtx = wx.createMapContext('myMap');
    const {longitude,latitude}=app.globalData;
    if(longitude&&latitude){
      this.getPolyline(longitude,latitude)
    }else{
      wx.getSetting({
        success(res) {
          if (res.authSetting['scope.userFuzzyLocation'] === true) {
            that.getLocation();
          } else if (res.authSetting['scope.userFuzzyLocation'] === undefined) {
            that.locationScope();
          } else if (res.authSetting['scope.userFuzzyLocation'] !== undefined && res.authSetting['scope.userFuzzyLocation'] !== true) {
            wx.showModal({
              title: '是否授权当前位置',
              content: '需要获取您的地理位置，请确认授权，否则无法使用相关功能！',
              success: res => {
                //如果点击取消则显示授权失败
                if (res.cancel) {
                  wx.showToast({
                    title: '您已拒绝授权!',
                    icon:'none'
                  })
                  that.setData({
                    city_name:"请选择"
                  })
                }
                //如果点击确定会打开授权页请求二次授权
                else if (res.confirm) {
                  wx.openSetting({
                    success: dataAu => {
                      if (dataAu.authSetting['scope.userFuzzyLocation'] === true) {
                        wx.showToast({
                          title: '授权成功',
                          icon:'none'
                        })
                        //再次授权，调用getLocationt的API
                        that.locationScope()
                      } else {
                        wx.showToast({
                          title: '授权失败',
                          icon:'none'
                        })
                      }
                    }
                  })
                }
              }
            })
          }
        },
        fail(err){
          wx.showModal({
              title: '提示',
              content: '未知错误，请重新打开小程序！'
          })
      }
      })
    }
  },
  getLocation() {
    const that = this;
    wx.getFuzzyLocation({
      success(res) {
        const {
          latitude,
          longitude
        } = res;
        app.globalData.longitude=longitude;
        app.globalData.latitude=latitude;
        that.getPolyline(longitude,latitude)
      },
    })
  },
  locationScope() {
    const that = this;
    wx.authorize({
      scope: 'scope.userFuzzyLocation',
      success(res) {
        if (res.errMsg == "authorize:ok") {
          that.getLocation();
        }
      },
    })
  },
  getPolyline(longitude,latitude){
    const {lat,lnt} = this.data;
        this.setData({
          longitude,
          latitude,
          polyline: [{
            points: [{
              latitude,
              longitude
            },
            {
              latitude: lat-0 ,
              longitude: lnt-0
            }
          ],
          width:10,
          colorList:["#FFB6C1","#1E90FF"]
          }]
        })
  }
})