fuchengshen
2022-07-09 3a25c05b5ab837714f30469cdcb1e2a77d8d7f6c
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
// import kgUtil from "kml-geojson"
 
/**
 * 通过转geojson方式 加载kml和kmz文件。
 * kgUtil使用需要引入 ../lib/kml/kml-geojson.js文件
 */
class Kml2JsonLayer extends mars3d.layer.GeoJsonLayer {
  /**
   * 加载新数据 或 刷新数据
   *
   * @param {Object} [newOptions] 新设定的参数,会与类的构造参数合并。
   * @param {String} [newOptions.url] geojson文件或服务url地址
   * @param {Object} [newOptions.data] geojson格式规范数据对象,与url二选一即可。
   * @param {Object} [newOptions.类参数] 包含当前类支持的所有参数
   * @param {BaseGraphicLayer.ConstructorOptions} [newOptions.通用参数] 包含父类支持的所有参数
   * @return {this} 当前对象本身,可以链式调用
   */
  load(newOptions) {
    if (newOptions) {
      if (Cesium.defaultValue(newOptions.clear, true)) {
        delete this.options.url
        delete this.options.data
      }
      this.clear()
 
      this.options = {
        ...this.options,
        ...newOptions
      }
    }
 
    if (this.options.url) {
      kgUtil
        .toGeoJSON(this.options.url)
        .then((data) => {
          if (this._state === mars3d.State.REMOVED) {
            return
          }
          this._load_data(data)
        })
        .catch(function (error) {
          this.showError("服务出错", error)
        })
    } else if (this.options.data) {
      kgUtil
        .toGeoJSON(this.options.data)
        .then((data) => {
          if (this._state === mars3d.State.REMOVED) {
            return
          }
          this._load_data(data)
        })
        .catch(function (error) {
          this.showError("服务出错", error)
        })
    } else {
      if (newOptions) {
        console.warn("Kml2JsonLayer:没有传入 url 或 data 参数,请确认是否有误。")
      }
    }
  }
}
 
mars3d.layer.Kml2JsonLayer = Kml2JsonLayer
 
// 注册下
mars3d.LayerUtil.register("kml2json", Kml2JsonLayer)