- /**
- @module L.PtvLayer
- **/
- L.PtvLayer = L.PtvLayer || {};
-
- /**
- Provides the PTV Background TileLayer class.
- @class L.PtvLayer.Background
- @extends L.TileLayer
- @params {XMapClient} client XMapClient object
- @params {Object} options The options object
- @params {String} [options.format] The image format used in tile requests.
- @params {String} [options.beforeSend] Function to be called before sending the request with the request object given as first parameter. The (modified) request object must be returned.
- @params {String} [options.errorTileUrl] The image to display when requests fail.
- @constructor
- **/
- L.PtvLayer.Background = L.TileLayer.extend({
-
- _client: null,
-
- /**
- Default options used in this class.
- @property options
- @type Object
- **/
- options: {
- /**
- The image format used in tile requests.
- @property options.format
- @type String
- @default "PNG"
- **/
- format: 'PNG',
-
- /**
- Function to be called before sending the request with the request object given as first parameter. The (modified) request object must be returned.
- @property options.beforeSend
- @type function
- @default null
- **/
- beforeSend: null,
-
- /**
- The image to display when requests fail.
- @property options.errorTileUrl
- @type String
- @default "tile-error.png"
- **/
- errorTileUrl: 'tile-error.png',
-
- /**
- If set to true, the tiles just won't load outside the world width (-180 to 180 longitude) instead of repeating.
- @property options.noWrap
- @type boolean
- @default true
- **/
- noWrap: true,
-
- /**
- The world bounds for PTV Maps.
- @property options.bounds
- @type L.LatLngBounds
- @default [[85.0, -178.965000], [-66.5, 178.965000]]
- **/
- bounds: new L.LatLngBounds([[85.0, -178.965000], [-66.5, 178.965000]]),
-
- /**
- Minimum zoom number.
- @property options.minZoom
- @type boolean
- @default true
- **/
- minZoom: 2
- },
-
- /**
- Constructor
- @method L.PtvLayer.Background
- @param {XMapClient} client optional XMapClient object
- @param {Object} options optional options object
- **/
- initialize: function(client, options) {
- this._client = client;
- if (typeof client !== 'object' ) {
- if (typeof XMapClient !== 'function') {
- throw Error('The XMapClient object is not accessible globally. Have you loaded "xmap-client.js" properly?');
- } else {
- this._client = new XMapClient(null, 5);
- }
- }
- L.Util.setOptions(this, options);
- },
-
- onAdd: function(map) {
- L.TileLayer.prototype.onAdd.call(this, map);
- if ( typeof this._client.cancelPendingRequests === 'function') {
- map.on('zoomstart', this._client.cancelPendingRequests);
- }
- },
-
- onRemove: function(map) {
- L.TileLayer.prototype.onAdd.call(this, map);
- if ( typeof this._client.cancelPendingRequests === 'function') {
- map.off('zoomstart', this._client.cancelPendingRequests);
- }
- },
-
- /**
- Loads a specific tile and shows the result when finished.
- @method _loadTile
- @private
- @param {DOMElement} tile The tile dom img element
- @param {Point} tilePoint The tile point
- **/
- _loadTile: function(tile, tilePoint) {
- tile._layer = this;
- tile.onload = this._tileOnLoad;
- tile.onerror = this._tileOnError;
-
- var tileUrl = this._requestTile(tile, tilePoint);
- },
-
- /**
- Requests a specific tile from the server.
- @method _requestTile
- @private
- @param {DOMElement} tile The tile dom img element
- @param {Point} tilePoint The tile point
- **/
- _requestTile: function(tile, tilePoint) {
- var self = this, map = this._map, crs = map.options.crs, tileSize = this.options.tileSize, zoom = this._map.getZoom(), nwPoint = tilePoint.multiplyBy(tileSize), sePoint = nwPoint.add(new L.Point(tileSize, tileSize)), nw = crs.project(map.unproject(nwPoint, zoom)), se = crs.project(map.unproject(sePoint, zoom)), bbox = new L.LatLngBounds([se.y, nw.x], [nw.y, se.x]);
-
- var mapSection = {
- leftTop: {
- "$type": "Point",
- point: {
- "$type": "PlainPoint",
- x: bbox.getNorthWest().lng,
- y: bbox.getNorthWest().lat
- }
- },
- rightBottom: {
- "$type": "Point",
- point: {
- "$type": "PlainPoint",
- x: bbox.getSouthEast().lng,
- y: bbox.getSouthEast().lat
- }
- }
- };
-
- var mapParams = {
- showScale: false,
- useMiles: false
- };
-
- var imageInfo = {
- format: this.options.format,
- width: tileSize,
- height: tileSize
- };
-
- var layers = [];
- var includeImageInResponse = true;
-
- var callerContext = {
- properties: [{
- key: "Profile",
- value: "ajax-bg"
- }, {
- key: "CoordFormat",
- value: "PTV_MERCATOR"
- }]
- };
-
- if (typeof this.options.beforeSend === "function") {
- var req = this.options.beforeSend({ mapSection: mapSection, mapParams: mapParams, imageInfo: imageInfo, layers: layers, includeImageInResponse: includeImageInResponse, callerContext: callerContext });
- mapSection = req.mapSection;
- mapParams = req.mapParams;
- imageInfo = req.imageInfo;
- layers = req.layers;
- includeImageInResponse = req.includeImageInResponse;
- callerContext = req.callerContext;
- }
-
- var cb = (function(tile) {
- return function(resp, error) {
- if (!error && resp) {
- tile.src = L.PtvUtils.createDataURI(resp.image.rawImage);
- } else {
- tile.src = self.options.errorTileUrl;
- console.log(error.errorMessage);
- }
- };
- })(tile);
-
- this._client.renderMapBoundingBox(mapSection, mapParams, imageInfo, layers, includeImageInResponse, callerContext, cb);
- }
- });
-
- L.PtvLayer.background = function(client, options) {
- return new L.PtvLayer.Background(client, options);
- };
-
-