area.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <template>
  2. <el-form-item label="地区:">
  3. <el-cascader
  4. v-model="selectedOptions"
  5. placeholder="请选择地区"
  6. size="default"
  7. :options="regionData"
  8. @change="handleChange"
  9. />
  10. </el-form-item>
  11. <tlbs-map
  12. ref="mapRef"
  13. api-key="3SVBZ-6MYYZ-Q75X5-7YN43-BMIBZ-YABND"
  14. :center="center"
  15. :zoom="zoom"
  16. :control="control"
  17. class="mapBox"
  18. :options="mapOption"
  19. @click="onClick"
  20. @map_inited="onMapInited"
  21. >
  22. <tlbs-multi-marker
  23. ref="markerRef"
  24. :geometries="geometries"
  25. :styles="styles"
  26. :options="options"
  27. @click="openInfo"
  28. />
  29. </tlbs-map>
  30. </template>
  31. <script setup>
  32. import { ref } from 'vue'
  33. // 地区选择
  34. import { regionData, codeToText } from 'element-china-area-data'
  35. import { jsonp } from 'vue-jsonp'
  36. import { useCoordinateStore } from '@/pinia/modules/coordinate'
  37. const useCoordinate = useCoordinateStore()
  38. const handleChange = async(value) => {
  39. const province = codeToText[value[0]] // 省-->
  40. const city = codeToText[value[1]] // 市-->
  41. const district = codeToText[value[2]] // 区-->
  42. const fullAddress = `${province}${city}${district}`
  43. console.log(fullAddress)
  44. if (value.length < 3) {
  45. console.log('请选择完整的省市区')
  46. return
  47. }
  48. // 3SVBZ-6MYYZ-Q75X5-7YN43-BMIBZ-YABND
  49. jsonp('https://apis.map.qq.com/ws/geocoder/v1/?address=', {
  50. output: 'jsonp',
  51. address: fullAddress,
  52. key: 'GDFBZ-AXD6B-2TPUJ-JBLEX-STUGE-DGBZH'
  53. }).then(res => {
  54. console.log('返回经纬度', res.result.location)
  55. }).catch(err => {
  56. console.log('地图错误:', err)
  57. })
  58. }
  59. const selectedOptions = ref('')
  60. const control = {
  61. scale: {},
  62. zoom: {
  63. position: 'topRight',
  64. },
  65. }
  66. const styles = {
  67. marker: {
  68. width: 20,
  69. height: 30,
  70. anchor: { x: 10, y: 30 },
  71. },
  72. }
  73. // 地图
  74. const mapRef = ref(null)
  75. const markerRef = ref(null)
  76. const center = ref(useCoordinate.place)
  77. const zoom = ref(10)
  78. const mapOption = ref({
  79. mapStyleId: 'style1'
  80. })
  81. const geometries = ref([{ styleId: 'marker', position: { lat: 39.91799, lng: 116.397027 }}])
  82. const onClick = (e) => {
  83. console.log(e.latLng)
  84. // geometries.value = [
  85. // {
  86. // styleId: 'marker',
  87. // position: { lat: 28.7052848, lng: 113.5759597 },
  88. // },
  89. // {
  90. // styleId: 'marker',
  91. // position: { lat: e.latLng.lat, lng: e.latLng.lng },
  92. // },
  93. // ]
  94. }
  95. const onMapInited = () => {
  96. // 地图加载完成后,可以获取地图实例、点标记实例,调用地图实例、点标记实例方法
  97. // console.log(mapRef.value.map)
  98. // console.log(markerRef.value.marker)
  99. }
  100. </script>