zhangwei
2025-12-29 8edec205baf395715c044a86067505d58455b1c6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<template>
 
  <el-dialog :title="props.title" v-model="dialogVisble" width="100%">
    <div>
 
      <p> 经纬度:{{ loc.lat }},{{ loc.lon }}</p>
      <p>地址:{{ loc.address }}</p>
      <div id="container" style="width:100%;height:450px" ref="container">
 
      </div>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
 
    </template>
  </el-dialog>
</template>
 
<script setup>
import { onMounted, ref } from 'vue';
// 定义控制弹窗显隐的变量
var times
const dialogVisble = ref(false)
const props = defineProps({
  title: {
    type: String,
    default: "提示"
  }
})
const container = ref(null)
const loc = ref({
  lon: 104.07,
  lat: 30.67,
  address: "",
})
 
watch(() => dialogVisble.value,
  (val, o) => {
    console.log('val is ', val)
 
  }
)
 
onMounted(() => {
  console.log(1111)
  var times = setTimeout(() => {
    initmap()
  }, 9000);
 
 
})
 
const initmap = () => {
  var center = new TMap.LatLng(loc.value.lat, loc.value.lon);//设置中心点坐标
  const mapContainer = document.getElementById('container');
  console.log("mapContainer:", mapContainer)
  if (mapContainer) {
    clearTimeout(times)
  }
  //初始化地图
  var map = new TMap.Map(mapContainer, {
    center: center,
    zoom: 14, //设置地图缩放级别
  });
  var markerGeo = {
    id: 'center',
    position: map.getCenter(),
  };
 
  //创建并初始化MultiMarker
  var markerLayer = new TMap.MultiMarker({
    map: map,  //指定地图容器
    //样式定义
    geometries: [
      markerGeo
    ]
  })
  var geocoder = new TMap.service.Geocoder(); // 新建一个正逆地址解析类
  //绑定点击事件
  map.on("click", function (evt) {
    let poi = evt.poi;
    console.log('event is ', evt)
    console.log('poi is ', poi)
    var lat = evt.latLng.getLat().toFixed(6);
    var lng = evt.latLng.getLng().toFixed(6);
    loc.value.lon = lng;
    loc.value.lat = lat
 
    // markerGeo.position = map.getCenter();
    // markerLayer.updateGeometries([markerGeo]);
    var location = new TMap.LatLng(lat, lng);
    geocoder.getAddress({ location: location }) // 将给定的坐标位置转换为地址
      .then((result) => {
        loc.value.address = result.result.address + (poi ? poi.name : '')
      });
    map.setCenter(new TMap.LatLng(lat, lng));//坐标为天安门
    // flushLatLon()
  })
}
const emit = defineEmits(["click"])
const confirm = () => {
  emit("click", loc.value)
 
  // dialogVisble.value = false
}
 
const close = () => {
  dialogVisble.value = false
}
 
// 将变量暴露出来
defineExpose({
  dialogVisble
})
</script>
 
<style></style>