hejianhao
2025-04-16 dab2d210ca06c1faa514c6388fbd5de1ab355360
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
222
class demoMap {
  constructor() {
    this.clickDot = null
    this.searchDot = []
    this.nowCity = ''
    this.city()
  }
  init(id) {
    var map = new AMap.Map(id, { zoom: 13 });
    return map
  }
  // 当前城市
  city(fn) {
    let t = this
    AMap.plugin('AMap.CitySearch', function () {
      var citySearch = new AMap.CitySearch()
      citySearch.getLocalCity(function (status, result) {
        if (status === 'complete' && result.info === 'OK') {
          // 查询成功,result即为当前所在城市信息
          t.nowCity = result.city
          fn ? fn(result) : null
        }
      })
    })
  }
  // 设置城市
  setCity(v, map) {
    let t = this
    t.nowCity = ''
    t.search(v, e => {
      if (e.length) {
        let lng = e[0].location.lng
        let lat = e[0].location.lat
        t.geo({
          lng, lat
        }, res => {
          if (res.addressComponent) {
            t.nowCity = res.addressComponent.city
          }
        })
        if (map && e[0].location) {
          map.setCenter([lng, lat])
        }
      }
    }, false)
  }
  // 点击
  click(map, fn, dot) {
    let t = this
    map.on('click', e => {
      let os = {
        lat: e.lnglat.lat,
        lng: e.lnglat.lng,
        type: e.type,
        target: e.target
      }
      if (!dot) {
        t.clearDot(map)
        t.clickDot = t.dot(map, os)
        t.geo(os, res => {
          os.geo = res
          fn ? fn(os) : null
        })
        t.clickDot.on('click', es => {
          fn ? fn({ code: 1000, e: es.target.w.extData }) : null
        })
      } else {
        fn ? fn(os) : null
      }
    })
    return map
  }
  // 生成小点
  dot(map, opt, click) {
    opt = opt || {}
    var marker = new AMap.Marker({
      position: new AMap.LngLat(opt.lng, opt.lat),
      extData: opt,
      clickable: !click,
      title: opt.name || ''
    });
    map.add(marker);
    return marker
  }
  // 地址 <-> 经纬度 AMap.Geocoder
  geo(opt, fn) {
    let t = this
    AMap.plugin('AMap.Geocoder', function () {
      var geocoder = new AMap.Geocoder({
        // city 指定进行编码查询的城市,支持传入城市名、adcode 和 citycode
        city: t.nowCity,
        pageSize: 30
      })
 
      var search = opt.type === 'address' ? opt.search : [opt.lng, opt.lat]
 
      geocoder.getAddress(search, function (status, result) {
        if (status === 'complete' && result.info === 'OK') {
          // result为对应的地理位置详细信息
          fn ? fn(result.regeocode) : null
        }
      })
    })
  }
  // 输入提示
  input(val, fn, dot, map) {
    var t = this
    AMap.plugin('AMap.Autocomplete', function () {
      // 实例化Autocomplete
      var autoOptions = {
        //city 限定城市,默认全国
        city: t.nowCity
      }
      var autoComplete = new AMap.Autocomplete(autoOptions);
      autoComplete.search(val, function (status, result) {
        // 搜索成功时,result即是对应的匹配数据
        if (status === 'complete' && result.info === 'OK') {
          // result为对应的地理位置详细信息
          var list = result.tips || []
          fn ? fn(list) : null
          if (dot && map) {
            // 生成 点
            t.clearDot(map)
            list.forEach(v => {
              if (v.location) {
                var r = {
                  lng: v.location.lng,
                  lat: v.location.lat,
                  address: v.address,
                  name: v.name,
                  desc: v.district,
                  code: v.adcode
                }
                r.dot = t.dot(map, r)
                t.searchDot.push(r)
              }
            })
            if (t.searchDot.length) {
              map.setCenter([t.searchDot[0].lng, t.searchDot[0].lat])
            }
            t.searchClick(e => {
              fn ? fn({ code: 1000, e }) : null
            })
          }
        }
      })
    })
  }
  // 搜索
  search(val, fn, dot, map) {
    var t = this
    AMap.plugin('AMap.PlaceSearch', function () {
      var placeSearch = new AMap.PlaceSearch({
        // city 指定搜索所在城市,支持传入格式有:城市名、citycode和adcode
        city: t.nowCity,
        pageSize: 50
      })
 
      placeSearch.search(val, function (status, result) {
        // 查询成功时,result即对应匹配的POI信息
        if (status === 'complete' && result.info === 'OK') {
          var list = (result.poiList || {}).pois || []
          fn ? fn(list) : null
          if (dot && map) {
            t.clearDot(map)
            list.forEach(v => {
              if (v.location) {
                var r = {
                  lng: v.location.lng,
                  lat: v.location.lat,
                  address: v.address,
                  name: v.name,
                  type: v.type
                }
                r.dot = t.dot(map, r)
                t.searchDot.push(r)
              }
            })
            map.setCenter([t.searchDot[0].lng, t.searchDot[0].lat])
            t.searchClick(e => {
              fn ? fn({ code: 1000, e }) : null
            })
          }
        }
      })
    })
  }
  // 点 事件
  searchClick(fn) {
    let t = this
    if (t.searchDot.length) {
      t.searchDot.forEach(r => {
        r.dot.on('click', e => {
          let val = {}
          try {
            val = e.target.w.extData
            // if (val.dot) { delete val.dot }
          } catch (e) { }
          fn ? fn(val) : null
        })
      })
    }
  }
  // 删除点
  clearDot(map) {
    let t = this
    if (t.searchDot.length) {
      t.searchDot.forEach(r => {
        if (r.dot) {
          map.remove(r.dot)
        }
      })
      t.searchDot = []
    }
    if (t.clickDot) {
      map.remove(t.clickDot)
      t.clickDot = null
    }
  }
}
 
export default new demoMap()