API Docs for: 0.1.0
Show:

File: src\Poi.js

/**
 @module L.PtvLayer
 **/
L.PtvLayer = L.PtvLayer || {};

/**
 Provides the PTV POI Layer class.
 @class L.PtvLayer.Poi
 @extends L.PtvLayer.AbstractLayer
 @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.
 @constructor
 **/
L.PtvLayer.Poi = L.PtvLayer.AbstractOverlay.extend({
  _layerName: 'default.points-of-interest',
  _profile: '',
  _condition: '',
  _formatterFn: null,
  _poiMarkers: null,

  initialize: function(client, options) {
    L.PtvLayer.AbstractOverlay.prototype.initialize.call(this, client, options);
  },

  onAdd: function(map) {
    L.PtvLayer.AbstractOverlay.prototype.onAdd.call(this, map);
    this._poiMarkers = L.layerGroup().addTo(map);
  },

  onRemove: function(map) {
    L.PtvLayer.AbstractOverlay.prototype.onRemove.call(this, map);
    map.removeLayer(this._poiMarkers);
  },

  _getRequestObject: function() {
    var req = L.PtvLayer.AbstractOverlay.prototype._getRequestObject.call(this);

    req.callerContext.properties[0].value = "ajax-av";
    req.callerContext.properties[1].value = "PTV_MERCATOR";
    req.layers = [{
      $type: "SMOLayer",
      name: this._getConfig(),
      visible: true,
      objectInfos: "FULLGEOMETRY",
      configuration: ""
    }];

    return req;
  },

  _renderMapCallback: function(resp, error, el) {
    this._poiMarkers.clearLayers();

    L.PtvLayer.AbstractOverlay.prototype._renderMapCallback.call(this, resp, error, el);

    if (error || resp.objects.length === 0) {
      return;
    }

    var formatter = this.getFormatter(), objects = resp.objects[0].objects, myIcon = L.divIcon({
      className: 'poi-icon',
      iconSize: [20, 20]
    });

    for (var i = 0; i < objects.length; i++) {
      latlng = this._map.containerPointToLatLng([objects[i].pixel.x, objects[i].pixel.y]);
      L.marker(latlng, {
        icon: myIcon
      }).bindPopup(formatter(objects[i].descr)).addTo(this._poiMarkers);
    }
  },

  getFormatter: function() {
    if ( typeof this._formatterFn === 'function') {
      return this._formatterFn;
    } else {
      return function(value) {
        var desc, poiType;

        value = value.split('#')[1];

        if (value.indexOf(':') === -1) {
          desc = value;
        } else {
          var values = value.split(':');
          desc = values[1];
          poiType = values[0];
        }

        return '<p>' + '<strong>' + desc.replace('$ยง$', '<br/>') + '</strong><br />' + ( poiType ? 'POI Type: ' + poiType : '') + '</p>';
      };
    }
  },

  setFormatter: function(fn) {
    if ( typeof fn === 'function') {
      this._formatterFn = fn;
    }
  },

  _getConfig: function() {
    return this._layerName + (this._profile ? '.' + this._profile : '') + ';' + this._condition;
  },

  setLayerName: function(layerName) {
    this._layerName = layerName;
    this._update();
  },

  getLayerName: function() {
    return this._layerName;
  },

  setProfile: function(profile) {
    this._profile = profile;
    this._update();
  },

  getProfile: function() {
    return this._profile;
  },

  setCondition: function(condition) {
    this._condition = condition;
    this._update();
  },

  getCondition: function() {
    return this._condition;
  }
});

L.PtvLayer.poi = function(client, options) {
  return new L.PtvLayer.Poi(client, options);
};