<template>
  <div>
    <avue-map v-model="map" placeholder="请选择地图"></avue-map>
  </div>
</template>

<script>
export default {
  props: {
    value: [String, Object, Array, Number, Boolean],
    resultFormat: {
      type: [String, Object, Array],
      default: () => ['latitude', 'longitude'] //latitude纬度，longitude经度,formattedAddress地址
    }
  },
  data() {
    return {
      map: {},

    }
  },
  methods: {},
  computed: {},
  watch: {
    map(newVal) {
      if(!newVal) return
      let res = ''
      if(typeof this.resultFormat === 'string') {
        res = newVal[this.resultFormat]
      }
      if(this.resultFormat instanceof Array) {
        res = this.resultFormat.map(i => newVal[i])?.toString()
      }
      if(typeof this.resultFormat === 'object' && (!this.resultFormat instanceof Array)) {
        let obj = {}
        for(let key in this.resultFormat) {
          obj[key] = newVal[this.resultFormat[key]]
        }
        res = obj;
      }
      this.$emit('input', res)
    },
    value(newVal) {
      if(typeof this.resultFormat === 'string') {
        this.map[this.resultFormat] = Number(newVal)
      }
      if(this.resultFormat instanceof Array && newVal) {
        let valArr = newVal.split(',')
        this.resultFormat.forEach((i,index) => {
          this.map[i] = Number(valArr[index])
        })
      }
      if(typeof this.resultFormat === 'object' && (!this.resultFormat instanceof Array)) {
        for(let key in this.resultFormat) {
          this.map[key] = Number(newVal[key])
        }
      }

      console.log(this.map)
    }
  }
}
</script>

<style scoped lang="scss">
</style>
