quanyawei
2024-01-11 1b3f8b875ed16cad81abf8f69e5f347561c76085
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
<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="visible"
      top="30px"
      width="1000px"
      center
      :before-close="close"
      @closed="close"
    >
      <div
        v-if="visible"
        class="mapBox"
      >
        <div
          class="searchBox"
        >
          <el-input
            id="tipinput"
            v-model="placeSearchName"
            :disabled="$parent.mapType!=='edit'"
            prefix-icon="el-icon-search"
            placeholder="请输入地址"
            class="input-with-select"
            style="margin-right: 10px"
            @keyup.enter.native="searchPlace"
          />
          <el-input
            v-model="positionInput"
            :disabled="$parent.mapType!=='edit'"
            prefix-icon="el-icon-position"
            placeholder="请输入经纬度"
            class="input-with-select"
            style="margin-right: 10px"
            @keyup.enter.native="searchAddress"
          />
          <el-button
            v-if="$parent.mapType==='edit'"
            type="primary"
            @click="search"
          >
            查询
          </el-button>
          <el-button
            v-if="$parent.mapType==='edit'"
            type="primary"
            :disabled="placeSearchName===''"
            @click="close"
          >
            确定
          </el-button>
        </div>
        <div id="mapd" />
      </div>
    </el-dialog>
  </div>
</template>
 
<script>
export default {
  props: {
    visible: { type: Boolean, required: true }
  },
  data () {
    return {
      title: '污染位置',
      map: null,
      geolocation: null,
      marker: null,
      placeSearchName: '',
      mapPlaceSearch: null,
      position: [],
      positionInput: ''
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.initMap()
    })
  },
  destroyed () {
    if (this.map) {
      this.map.destroy()
    }
  },
  methods: {
    // 初始化地图
    initMap () {
      let map = new AMap.Map('mapd', {
        resizeEnable: true,
        zooms: [3, 18],
        zoom: 15,
      })
      this.map = map
      let that = this
      AMap.plugin(['AMap.Autocomplete', 'AMap.PlaceSearch'], function () {
        // 实例化Autocomplete
        let autoOptions = {
          // city 限定城市,默认全国
          city: '全国',
          input: 'tipinput',
        }
        that.mapPlaceSearch = new AMap.PlaceSearch({
          map: map,
        })
        // that.geolocation = new AMap.Geolocation({})
        let autoComplete = new AMap.Autocomplete(autoOptions)
        AMap.event.addListener(autoComplete, 'select', function (e) {
          that.map.clearMap()
          this.position = []
          console.log('eee', e.poi.name)
          console.log('eee', that.placeSearchName)
          that.placeSearchName = e.poi.name
          // that.mapPlaceSearch.search(e.poi.name)
        })
        AMap.event.addListener(that.mapPlaceSearch, 'markerClick', function (e) {
          that.position = [e.data.location.lng, e.data.location.lat]
          that.positionInput = e.data.location.lng + ',' + e.data.location.lat
          this.placeSearchName = e.data.cityname + e.data.adname + e.data.address
          console.log(e)
          console.log(this.placeSearchName)
          document.getElementById('tipinput').value = e.data.cityname + e.data.adname + e.data.address
        })
      })
      if (this.$parent.mapType === 'edit') {
        map.on('click', function (ev) {
          that.map.clearMap()
          that.position = [ev.lnglat.lng, ev.lnglat.lat]
          that.positionInput = ev.lnglat.lng + ',' + ev.lnglat.lat
          that.marker = new AMap.Marker({
            position: that.position,
            offset: new AMap.Pixel(-13, -30)
          })
          that.marker.setMap(map)
          that.getAddress(that.position)
        })
      } else {
        this.map.clearMap()
        that.marker = new AMap.Marker({
          map: map,
          position: that.$parent.position
        })
        that.positionInput = that.toFixed(that.$parent.position[0], 8) + ',' + that.toFixed(that.$parent.position[1], 8)
        that.marker.setMap(map)
        that.map.setCenter(that.$parent.position)
        that.marker.setAnimation('AMAP_ANIMATION_BOUNCE')
        this.placeSearchName = this.$parent.address
      }
    },
    toFixed (str, decimalPlaces) {
      const num = parseFloat(str)
      return num.toFixed(decimalPlaces)
    },
    getAddress (position) {
      AMap.plugin('AMap.Geocoder', () => {
        const geocoder = new AMap.Geocoder({})
        geocoder.getAddress(position, (status, result) => {
          if (status === 'complete' && result.info === 'OK') {
            this.placeSearchName = result.regeocode.formattedAddress
            document.getElementById('tipinput').value = result.regeocode.formattedAddress
          }
        })
      })
    },
    search () {
      console.log('search', this.placeSearchName)
      if (this.positionInput) {
        this.searchAddress()
        return
      }
      if (this.placeSearchName) {
        this.searchPlace(this.placeSearchName)
      }
    },
    searchPlace () {
      this.mapPlaceSearch.search(this.placeSearchName)
    },
    searchAddress () {
      this.map.clearMap()
      this.marker = new AMap.Marker({
        map: this.map,
        position: this.positionInput.split(',')
      })
      this.marker.setMap(this.map)
      this.map.setCenter(this.positionInput.split(','))
      this.getAddress(this.positionInput.split(','))
    },
    close () {
      this.map.destroy()
      this.$emit('addressAndLnt', this.position, this.placeSearchName)
      this.$emit('update:visible', false)
    }
  }
}
</script>
 
<style scoped lang="scss">
/deep/.el-dialog__body {
  height: 600px;
  .mapBox {
    width: 100%;
    height: 100%;
    padding-bottom: 30px;
    position: relative;
    .searchBox{
      position: absolute;
      display: flex;
      z-index: 100;
       width: 100%;
    }
  }
}
#mapd {
  width: 100%;
  height: 100%;
  resize: both;
  z-index: 0;
}
</style>