fuchengshen
2022-07-15 dfdcca599b595c7340b31ee7cd5878f29a3268eb
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//poi查询按钮 控件
class PoiQueryButton extends mars3d.control.BaseControl {
  get parentContainer() {
    return document.getElementsByClassName("cesium-viewer-toolbar")[0];
  }
 
  /**
   * 创建_container控件容器对象的方法,
   * 只会调用一次
   * @return {void}  无
   * @private
   */
  _mountedHook() {
    // 初始化页面
    this.initQueryUI();
 
    //查询控制器
    this.queryPoi = new mars3d.query.GaodePOI();
 
    //创建矢量数据图层
    this.graphicLayer = new mars3d.layer.GraphicLayer();
    this._map.addLayer(this.graphicLayer);
 
    //鼠标单击后的信息面板弹窗
    this.graphicLayer.bindPopup(function (event) {
      let item = event.graphic.attr;
      if (!item) {
        return;
      }
      var inHtml = `<div class="mars-popup-titile"><a href="https://www.amap.com/detail/${item.id}"  target="_black" style="color: #ffffff; ">${item.name}</a></div><div class="mars-popup-content" >`;
 
      var phone = $.trim(item.tel);
      if (phone != "") {
        inHtml += "<div><label>电话</label>" + phone + "</div>";
      }
 
      var dz = $.trim(item.address);
      if (dz != "") {
        inHtml += "<div><label>地址</label>" + dz + "</div>";
      }
 
      if (item.type) {
        var fl = $.trim(item.type);
        if (fl != "") {
          inHtml += "<div><label>类别</label>" + fl + "</div>";
        }
      }
      inHtml += "</div>";
 
      return inHtml;
    });
  }
 
  clear() {
    var ulList = this.poiButtonResult.querySelector(".searchResults");
    var gaodesousuo = this.poiButtonResult.querySelector(".gaodesousuo");
    if (ulList) {
      ulList.remove();
    }
    if (this.resultNextPages) {
      this.resultNextPages.remove();
    }
    if (gaodesousuo) {
      gaodesousuo.remove();
    }
    if (this.graphicLayer) {
      this.graphicLayer.clear();
    }
  }
 
  //初始化所有相关UI
  initQueryUI() {
    //高度值获取
    this._container = mars3d.DomUtil.create("div", "cesium-button cesium-toolbar-button");
    this._container.style.display = "inline-block";
    this._container.setAttribute("title", this.options.title || "POI查询");
 
    mars3d.DomUtil.createSvg(
      33,
      33,
      "M29.772,26.433l-7.126-7.126c0.96-1.583,1.523-3.435,1.524-5.421C24.169,8.093,19.478,3.401,13.688,3.399C7.897,3.401,3.204,8.093,3.204,13.885c0,5.789,4.693,10.481,10.484,10.481c1.987,0,3.839-0.563,5.422-1.523l7.128,7.127L29.772,26.433zM7.203,13.885c0.006-3.582,2.903-6.478,6.484-6.486c3.579,0.008,6.478,2.904,6.484,6.486c-0.007,3.58-2.905,6.476-6.484,6.484C10.106,20.361,7.209,17.465,7.203,13.885z",
      this._container
    );
 
    //鼠标移入移出
    let cacheTarget;
    this._container.addEventListener("mouseover", (e) => {
      //缓存,提高效率
      if (cacheTarget == this.uuid) {
        return;
      }
      cacheTarget = this.uuid;
 
      // console.log("mouseover");
      if (this.toolSearch.style.display !== "block") {
        this.toolSearchNoShow("block");
        this.toolActive();
        mars3d.DomUtil.addClass(this._container, "queryPoiButton");
 
        this._container.style.height = this.parentContainer.offsetHeight + 40 + "px";
        this.poiButtonResult.style.height = this.parentContainer.offsetHeight - 10 + "px";
      }
    });
    this._container.addEventListener("mouseout", (e) => {
      cacheTarget = null;
      // console.log("mouseout");
      var queryVal = this.toolSearch.querySelector(".searchInput").value;
      if (queryVal.length == 0) {
        this.clear();
        this.toolSearchNoShow("none");
        mars3d.DomUtil.removeClass(this._container, "queryPoiButton");
        this._container.style.height = "";
      }
    });
 
    // input面板,在queryPoiButton下面
    this.toolSearch = mars3d.DomUtil.create("div", "toolSearch");
    this._container.appendChild(this.toolSearch);
 
    // 搜寻结果,在mars3dContainer面板下面
    this.poiButtonResult = mars3d.DomUtil.create("div", "poiButtonResult");
    this._container.appendChild(this.poiButtonResult);
    this.toolSearchNoShow("none");
 
    // 创建input输入框
    var textInput = mars3d.DomUtil.create("input", "searchInput");
    textInput.type = "search";
    textInput.setAttribute("placeholder", "请输入地址...");
    this.toolSearch.appendChild(textInput);
 
    // input的单击事件
    var deleteInput = mars3d.DomUtil.create("div", "deleteInput");
    this.toolSearch.appendChild(deleteInput);
 
    this.addElementP(deleteInput, "×", () => {
      this.toolSearch.querySelector(".searchInput").value = "";
      this.clear();
      this.toolSearchNoShow("none");
      mars3d.DomUtil.removeClass(this._container, "queryPoiButton");
      this._container.style.height = "";
      cacheTarget = null;
    });
 
    // 绑定change事件
    var timetik;
 
    textInput.addEventListener("input", () => {
      this.clear();
      clearTimeout(timetik);
      timetik = setTimeout(() => {
        var queryVal = this.toolSearch.querySelector(".searchInput").value;
        if (queryVal.length !== 0) {
          deleteInput.style.display = "block";
          this.autoTip(queryVal);
        }
      }, 250);
    });
 
    //绑定回车键
    textInput.addEventListener("keydown", (event) => {
      if (event.keyCode == "13") {
        clearTimeout(timetik);
        // 让change事件执行完成之后,在执行以下操作
        timetik = setTimeout(() => {
          this.clear();
          this.showPages = 1;
          this.strartQueryPOI();
        }, 250);
      }
    });
  }
  toolActive() {
    this.toolSearch.style.display = "block";
    let searchInput = this.toolSearch.querySelector(".searchInput");
    searchInput.focus();
    if (document.activeElement.tagName == "INPUT" && searchInput.value == "") {
      return;
    }
    this.clear();
    this.showPages = 1;
    this.strartQueryPOI();
  }
 
  // 根据输入框内容,查询显示列表
  strartQueryPOI() {
    var text = this.toolSearch.querySelector(".searchInput").value;
    if (text.trim().length == 0) {
      haoutil.msg("请输入搜索关键字!");
      return;
    }
    //输入经纬度数字时
    if (this.isLonLat(text)) {
      this.centerAtLonLat(text);
      return;
    }
    this.queryTextByServer(text);
  }
 
  queryTextByServer(text) {
    this.queryPoi.queryText({
      text: text,
      count: 10,
      page: this.showPages - 1,
      success: (result) => {
        // console.log("文字搜索", result);
        var pois = result.list;
        if (pois.length > 0) {
          result.list.forEach((item, index) => {
            if (!item.x || !item.y) {
              return;
            }
            // 在地图上将搜寻的结果展现为矢量数据
            var graphic = new mars3d.graphic.PointEntity({
              id: item.id,
              position: [item.x, item.y],
              style: {
                name: item.name,
                pixelSize: 10,
                color: "#3388ff",
                outline: true,
                outlineColor: "#ffffff",
                outlineWidth: 2,
                scaleByDistance: new Cesium.NearFarScalar(1000, 1, 1000000, 0.1),
                clampToGround: true, //贴地
                visibleDepth: false, //是否被遮挡
 
                highlight: {
                  type: mars3d.EventType.click,
                  color: "#ff0000",
                },
 
                label: {
                  text: item.name,
                  font_size: 20,
                  color: "rgb(240,255,255)",
                  outline: true,
                  outlineWidth: 2,
                  outlineColor: Cesium.Color.BLACK,
                  horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                  verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                  pixelOffsetY: -10, //偏移量
                  distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 200000),
                  clampToGround: true, //贴地
                  visibleDepth: false, //是否被遮挡
                },
              },
              attr: item,
            });
            this.graphicLayer.addGraphic(graphic);
          });
          this.graphicLayer.flyTo();
          console.log(result);
          this.showDifferentPagesResult(result);
        }
      },
    });
  }
 
  flyTo(item) {
    var graphic = this.graphicLayer.getGraphicById(item.id);
    if (graphic == null) {
      window.toastr.warning(item.name + " 无经纬度坐标信息!");
      return;
    }
 
    graphic.openHighlight();
 
    graphic.flyTo({
      radius: 2000,
      complete: () => {
        graphic.openPopup();
      },
    });
  }
 
  //构造查询结果的面板Html
  showDifferentPagesResult(result) {
    this.poiButtonResult.innerHTML = "";
 
    // 页面上显示结果li列表
    var resultDiv = document.createElement("div");
    resultDiv.className = "searchResults";
 
    var suggestionsList = document.createElement("ul");
    resultDiv.appendChild(suggestionsList);
 
    this.poiButtonResult.appendChild(resultDiv);
 
    result.list.forEach((item, index) => {
      var name = item.name;
      if (!item.x || !item.y) {
        return;
      }
 
      var number;
      if (this.showPages > 1) {
        number = (this.showPages - 1) * 10 + index + 1;
      } else {
        number = index + 1;
      }
      var suggestions = document.createElement("li");
      var resultList = document.createTextNode(number + ". " + name);
 
      suggestionsList.appendChild(suggestions);
      suggestions.appendChild(resultList);
      suggestions.addEventListener("click", () => {
        this.flyTo(item);
      });
    });
    var allPages = Math.ceil(result.allcount / 10); // 全部的页数 10 = result.count
    this.resultNextPages = document.createElement("div");
    this.resultNextPages.className = "resultNextPages";
    this.poiButtonResult.appendChild(this.resultNextPages);
 
    // 共加载条数
    this.addElementP(this.resultNextPages, "共加载了" + result.allcount + "条", null);
 
    // 展示的页数
    this.addElementP(this.resultNextPages, this.showPages + "/" + allPages + "页", null);
 
    // 首页
    this.addElementP(this.resultNextPages, "首页", () => {
      this.showPages = 1;
      this.pagesClickToPages(this.resultNextPages);
    });
 
    // 上一页
    this.addElementP(this.resultNextPages, "<", () => {
      if (this.showPages == 1) {
        haoutil.msg("当前已是第一页!");
        return;
      }
      this.showPages--;
      this.pagesClickToPages(this.resultNextPages);
    });
 
    // 下一页
    this.addElementP(this.resultNextPages, ">", () => {
      if (this.showPages >= allPages) {
        haoutil.msg("当前已是最后一页!");
        return;
      }
      this.showPages++;
      this.pagesClickToPages(this.resultNextPages); //查询结果
    });
  }
 
  // 添加p元素
  addElementP(parentElement, chilidWord, callback) {
    var allResult = document.createElement("p");
    var allResultWord = document.createTextNode(chilidWord);
    parentElement.appendChild(allResult); //添加p元素
 
    allResult.appendChild(allResultWord); // 给p元素添加内容
 
    allResult.addEventListener("click", callback);
  }
  // 点击上、下一页,清空当前页
  pagesClickToPages(allPages) {
    if (allPages) {
      this.addElementP(this.poiButtonResult, this.showPages + "/" + allPages + "页");
    }
    if (this.graphicLayer) {
      this.graphicLayer.clear();
    }
    this.strartQueryPOI();
  }
 
  autoTip(text) {
    this.queryPoi.autoTip({
      text: text,
      success: (result) => {
        var pois = result.list;
        let gaodesousuo = this.poiButtonResult.querySelector(".gaodesousuo");
        if (gaodesousuo) {
          gaodesousuo.remove();
        }
        var resultDiv = document.createElement("div");
        resultDiv.className = "searchResults gaodesousuo";
 
        var suggestionsList = document.createElement("ul");
        resultDiv.appendChild(suggestionsList);
 
        this.poiButtonResult.appendChild(resultDiv);
 
        if (pois.length > 0) {
          result.list.forEach((item) => {
            var name = item.name;
 
            var suggestions = document.createElement("li");
            var resultList = document.createTextNode(name);
            var fa_search = document.createElement("span");
            fa_search.className = "fa fa-search";
            suggestions.appendChild(fa_search);
 
            suggestionsList.appendChild(suggestions);
            suggestions.appendChild(resultList);
            suggestions.addEventListener("click", () => {
              this.toolSearch.querySelector(".searchInput").value = name;
 
              this.showPages = 1;
              this.queryTextByServer(name);
            });
          });
        } else {
          resultDiv.style.display = "none";
        }
      },
    });
  }
 
  toolSearchNoShow(val) {
    this.toolSearch.style.display = val;
    this.poiButtonResult.style.display = val;
  }
 
  //===================坐标定位处理========================
  isLonLat(text) {
    var reg = /^-?((0|1?[0-7]?[0-9]?)(([.][0-9]*)?)|180(([.][0]*)?)),-?((0|[1-8]?[0-9]?)(([.][0-9]*)?)|90(([.][0]*)?))$/; /*定义验证表达式*/
    return reg.test(text); /*进行验证*/
  }
  centerAtLonLat(text) {
    var arr = text.split(",");
    if (arr.length != 2) {
      return;
    }
 
    var jd = Number(arr[0]);
    var wd = Number(arr[1]);
    if (isNaN(jd) || isNaN(wd)) {
      return;
    }
 
    this.map.setCameraView({ x: jd, y: wd, minz: 2500 });
 
    //添加实体
    var graphic = new mars3d.graphic.PointEntity({
      position: Cesium.Cartesian3.fromDegrees(jd, wd),
      style: {
        color: "#3388ff",
        pixelSize: 10,
        outline: true,
        outlineColor: "#ffffff",
        outlineWidth: 2,
        scaleByDistance: new Cesium.NearFarScalar(1000, 1, 1000000, 0.1),
        clampToGround: true, //贴地
        visibleDepth: false, //是否被遮挡
      },
    });
    this.graphicLayer.addGraphic(graphic);
 
    graphic.bindPopup(`<div class="mars-popup-titile">坐标定位</div>
              <div class="mars-popup-content" >
                <div><label>经度</label> ${jd}</div>
                <div><label>纬度</label>${wd}</div>
              </div>`);
 
    setTimeout(() => {
      graphic.openPopup();
    }, 3000);
  }
}