UNPKG

@vtx/cs-map

Version:

React components for Vortex

265 lines (255 loc) 12.8 kB
"use strict"; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _cesium = require("cesium"); require("./index.less"); var _util = _interopRequireDefault(require("./util")); var _class; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); } function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; } function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); } function _classApplyDescriptorSet(receiver, descriptor, value) { if (descriptor.set) { descriptor.set.call(receiver, value); } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } descriptor.value = value; } } function _classStaticPrivateMethodGet(receiver, classConstructor, method) { _classCheckPrivateStaticAccess(receiver, classConstructor); return method; } function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } } /** * 信息工具。 * * @alias InfoTool * @constructor * @param {viewer} viewer Cesium视窗。 */ var _element = /*#__PURE__*/new WeakMap(); var InfoTool = /*#__PURE__*/function () { function InfoTool(options) { _classCallCheck(this, InfoTool); _classPrivateFieldInitSpec(this, _element, { writable: true, value: void 0 }); _defineProperty(this, "viewer", void 0); this.viewer = options.map; // 在Cesium容器中添加元素 _classPrivateFieldSet(this, _element, document.createElement('div')); } /** * 添加。 * @param {Object} options 选项。 * @param {Element} options.element 弹窗元素。 * @param {Cartesian2|Cartesian3} options.position 点击位置。 * @param {Cesium3DTileFeature} [options.inputFeature] 模型要素。 * @param {String} options.type 类型(默认值为default,即任意点击模式;如果设置为info,即信息模式,只有点击Feature才会响应)。 * @param {String} options.content 内容(只有类型为default时才起作用)。 * @param {Function} callback 回调函数。 */ _createClass(InfoTool, [{ key: "add", value: function add(options) { var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; this.element = options.element; // 判断参数为空返回 if (!options) { return; } // 点 var position, cartesian2d, cartesian3d, inputFeature; if (options instanceof _cesium.Cesium3DTileFeature) { inputFeature = options; options = {}; } else { if (options instanceof _cesium.Cartesian2 || options instanceof _cesium.Cartesian3) { position = options; options = {}; } else { position = options.position; inputFeature = options.inputFeature; } // 判断点位为空返回 if (!position) { return; } if (position instanceof _cesium.Cartesian2) { // 二维转三维 // 如果支持拾取模型则取模型值 cartesian3d = this.viewer.scene.pickPositionSupported && (0, _cesium.defined)(this.viewer.scene.pick(options.position)) ? this.viewer.scene.pickPosition(position) : this.viewer.camera.pickEllipsoid(position, this.viewer.scene.globe.ellipsoid); cartesian2d = position; } else { cartesian3d = position; cartesian2d = _cesium.SceneTransforms.wgs84ToWindowCoordinates(this.viewer.scene, cartesian3d); } // 判断点位为空返回 if (!cartesian3d) { return; } } var that = this; // 1.组织信息 var info = ''; if (options.type === 'info') { // 拾取要素 var feature = inputFeature || this.viewer.scene.pick(cartesian2d); // 判断拾取要素为空返回 if (!(0, _cesium.defined)(feature)) { this.remove(); return; } if (feature instanceof _cesium.Cesium3DTileFeature) { var _feature$getPropertyN; // 3dtiles var propertyNames = (feature === null || feature === void 0 || (_feature$getPropertyN = feature.getPropertyNames) === null || _feature$getPropertyN === void 0 ? void 0 : _feature$getPropertyN.call(feature)) || []; var length = propertyNames.length; for (var i = 0; i < length; ++i) { var propertyName = propertyNames[i]; info += '"' + (propertyName + '": "' + feature.getProperty(propertyName)) + '",\n'; } } else if (feature) { // Entity var properties = feature.properties; if (properties) { var _propertyNames = properties._propertyNames; var _length = _propertyNames.length; for (var _i = 0; _i < _length; ++_i) { var _propertyName = _propertyNames[_i]; info += '"' + (_propertyName + '": "' + properties[_propertyName]._value) + '",\n'; } } } } else { options.content && (info = options.content); } // 2.生成特效 // 添加之前先移除 this.remove(); // if (!info) { // return; // } options.position = cartesian3d; options.element = options.element || _classPrivateFieldGet(this, _element); _classStaticPrivateMethodGet(InfoTool, InfoTool, _createInfoTool).call(InfoTool, this.viewer, options, function () { _util["default"].setInnerText(options.element, info); typeof callback === 'function' && callback(); }); } }, { key: "show", value: function show() { _util["default"].setCss(this.element, 'opacity', '1'); _util["default"].setCss(this.element, 'z-index', '1'); _util["default"].setCss(_classPrivateFieldGet(this, _element), 'opacity', '1'); } /** * 移除。 */ }, { key: "remove", value: function remove() { _util["default"].setCss(this.element, 'opacity', '0'); _util["default"].setCss(this.element, 'z-index', '-1'); _util["default"].setCss(_classPrivateFieldGet(this, _element), 'opacity', '0'); } }]); return InfoTool; }(); _class = InfoTool; /** * 创建一个动态实体弹窗。 * * @param {viewer} viewer Cesium视窗。 * @param {Number} options 选项。 * @param {Cartesian3} options.position 弹出位置。 * @param {HTMLElement} options.element 弹出窗元素容器。 * @param {Function} callback 回调函数。 * @ignore */ function _createInfoTool(viewer, options) { var callback = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined; var cartographic = _cesium.Cartographic.fromCartesian(options.position); var lon = _cesium.Math.toDegrees(cartographic.longitude); //.toFixed(5); var lat = _cesium.Math.toDegrees(cartographic.latitude); //.toFixed(5); // 回调 callback(); // 添加div弹窗 setTimeout(function () { _classStaticPrivateMethodGet(_class, _class, _popup).call(_class, viewer, options.element, lon, lat, cartographic.height, // options.geometryType, options.offset); }, 100); } /** * 弹出HTML元素弹窗。 * @param {viewer} viewer Cesium视窗。 * @param {Element|HTMLElement} element 弹窗元素。 * @param {Number} lon 经度。 * @param {Number} lat 纬度。 * @param {Number} height 高度。 * @ignore */ function _popup(viewer, element, lon, lat, height, offset) { setTimeout(function () { // 设置元素效果 _util["default"].setCss(element, 'opacity', '1'); _util["default"].setCss(element, 'z-index', '0'); }, 100); var divPosition = _cesium.Cartesian3.fromDegrees(lon, lat, height); var x = (offset === null || offset === void 0 ? void 0 : offset.x) || 0; var y = (offset === null || offset === void 0 ? void 0 : offset.y) || 0; _classStaticPrivateMethodGet(_class, _class, _hookToGlobe).call(_class, viewer, element, divPosition, [-4 + x, y], true); viewer.scene.requestRender(); } /** * 将HTML弹窗挂接到地球上。 * * @param {viewer} viewer Cesium视窗。 * @param {Element} element 弹窗元素。 * @param {Cartesian3} position 地图坐标点。 * @param {Array} offset 偏移。 * @param {Boolean} hideOnBehindGlobe 当元素在地球背面会自动隐藏,以减轻判断计算压力。 * @ignore */ function _hookToGlobe(viewer, element, position, offset, hideOnBehindGlobe) { var scene = viewer.scene, camera = viewer.camera; var cartesian2 = new _cesium.Cartesian2(); var height = viewer._lastHeight; var _style = {}; scene.preRender.addEventListener(function () { var canvasPosition = scene.cartesianToCanvasCoordinates(position, cartesian2); // 笛卡尔坐标到画布坐标 if ((0, _cesium.defined)(canvasPosition)) { var left = parseInt(canvasPosition.x + offset[0]); var bottom = parseInt(height - canvasPosition.y + offset[1]); if (_style.left) { _style.left = _style.left - left > 1 || _style.left - left < -1 ? left : _style.left; } else { _style.left = left; } _style.bottom = bottom; _util["default"].setCss(element, 'left', _style.left + 'px'); _util["default"].setCss(element, 'bottom', _style.bottom + 'px'); // 是否在地球背面隐藏 if (hideOnBehindGlobe) { var cameraPosition = camera.position; var _height = scene.globe.ellipsoid.cartesianToCartographic(cameraPosition).height; _height += scene.globe.ellipsoid.maximumRadius; if (!(_cesium.Cartesian3.distance(cameraPosition, position) > _height)) { // util.setCss(element, 'display', 'flex'); } else { // util.setCss(element, 'display', 'none'); } } } }); } var _default = exports["default"] = InfoTool; //# sourceMappingURL=index.js.map