UNPKG

qwc2-lts

Version:
260 lines 12.6 kB
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); } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /** * Copyright 2015 GeoSolutions Sas * Copyright 2016-2024 Sourcepole AG * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. */ import ol from 'openlayers'; import CoordinatesUtils from '../../utils/CoordinatesUtils'; import LocaleUtils from '../../utils/LocaleUtils'; import './OlLocate.css'; var createPopup = function createPopup() { var pop = document.createElement('div'); pop.setAttribute("class", "ol-popup"); var popDismis = document.createElement('a'); popDismis.setAttribute("class", "ol-popup-close-btn"); popDismis.setAttribute("href", "#close"); popDismis.innerHTML = "x"; var popCntWrap = document.createElement('div'); popCntWrap.setAttribute("class", "ol-popup-cnt-wrapper"); var popCnt = document.createElement('div'); popCnt.setAttribute("class", "ol-popup-cnt"); popCntWrap.appendChild(popCnt); var popTipWrap = document.createElement('div'); popTipWrap.setAttribute("class", "ol-popup-tip-wrapper"); var popTip = document.createElement('div'); popTip.setAttribute("class", "ol-popup-tip"); popTipWrap.appendChild(popTip); pop.appendChild(popDismis); pop.appendChild(popCntWrap); pop.appendChild(popTipWrap); return pop; }; var OlLocate = /*#__PURE__*/function (_ol$Object) { function OlLocate(map, optOptions) { var _this; _classCallCheck(this, OlLocate); _this = _callSuper(this, OlLocate); _defineProperty(_this, "start", function () { _this.geolocate.on('error', _this.options.onLocationError, _this); _this.follow = _this.options.follow; _this.geolocate.setTracking(true); _this.layer.setMap(_this.map); _this.map.addOverlay(_this.overlay); if (_this.options.showPopup) { _this.map.on('click', _this.mapClick, _this); _this.map.on('touch', _this.mapClick, _this); } if (_this.options.stopFollowingOnDrag) { _this.map.on('pointerdrag', _this.stopFollow, _this); } if (!_this.p) { _this.set("state", "LOCATING"); _this.set("position", null); } else { _this._updatePosFt(); } }); _defineProperty(_this, "stop", function () { _this.geolocate.un('error', _this.options.onLocationError, _this); _this.geolocate.setTracking(false); _this.popup.hide = true; _this.map.removeOverlay(_this.overlay); _this.layer.setMap(null); if (_this.options.showPopup) { _this.map.un('click', _this.mapClick); _this.map.un('touch', _this.mapClick); } if (_this.options.stopFollowingOnDrag && _this.follow) { _this.map.un('pointerdrag', _this.stopFollow, _this); } _this.set("state", "DISABLED"); _this.set("position", null); }); _defineProperty(_this, "startFollow", function () { _this.follow = true; if (_this.options.stopFollowingOnDrag) { _this.map.on('pointerdrag', _this.stopFollow, _this); } if (_this.p) { _this._updatePosFt(); } }); _defineProperty(_this, "stopFollow", function () { _this.follow = false; _this.map.un('pointerdrag', _this.stopFollow, _this); _this.set("state", "ENABLED"); }); _defineProperty(_this, "updateView", function (point) { if (_this.follow) { _this.map.getView().setCenter(point.getCoordinates()); if (!_this.options.keepCurrentZoomLevel) { _this.map.getView().setZoom(_this.options.locateOptions.maxZoom); } } }); _defineProperty(_this, "onLocationError", function (err) { // eslint-disable-next-line alert(err.message); }); _defineProperty(_this, "mapClick", function (evt) { var feature = _this.map.forEachFeatureAtPixel(evt.pixel, function (ft) { return ft; }); if (feature && feature.get('id') === '_locate-pos' && _this.popup.hidden) { _this._updatePopUpCnt(); } else if (!_this.popup.hidden) { _this.popUp.hidden = true; } }); _defineProperty(_this, "setStrings", function (newStrings) { _this.options.strings = _objectSpread(_objectSpread({}, _this.options.strings), newStrings); }); _defineProperty(_this, "setProjection", function (projection) { _this.geolocate.setProjection(projection); }); _defineProperty(_this, "_updatePosFt", function () { var state = _this.get("state"); var nState = _this.follow ? "FOLLOWING" : "ENABLED"; if (nState !== state) { _this.set("state", nState); } var p = _this.geolocate.getPosition(); var wgsPos = CoordinatesUtils.reproject(p, _this.map.getView().getProjection(), "EPSG:4326"); _this.set("position", wgsPos); _this.p = p; var point = new ol.geom.Point(p); if (_this.options.drawCircle) { var accuracy = new ol.geom.Circle(point.getCoordinates(), _this.geolocate.getAccuracy()); _this.posFt.setGeometry(new ol.geom.GeometryCollection([point, accuracy])); } else { _this.posFt.setGeometry(new ol.geom.GeometryCollection([point])); } if (!_this.popup.hidden) { _this._updatePopUpCnt(); } if (_this.follow) { _this.updateView(point); } // Update only once if (!_this.options.remainActive) { _this.geolocate.setTracking(false); } }); _defineProperty(_this, "_updatePopUpCnt", function () { var distance; var unit; if (_this.options.metric) { distance = _this.geolocate.getAccuracy(); unit = _this.options.strings.metersUnit; } else { distance = Math.round(_this.geolocate.getAccuracy() * 3.2808399); unit = _this.options.strings.feetUnit; } var cnt = _this.options.strings.popup.replace("{distance}", distance); _this.popCnt.innerHTML = cnt.replace("{unit}", unit); _this.overlay.setPosition(_this.posFt.getGeometry().getGeometries()[0].getCoordinates()); _this.popup.hidden = false; }); _defineProperty(_this, "_getDefaultStyles", function () { return new ol.style.Style({ image: new ol.style.Circle({ radius: 6, fill: new ol.style.Fill({ color: 'rgba(42,147,238,0.7)' }), stroke: new ol.style.Stroke({ color: 'rgba(19,106,236,1)', width: 2 }) }), fill: new ol.style.Fill({ color: 'rgba(19,106,236,0.15)' }), stroke: new ol.style.Stroke({ color: 'rgba(19,106,236,1)', width: 2 }) }); }); _this.set("state", "DISABLED"); _this.map = map; var defOptions = { drawCircle: true, // draw accuracy circle follow: true, // follow with zoom and pan the user's location stopFollowingOnDrag: false, // if follow is true, stop following when map is dragged (deprecated) // if true locate control remains active on click even if the user's location is in view. // clicking control will just pan to location not implemented remainActive: true, locateStyle: _this._getDefaultStyles(), metric: true, onLocationError: _this.onLocationError, // keep the current map zoom level when displaying the user's location. (if 'false', use maxZoom) keepCurrentZoomLevel: false, showPopup: false, // display a popup when the user click on the inner marker strings: { metersUnit: LocaleUtils.tr("locate.metersUnit"), feetUnit: LocaleUtils.tr("locate.feetUnit"), popup: LocaleUtils.tr("locate.popup") }, locateOptions: { maximumAge: 2000, enableHighAccuracy: false, timeout: 10000, maxZoom: 18 } }; _this.options = _objectSpread(_objectSpread({}, defOptions), optOptions); _this.geolocate = new ol.Geolocation({ projection: _this.map.getView().getProjection(), trackingOptions: _this.options.locateOptions }); _this.geolocate.on('change:position', _this._updatePosFt, _this); _this.popup = createPopup(); _this.popup.hidden = true; _this.popCnt = _this.popup.getElementsByClassName("ol-popup-cnt")[0]; _this.overlay = new ol.Overlay({ element: _this.popup, positioning: 'top-center', stopEvent: false }); _this.layer = new ol.layer.Vector({ source: new ol.source.Vector({ useSpatialIndex: false }) }); _this.posFt = new ol.Feature({ geometry: _this.geolocate.getAccuracyGeometry(), name: 'position', id: '_locate-pos' }); _this.posFt.setStyle(_this.options.locateStyle); _this.layer.getSource().addFeature(_this.posFt); return _this; } _inherits(OlLocate, _ol$Object); return _createClass(OlLocate); }(ol.Object); export { OlLocate as default };