map.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * Copyright (c) UNA, Inc - https://una.io
  3. * MIT License - https://opensource.org/licenses/MIT
  4. *
  5. * @defgroup MapShow Display last sign up users on map
  6. * @ingroup UnaModules
  7. *
  8. * @{
  9. */
  10. function BxMapShow(oOptions) {
  11. this._sContainerId = 'BxMapShowContainer';
  12. this._iMapPointsLastId = 0;
  13. this._oMap = null;
  14. this._sActionsUri = oOptions.sActionUri;
  15. this._sPathToJsonData = oOptions.sPathToJsonData == undefined ? '' : oOptions.sPathToJsonData;
  16. this._iIntervalCheckNewInSeconds = oOptions.iIntervalCheckNewInSeconds == undefined ? 5 : new Number(oOptions.iIntervalCheckNewInSeconds);
  17. this._fCenterMapLonCoordinate = oOptions.fCenterMapLonCoordinate == undefined ? 2.896372 : new Number(oOptions.fCenterMapLonCoordinate);
  18. this._fCenterMapLatCoordinate = oOptions.fCenterMapLatCoordinate == undefined ? 44.60240 : new Number(oOptions.fCenterMapLatCoordinate);
  19. this._fMapZoom = oOptions.fMapZoom == undefined ? 2 : new Number(oOptions.fMapZoom);
  20. var $this = this;
  21. $(document).ready(function () {
  22. $this.init();
  23. });
  24. $(window).resize(function () { $this.adaptive(); })
  25. }
  26. BxMapShow.prototype.init = function () {
  27. var $this = this;
  28. var oCanvas = document.createElement('canvas');
  29. var oContext = oCanvas.getContext('2d');
  30. var iPixelRatio = ol.has.DEVICE_PIXEL_RATIO;
  31. var oPattern = (function () {
  32. oCanvas.width = oCanvas.height = 10 * iPixelRatio;
  33. oContext.fillStyle = 'rgb(214, 227, 241)';
  34. oContext.fillRect(0, 0, oCanvas.width, oCanvas.height);
  35. return oContext.createPattern(oCanvas, 'repeat');
  36. }());
  37. var oFill = new ol.style.Fill();
  38. var oStyle = new ol.style.Style({
  39. fill: oFill,
  40. stroke: new ol.style.Stroke({
  41. color: 'rgb(214, 227, 241)',
  42. width: 1
  43. })
  44. });
  45. var getStackedStyle = function () {
  46. oFill.setColor(oPattern);
  47. return oStyle;
  48. };
  49. var oBaseVectorLayer = new ol.layer.Vector({
  50. source: new ol.source.Vector({
  51. url: $this._sPathToJsonData,
  52. format: new ol.format.GeoJSON()
  53. }),
  54. style: getStackedStyle
  55. });
  56. $this._oMap = new ol.Map({
  57. layers: [oBaseVectorLayer],
  58. target: document.getElementById($this._sContainerId),
  59. view: new ol.View({
  60. center: ol.proj.fromLonLat([$this._fCenterMapLonCoordinate, $this._fCenterMapLatCoordinate]),
  61. zoom: $this.getZoomValue()
  62. }),
  63. controls: [],
  64. interactions: ol.interaction.defaults({
  65. dragRotate: false,
  66. dragPan: false,
  67. pinchRotate: false,
  68. pinchZoom: false,
  69. doubleClickZoom: false,
  70. dragAndDrop: false,
  71. keyboardPan: false,
  72. keyboardZoom: false,
  73. mouseWheelZoom: false,
  74. pointer: false,
  75. select: false
  76. })
  77. });
  78. $this.adaptive();
  79. $this.addPoints();
  80. }
  81. BxMapShow.prototype.adaptive = function () {
  82. var $this = this;
  83. var oView = this._oMap.getView();
  84. var fZoom = this.getZoomValue();
  85. oView.setZoom(fZoom);
  86. };
  87. BxMapShow.prototype.getZoomValue = function () {
  88. return this._fMapZoom * ($('#' + this._sContainerId).width() / 1629);
  89. };
  90. BxMapShow.prototype.addPoints = function () {
  91. var $this = this;
  92. var aPoints = new Array();
  93. var oStyles = {
  94. 'small': new ol.style.Style({
  95. image: new ol.style.Circle({
  96. radius: 5,
  97. fill: new ol.style.Fill({ color: '#00adea' }),
  98. stroke: new ol.style.Stroke({ color: '#cef1fb', width: 5 })
  99. })
  100. }),
  101. 'large': new ol.style.Style({
  102. image: new ol.style.Circle({
  103. radius: 10,
  104. fill: new ol.style.Fill({ color: '#00adea' }),
  105. stroke: new ol.style.Stroke({ color: '#cef1fb', width: 10 })
  106. })
  107. })
  108. };
  109. var bIsInitedDots = ($this._iMapPointsLastId == 0);
  110. $.getJSON($this._sActionsUri + 'GetMapPoints/' + $this._iMapPointsLastId + '/', function (aMapPoints) {
  111. $.each(aMapPoints, function (key, value) {
  112. var oPoint = new ol.Feature({
  113. geometry: new ol.geom.Point(ol.proj.fromLonLat([new Number(value.lng), new Number(value.lat)]))
  114. });
  115. oPoint.setStyle(bIsInitedDots ? oStyles['small'] : oStyles['large']);
  116. aPoints.push(oPoint);
  117. if ($this._iMapPointsLastId < new Number(value.id))
  118. $this._iMapPointsLastId = new Number(value.id);
  119. });
  120. if (aPoints.length > 0) {
  121. var oPointSource = new ol.source.Vector({
  122. features: aPoints
  123. });
  124. var oPointsVectorLayer = new ol.layer.Vector({
  125. source: oPointSource
  126. });
  127. $this._oMap.addLayer(oPointsVectorLayer);
  128. }
  129. setTimeout(function () {
  130. $this.addPoints();
  131. }, $this._iIntervalCheckNewInSeconds*1000);
  132. });
  133. }