riku
2023-10-31 2d3d56ff801b73afdb779267004d740f9beafe57
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<template>
  <el-dialog v-model="dialogShow" width="70%" destroy-on-close>
    <template #header>
      <div> 坐标拾取</div>
    </template>
    <div class="fy-tip-red">左键点击地图选取坐标点,或者根据关键字搜索地点</div>
    <el-row>
      <el-col :span="10">
        <el-form
          :inline="true"
          label-width="50px"
          label-position="left"
          :model="formObj"
          ref="formRef"
          destroy-on-close
        >
          <el-form-item label="地址" prop="address">
            <el-input
              v-model="formObj.address"
              placeholder="请输入地址搜索"
            ></el-input>
          </el-form-item>
          <!-- <el-form-item label="经度" prop="lon">
        <el-input
          v-model="formObj.lon"
          style="width: 100px"
        ></el-input>
      </el-form-item>
      <el-form-item label="纬度" prop="lat">
        <el-input
          v-model="formObj.lat"
          style="width: 100px"
        ></el-input>
      </el-form-item> -->
          <el-form-item>
            <el-button type="primary" @click="searchKeyword"
              >搜索</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="12">
        <div v-if="searchResult.address">
          <span>选择地址:</span>
          <span>{{ searchResult.address }}</span>
          <div>
            <span>{{
              searchResult.lon + ', ' + searchResult.lat
            }}</span>
            <el-divider direction="vertical" />
            <span>{{
              searchResult.gpsLon + ', ' + searchResult.gpsLat
            }}</span>
          </div>
        </div>
      </el-col>
      <el-col :span="2">
        <el-button
          :disabled="searchResult.gpsLon == undefined"
          type="success"
          @click="submit"
          >选择</el-button
        >
      </el-col>
    </el-row>
    <div id="mapContainer"></div>
  </el-dialog>
</template>
 
<script>
import { shallowRef } from 'vue';
import AMapLoader from '@amap/amap-jsapi-loader';
// import { AMap, onMapMounted } from './baseMap';
import baseMapUtil from './baseMapUtil.js';
 
// var map;
var AMap;
var geocoder;
var inited = false;
// onMapMounted(() => {
//   AMap.plugin('AMap.Geocoder', function () {
//     geocoder = new AMap.Geocoder({
//       city: '上海' // city 指定进行编码查询的城市,支持传入城市名、adcode 和 citycode
//     });
 
//     // 使用geocoder做地理/逆地理编码
//   });
// });
export default {
  setup() {
    const map = shallowRef(null);
    return {
      map
    };
  },
  props: {
    show: Boolean
  },
  data() {
    return {
      dialogShow: false,
      formObj: {},
      searchResult: {}
    };
  },
  emits: ['update:show', 'onSubmit'],
  watch: {
    show(val) {
      this.dialogShow = val;
    },
    dialogShow(val) {
      if (val) {
        this.mapInit();
      } else {
        this.formObj = {};
        this.searchResult = {};
        this.map.destroy();
      }
      this.$emit('update:show', val);
    }
  },
  methods: {
    mapInit() {
      // if (!inited) {
      AMapLoader.load({
        key: 'c55f27799afbfa69dc5a3fad90cafe51', // 申请好的Web端开发者Key,首次调用 load 时必填
        version: '2.0', // 指定要加载的 JS API 的版本,缺省时默认为 1.4.15
        plugins: ['AMap.Geocoder'] // 需要使用的的插件列表,如比例尺'AMap.Scale'等
      }).then((_AMap) => {
        AMap = _AMap;
        this.map = new AMap.Map('mapContainer', {
          rotateEnable: true,
          pitchEnable: true,
          alwaysRender: false,
          showLabel: true,
          showBuildingBlock: true,
          // mapStyle: 'amap://styles/e1e78509de64ddcd2efb4cb34c6fae2a',
          // features: ['bg', 'road'],
          pitch: 0, // 地图俯仰角度,有效范围 0 度- 83 度
          viewMode: '2D', // 地图模式
          resizeEnable: true,
          center: [121.6039283, 31.25295567],
          zooms: [3, 18],
          zoom: 14
        });
        geocoder = new AMap.Geocoder({
          city: '上海' // city 指定进行编码查询的城市,支持传入城市名、adcode 和 citycode
        });
        this.map.on('click', (ev) => {
          // this.formObj.lon = ev.lnglat.getLng();
          // this.formObj.lat = ev.lnglat.getLat();
          this.map.clearMap();
          const marker = new AMap.Marker({
            position: ev.lnglat
          });
          this.map.add(marker);
 
          geocoder.getAddress(ev.lnglat, (status, result) => {
            if (status === 'complete' && result.info === 'OK') {
              this.searchResult.address =
                result.regeocode.formattedAddress;
              this.searchResult.lon = ev.lnglat.getLng();
              this.searchResult.lat = ev.lnglat.getLat();
              const [gpsLon, gpsLat] = baseMapUtil.gcj02towgs84(
                this.searchResult.lon,
                this.searchResult.lat
              );
              this.searchResult.gpsLon = gpsLon;
              this.searchResult.gpsLat = gpsLat;
            }
          });
        });
      });
      // inited = true;
      // }
    },
    searchKeyword() {
      const keyWord = this.formObj.address;
      this.map.clearMap();
      geocoder.getLocation(keyWord, (status, result) => {
        if (status === 'complete' && result.info === 'OK') {
          const geocode = result.geocodes[0];
          this.searchResult.address = geocode.formattedAddress;
 
          this.searchResult.lon = geocode.location.getLng();
          this.searchResult.lat = geocode.location.getLat();
          const [gpsLon, gpsLat] = baseMapUtil.gcj02towgs84(
            this.searchResult.lon,
            this.searchResult.lat
          );
          this.searchResult.gpsLon = gpsLon;
          this.searchResult.gpsLat = gpsLat;
 
          const marker = new AMap.Marker({
            position: geocode.location
          });
          this.map.add(marker);
          this.map.setFitView(marker);
        }
      });
    },
    submit() {
      this.$emit('onSubmit', this.searchResult);
      this.dialogShow = false;
    }
  }
  // updated() {
  //   this.mapInit();
  // }
};
</script>
<style>
#mapContainer {
  position: relative;
  width: 100%;
  height: 60vh;
  z-index: 0px;
  border-radius: var(--el-border-radius-round);
  box-shadow: var(--el-box-shadow);
}
</style>