src\Poi.js - leaflet.ptv

API Docs for: 0.1.0
Show:

File: src\Poi.js

  1. /**
  2. @module L.PtvLayer
  3. **/
  4. L.PtvLayer = L.PtvLayer || {};
  5.  
  6. /**
  7. Provides the PTV POI Layer class.
  8. @class L.PtvLayer.Poi
  9. @extends L.PtvLayer.AbstractLayer
  10. @params {XMapClient} client XMapClient object
  11. @params {Object} options The options object
  12. @params {String} [options.format] The image format used in tile requests.
  13. @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.
  14. @constructor
  15. **/
  16. L.PtvLayer.Poi = L.PtvLayer.AbstractOverlay.extend({
  17. _layerName: 'default.points-of-interest',
  18. _profile: '',
  19. _condition: '',
  20. _formatterFn: null,
  21. _poiMarkers: null,
  22.  
  23. initialize: function(client, options) {
  24. L.PtvLayer.AbstractOverlay.prototype.initialize.call(this, client, options);
  25. },
  26.  
  27. onAdd: function(map) {
  28. L.PtvLayer.AbstractOverlay.prototype.onAdd.call(this, map);
  29. this._poiMarkers = L.layerGroup().addTo(map);
  30. },
  31.  
  32. onRemove: function(map) {
  33. L.PtvLayer.AbstractOverlay.prototype.onRemove.call(this, map);
  34. map.removeLayer(this._poiMarkers);
  35. },
  36.  
  37. _getRequestObject: function() {
  38. var req = L.PtvLayer.AbstractOverlay.prototype._getRequestObject.call(this);
  39.  
  40. req.callerContext.properties[0].value = "ajax-av";
  41. req.callerContext.properties[1].value = "PTV_MERCATOR";
  42. req.layers = [{
  43. $type: "SMOLayer",
  44. name: this._getConfig(),
  45. visible: true,
  46. objectInfos: "FULLGEOMETRY",
  47. configuration: ""
  48. }];
  49.  
  50. return req;
  51. },
  52.  
  53. _renderMapCallback: function(resp, error, el) {
  54. this._poiMarkers.clearLayers();
  55.  
  56. L.PtvLayer.AbstractOverlay.prototype._renderMapCallback.call(this, resp, error, el);
  57.  
  58. if (error || resp.objects.length === 0) {
  59. return;
  60. }
  61.  
  62. var formatter = this.getFormatter(), objects = resp.objects[0].objects, myIcon = L.divIcon({
  63. className: 'poi-icon',
  64. iconSize: [20, 20]
  65. });
  66.  
  67. for (var i = 0; i < objects.length; i++) {
  68. latlng = this._map.containerPointToLatLng([objects[i].pixel.x, objects[i].pixel.y]);
  69. L.marker(latlng, {
  70. icon: myIcon
  71. }).bindPopup(formatter(objects[i].descr)).addTo(this._poiMarkers);
  72. }
  73. },
  74.  
  75. getFormatter: function() {
  76. if ( typeof this._formatterFn === 'function') {
  77. return this._formatterFn;
  78. } else {
  79. return function(value) {
  80. var desc, poiType;
  81.  
  82. value = value.split('#')[1];
  83.  
  84. if (value.indexOf(':') === -1) {
  85. desc = value;
  86. } else {
  87. var values = value.split(':');
  88. desc = values[1];
  89. poiType = values[0];
  90. }
  91.  
  92. return '<p>' + '<strong>' + desc.replace('$ยง$', '<br/>') + '</strong><br />' + ( poiType ? 'POI Type: ' + poiType : '') + '</p>';
  93. };
  94. }
  95. },
  96.  
  97. setFormatter: function(fn) {
  98. if ( typeof fn === 'function') {
  99. this._formatterFn = fn;
  100. }
  101. },
  102.  
  103. _getConfig: function() {
  104. return this._layerName + (this._profile ? '.' + this._profile : '') + ';' + this._condition;
  105. },
  106.  
  107. setLayerName: function(layerName) {
  108. this._layerName = layerName;
  109. this._update();
  110. },
  111.  
  112. getLayerName: function() {
  113. return this._layerName;
  114. },
  115.  
  116. setProfile: function(profile) {
  117. this._profile = profile;
  118. this._update();
  119. },
  120.  
  121. getProfile: function() {
  122. return this._profile;
  123. },
  124.  
  125. setCondition: function(condition) {
  126. this._condition = condition;
  127. this._update();
  128. },
  129.  
  130. getCondition: function() {
  131. return this._condition;
  132. }
  133. });
  134.  
  135. L.PtvLayer.poi = function(client, options) {
  136. return new L.PtvLayer.Poi(client, options);
  137. };
  138.