location.js 3.14 KB
// components/2.0/filter/location.js
import DA from '../../../pages/reserve/data'
import util from '../../../utils/util'
import QQMapWX from '../../../utils/qqmap-wx-jssdk.js'
const qqmapsdk = new QQMapWX({
  key: 'SDEBZ-I74W4-QLGUX-XXYXU-HIR23-3QFBJ'
});
const CACHE_LOCALTION_KEY='CACHE_LOCALTION_KEY'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    code:String,
  },

  /**
   * 组件的初始数据
   */
  data: {
    address:'',
    activeCity:{},
    hasShow:false,
  },
  attached(){
    this.init()

  },
  /**
   * 组件的方法列表
   */
  methods: {
    async init(){
      this.position={}
      this.isAddr=false
      this.useDefTS=false
      this.citys=await DA.getCitys()
      this.cacheCity=null
      this.setData({
        citys: this.citys
      })
      let cache=util.cache(CACHE_LOCALTION_KEY)
      if(cache && cache.city){
        this.cacheCity=cache.city
      }
      this.selectComponent('#getpos').getPos();
    },
    rePos(e){
      this.cacheCity=null
      this.isAddr=util.attr(e,'isAddr') && !this.useDefTS
      this.selectComponent('#getpos').getPos();
    },
    hide(){
      this.setData({
        hasShow:false,
      })
    },
    show(){
      this.setData({
        hasShow:true,
      })
    },
    event(data){
      data.position=this.position
      this.triggerEvent('event',data)
      util.cache('CACHE_LOCALTION_KEY',data)
    },
    checkedCity(e){
      let activeCity=this.citys[util.attr(e,'index')]
      this.setData({
        activeCity:activeCity,
      })
      this.event({city:activeCity,address:this.position.address,isAddr:false})
      this.hide()
    },
    onpos(e) {
      let res = e.detail;
      console.log('onpos.res', res);
      let useDef=false
      if(res.latitude==''){//默认使用唐山
        this.useDefTS=true
        res.latitude=39.684671
        res.longitude=118.176300
      }
      this.position.latitude = res.latitude;
      this.position.longitude = res.longitude;
      qqmapsdk.reverseGeocoder({
        location: {
          latitude: res.latitude,
          longitude: res.longitude
        },
        success:(res)=>{
          if(res.status!=0) return;
          console.log('qqmapsdk.reverseGeocoder',res.result)
          this.position.address_component= res.result.address_component
          this.position.address=this.useDefTS?'':res.result.address
          let activeCity={}
          if(this.cacheCity){
            activeCity=this.cacheCity
          }else if(!this.data.code){
            this.citys.some(ci=>{
              if(ci.name==this.position.address_component.city){
                activeCity=ci
                return true
              }
            })
          }else{
            this.citys.some(ci=>{
              if(ci.code==this.data.code){
                activeCity=ci
                return true
              }
            })
          }

          this.setData({
            activeCity:activeCity,
            address:this.position.address
          })
          this.event({city:activeCity,address:this.position.address,isAddr:this.isAddr})
          if(this.isAddr){//点击地址
            this.hide()
          }
        },
      })

    },
  }
})