UNPKG

leaflet-webatlastile

Version:

Leaflet plugin to use Webatlas tilecache with correct attribution and apikey's. Requires only vanilla Leaflet.

107 lines (106 loc) 5.22 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WebatlasTileLayerTypes = exports.webatlasTileLayer = exports.WebatlasTileLayer = void 0; var leaflet_1 = __importDefault(require("leaflet")); var util_1 = require("./util"); var defaultTilesets_1 = require("./constants/defaultTilesets"); var layerTypes_1 = require("./constants/layerTypes"); Object.defineProperty(exports, "WebatlasTileLayerTypes", { enumerable: true, get: function () { return layerTypes_1.WebatlasTileLayerTypes; } }); var getAttributionText_1 = require("./getAttributionText"); var defaultOptions = { mapType: 'vector', maxZoom: 20, minZoom: 0, attributionPosition: 'bottomright' }; var TILE_URL = '//waapi.webatlas.no/maptiles/tiles/{tileset}/wa_grid/{z}/{x}/{y}.{ext}?APITOKEN={apiKey}'; var getUrl = function (mapType, apiKey, tilesetList) { return util_1.doTemplate(window.location.protocol === 'https:' ? 'https:' + TILE_URL : 'http:' + TILE_URL, __assign({ apiKey: apiKey }, tilesetList[mapType])); }; var WebatlasTileLayer = /** @class */ (function (_super) { __extends(WebatlasTileLayer, _super); function WebatlasTileLayer(options) { var _this = this; var _a = __assign(__assign({}, defaultOptions), options), attributionPosition = _a.attributionPosition, apiKey = _a.apiKey, mapType = _a.mapType, tileset = _a.tileset, rest = __rest(_a, ["attributionPosition", "apiKey", "mapType", "tileset"]); var url = getUrl(mapType || layerTypes_1.WebatlasTileLayerTypes.VECTOR, apiKey, __assign(__assign({}, defaultTilesets_1.defaultTilesets), (tileset || {}))); _this = _super.call(this, url, rest) || this; _this.attributionPosition = attributionPosition; return _this; } WebatlasTileLayer.prototype.onAdd = function (map) { this._map = map; this.attributionText = ''; this._map.on('moveend', this._onMapMoved, this); if (this._map.attributionControl) { this._map.removeControl(this._map.attributionControl); //@ts-ignore this._map.attributionControl = undefined; } this._onMapMoved(); leaflet_1.default.TileLayer.prototype.onAdd.call(this, map); return this; }; WebatlasTileLayer.prototype.onRemove = function (map) { this._map.off('moveend', this._onMapMoved, this); leaflet_1.default.TileLayer.prototype.onRemove.call(this, map); return this; }; WebatlasTileLayer.prototype._onMapMoved = function () { var attributionText = getAttributionText_1.getAttributionText(this._map.getCenter(), this._map.getZoom()); //add an attribution control if the default is disabled if (!this._map.attributionControl) { this._map.attributionControl = leaflet_1.default.control .attribution({ position: this.attributionPosition }) .addTo(this._map); } //remove the previous attribution we set this._map.attributionControl.removeAttribution(this.attributionText); //set the new attribution this._map.attributionControl.addAttribution(attributionText); //store the current so that we can remove it later this.attributionText = attributionText; }; return WebatlasTileLayer; }(leaflet_1.default.TileLayer)); exports.WebatlasTileLayer = WebatlasTileLayer; exports.webatlasTileLayer = function (options) { return new WebatlasTileLayer(options); };