Kml2JsonLayer.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * 通过转geojson方式 加载kml和kmz文件。
  3. * kgUtil使用需要引入 ../lib/kml/kml-geojson.js文件
  4. */
  5. class Kml2JsonLayer extends mars3d.layer.GeoJsonLayer {
  6. /**
  7. * 加载新数据 或 刷新数据
  8. *
  9. * @param {Object} [newOptions] 新设定的参数,会与类的构造参数合并。
  10. * @param {String} [newOptions.url] geojson文件或服务url地址
  11. * @param {Object} [newOptions.data] geojson格式规范数据对象,与url二选一即可。
  12. * @param {Object} [newOptions.类参数] 包含当前类支持的所有参数
  13. * @param {BaseGraphicLayer.ConstructorOptions} [newOptions.通用参数] 包含父类支持的所有参数
  14. * @return {this} 当前对象本身,可以链式调用
  15. */
  16. load(newOptions) {
  17. if (newOptions) {
  18. if (Cesium.defaultValue(newOptions.clear, true)) {
  19. delete this.options.url;
  20. delete this.options.data;
  21. }
  22. this.clear();
  23. this.options = {
  24. ...this.options,
  25. ...newOptions,
  26. };
  27. }
  28. if (this.options.url) {
  29. kgUtil
  30. .toGeoJSON(this.options.url)
  31. .then((data) => {
  32. if (this._state == mars3d.State.REMOVED) {
  33. return;
  34. }
  35. this._load_data(data);
  36. })
  37. .otherwise(function (error) {
  38. this.showError("服务出错", error);
  39. });
  40. } else if (this.options.data) {
  41. kgUtil
  42. .toGeoJSON(this.options.data)
  43. .then((data) => {
  44. if (this._state == mars3d.State.REMOVED) {
  45. return;
  46. }
  47. this._load_data(data);
  48. })
  49. .otherwise(function (error) {
  50. this.showError("服务出错", error);
  51. });
  52. } else {
  53. if (newOptions) {
  54. console.warn("Kml2JsonLayer:没有传入 url 或 data 参数,请确认是否有误。");
  55. }
  56. }
  57. }
  58. }
  59. mars3d.layer.Kml2JsonLayer = Kml2JsonLayer;
  60. //注册下
  61. mars3d.LayerUtil.register("kml2json", Kml2JsonLayer);