api.lmaps.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <?php
  2. /*
  3. * Leaflet maps API implementation
  4. */
  5. /**
  6. * Returns leaflet maps empty container
  7. *
  8. * @param string $width
  9. * @param string $height
  10. * @param string $id
  11. *
  12. * @return string
  13. */
  14. function generic_MapContainer($width = '', $height = '', $id = '') {
  15. $width = (!empty($width)) ? $width : '100%';
  16. $height = (!empty($height)) ? $height : '800px';
  17. $id = (!empty($id)) ? $id : 'ubmap';
  18. $result = wf_tag('div', false, '', 'id="' . $id . '" style="width:' . $width . '; height:' . $height . ';"');
  19. $result .= wf_tag('div', true);
  20. return ($result);
  21. }
  22. /**
  23. * Translates yandex to google icon code
  24. *
  25. * @param string $icon
  26. * @return string
  27. */
  28. function lm_GetIconUrl($icon) {
  29. $result = '';
  30. switch ($icon) {
  31. case 'twirl#lightblueIcon':
  32. $result = 'skins/mapmarks/blue.png';
  33. break;
  34. case 'twirl#lightblueStretchyIcon':
  35. $result = 'skins/mapmarks/blue.png';
  36. break;
  37. case 'twirl#redStretchyIcon':
  38. $result = 'skins/mapmarks/red.png';
  39. break;
  40. case 'twirl#yellowIcon':
  41. $result = 'skins/mapmarks/yellow.png';
  42. break;
  43. case 'twirl#greenIcon':
  44. $result = 'skins/mapmarks/green.png';
  45. break;
  46. case 'twirl#pinkDotIcon':
  47. $result = 'skins/mapmarks/pink.png';
  48. break;
  49. case 'twirl#brownIcon':
  50. $result = 'skins/mapmarks/brown.png';
  51. break;
  52. case 'twirl#nightDotIcon':
  53. $result = 'skins/mapmarks/darkblue.png';
  54. break;
  55. case 'twirl#redIcon':
  56. $result = 'skins/mapmarks/red.png';
  57. break;
  58. case 'twirl#orangeIcon':
  59. $result = 'skins/mapmarks/orange.png';
  60. break;
  61. case 'twirl#greyIcon':
  62. $result = 'skins/mapmarks/grey.png';
  63. break;
  64. case 'twirl#buildingsIcon':
  65. $result = 'skins/mapmarks/build.png';
  66. break;
  67. case 'twirl#houseIcon':
  68. $result = 'skins/mapmarks/house.png';
  69. break;
  70. case 'twirl#campingIcon':
  71. $result = 'skins/mapmarks/camping.png';
  72. break;
  73. //extended icon pack
  74. case 'redCar':
  75. $result = 'skins/mapmarks/redcar.png';
  76. break;
  77. case 'greenCar':
  78. $result = 'skins/mapmarks/greencar.png';
  79. break;
  80. case 'yellowCar':
  81. $result = 'skins/mapmarks/yellowcar.png';
  82. break;
  83. //unknown icon fallback
  84. default:
  85. $result = 'skins/mapmarks/blue.png';
  86. show_warning('Unknown icon received: ' . $icon);
  87. break;
  88. }
  89. return ($result);
  90. }
  91. /**
  92. * Returns placemark code
  93. *
  94. * @param string $coords
  95. * @param string $title
  96. * @param string $content
  97. * @param string $footer
  98. * @param string $icon
  99. * @param string $iconlabel
  100. * @param bool $canvas
  101. *
  102. * @return string
  103. */
  104. function generic_MapAddMark($coords, $title = '', $content = '', $footer = '', $icon = 'twirl#lightblueIcon', $iconlabel = '', $canvas = false) {
  105. $result = '';
  106. $title = str_replace('"', '\"', $title);
  107. $content = str_replace('"', '\"', $content);
  108. $footer = str_replace('"', '\"', $footer);
  109. $iconCode = '';
  110. $iconDefines = '';
  111. if (!empty($icon)) {
  112. $iconFile = lm_GetIconUrl($icon);
  113. $iconDefines .= "var LeafIcon = L.Icon.extend({
  114. options: {
  115. iconSize: [42, 42],
  116. iconAnchor: [22, 41],
  117. popupAnchor: [-3, -44]
  118. }
  119. });
  120. var customIcon = new LeafIcon({iconUrl: '" . $iconFile . "'});
  121. ";
  122. $iconCode .= ', {icon: customIcon}';
  123. }
  124. $result .= $iconDefines;
  125. $result .= 'var placemark=L.marker([' . $coords . ']' . $iconCode . ').addTo(map)
  126. .bindPopup("<b>' . $title . '</b><br />' . $content . '<br>' . $footer . '", {maxWidth: 320, minWidth: 50, maxHeight: 600, closeButton: true, closeOnEscapeKey: true });';
  127. if (!empty($content)) {
  128. $result .= 'placemark.bindTooltip("' . $content . '", { sticky: true});';
  129. }
  130. return ($result);
  131. }
  132. /**
  133. * Returns circle map placemark
  134. *
  135. * @param string $coords - map coordinates
  136. * @param int $radius - circle radius in meters
  137. * @param string $content - popup balloon content
  138. * @param string $hint - on mouseover hint
  139. * @param string $color - circle border color, default: 009d25
  140. * @param float $opacity - border opacity from 0 to 1, default: 0.8
  141. * @param string $fillColor - fill color of circle, default: 00a20b55
  142. * @param float $fillOpacity - fill opacity from 0 to 1, default: 0.5
  143. *
  144. * @return string
  145. */
  146. function generic_MapAddCircle($coords, $radius, $content = '', $hint = '', $color = '009d25', $opacity = 0.8, $fillColor = '00a20b55', $fillOpacity = 0.5) {
  147. $result = '
  148. var circle = L.circle([' . $coords . '], {
  149. color: \'#' . $color . '\',
  150. opacity: ' . $opacity . ',
  151. fillColor: \'#' . $fillColor . '\',
  152. fillOpacity: ' . $fillOpacity . ',
  153. radius: ' . $radius . '
  154. }).addTo(map);
  155. ';
  156. if (!empty($content)) {
  157. $result .= 'circle.bindPopup("' . $content . '");';
  158. }
  159. if (!empty($hint)) {
  160. $hint = str_replace('"', '\"', $hint);
  161. $result .= 'circle.bindTooltip("' . $hint . '", { sticky: true});';
  162. }
  163. return ($result);
  164. }
  165. /**
  166. * Initalizes leaflet maps API with some params
  167. *
  168. * @param string $center
  169. * @param int $zoom
  170. * @param string $type
  171. * @param string $placemarks
  172. * @param string $editor
  173. * @param string $lang
  174. * @param string $container
  175. * @param string $searchPrefill
  176. *
  177. * @return string
  178. */
  179. function generic_MapInit($center='', $zoom = 15, $type = 'map', $placemarks = '', $editor = '', $lang = 'uk-UA', $container = 'ubmap', $searchPrefill = '') {
  180. global $ubillingConfig;
  181. $mapsCfg = $ubillingConfig->getYmaps();
  182. $result = '';
  183. $tileLayerCustoms = '';
  184. $searchCode = '';
  185. $canvasRender = ($mapsCfg['CANVAS_RENDER']) ? 'true' : 'false'; //string values
  186. if (empty($center)) {
  187. //autolocator here
  188. $mapCenter = 'map.locate({setView: true, maxZoom: ' . $zoom . '});';
  189. //error notice if autolocation failed
  190. $mapCenter .= 'function onLocationError(e) {
  191. alert(e.message);
  192. }
  193. map.on(\'locationerror\', onLocationError)';
  194. } else {
  195. //explicit map center
  196. $mapCenter = 'map.setView([' . $center . '], ' . $zoom . ');';
  197. }
  198. //default OSM tile layer
  199. $tileLayer = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
  200. //custom tile layer
  201. if (isset($mapsCfg['LEAFLET_TILE_LAYER'])) {
  202. if ($mapsCfg['LEAFLET_TILE_LAYER']) {
  203. $tileLayer = $mapsCfg['LEAFLET_TILE_LAYER'];
  204. //Visicom custom options
  205. if (ispos($tileLayer, 'visicom')) {
  206. $tileLayerCustoms = "subdomains: '123',
  207. tms: true";
  208. }
  209. //google satellite
  210. if (ispos($tileLayer, 'google.com')) {
  211. $tileLayerCustoms = "subdomains:['mt0','mt1','mt2','mt3']";
  212. }
  213. }
  214. }
  215. if (!empty($searchPrefill)) {
  216. $searchCode = '
  217. const searchInput = document.querySelector(\'.leaflet-control-geocoder-form input\');
  218. if (searchInput) {
  219. searchInput.value = \'' . $searchPrefill . '\';
  220. }
  221. ';
  222. }
  223. //Leaflet core libs
  224. $result .= wf_tag('link', false, '', 'rel="stylesheet" href="modules/jsc/leaflet/leaflet.css"');
  225. $result .= wf_tag('script', false, '', 'src="modules/jsc/leaflet/leaflet.js"') . wf_tag('script', true);
  226. //Geocoder libs init
  227. $result .= wf_tag('link', false, '', 'rel="stylesheet" href="modules/jsc/leaflet-geocoder/Control.Geocoder.css"');
  228. $result .= wf_tag('script', false, '', 'src="modules/jsc/leaflet-geocoder/Control.Geocoder.min.js"') . wf_tag('script', true);
  229. //Ruler libs init
  230. $result .= wf_tag('link', false, '', 'rel="stylesheet" href="modules/jsc/leaflet-ruler/src/leaflet-ruler.css"');
  231. $result .= wf_tag('script', false, '', 'src="modules/jsc/leaflet-ruler/src/leaflet-ruler.js"') . wf_tag('script', true);
  232. //Easyprint libs init
  233. $result .= wf_tag('script', false, '', 'src="modules/jsc/leaflet-easyprint/dist/bundle.js"') . wf_tag('script', true);
  234. //basic map init
  235. $result .= wf_tag('script', false, '', 'type = "text/javascript"');
  236. $result .= '
  237. var map = L.map(\'' . $container . '\');
  238. ' . $mapCenter . '
  239. L.tileLayer(\'' . $tileLayer . '\', {
  240. maxZoom: 18,
  241. attribution: \'\',
  242. id: \'mapbox.streets\',
  243. ' . $tileLayerCustoms . '
  244. }).addTo(map);
  245. var geoControl = new L.Control.Geocoder({showResultIcons: true, errorMessage: "' . __('Nothing found') . '", placeholder: "' . __('Search') . '"});
  246. geoControl.addTo(map);
  247. L.easyPrint({
  248. title: \'' . __('Export') . '\',
  249. defaultSizeTitles: {Current: \'' . __('Current') . '\', A4Landscape: \'A4 Landscape\', A4Portrait: \'A4 Portrait\'},
  250. position: \'topright\',
  251. filename: \'ubillingmap_' . date("Y-m-d_H:i:s") . '\',
  252. exportOnly: true,
  253. hideControlContainer: true,
  254. sizeModes: [\'Current\', \'A4Landscape\', \'A4Portrait\'],
  255. }).addTo(map);
  256. var options = {
  257. position: \'topright\',
  258. preferCanvas: \'' . $canvasRender . '\',
  259. lengthUnit: {
  260. display: \'' . __('meters') . '\',
  261. decimal: 2,
  262. factor: 1000,
  263. label: \'' . __('Distance') . ':\'
  264. },
  265. angleUnit: {
  266. display: \'&deg;\',
  267. decimal: 2,
  268. factor: null,
  269. label: \'' . __('Bearing') . ':\'
  270. }
  271. };
  272. L.control.ruler(options).addTo(map);
  273. ' . $placemarks . '
  274. ' . $editor . '
  275. ' . $searchCode . '
  276. ';
  277. $result .= wf_tag('script', true);
  278. return ($result);
  279. }
  280. /**
  281. * Return generic editor code
  282. *
  283. * @param string $name
  284. * @param string $title
  285. * @param string $data
  286. *
  287. * @return string
  288. */
  289. function generic_MapEditor($name, $title = '', $data = '') {
  290. $data = str_replace("'", '`', $data);
  291. $data = str_replace("\n", '', $data);
  292. $data = str_replace('"', '\"', $data);
  293. $content = '<form action=\"\" method=\"POST\"><input type=\"hidden\" name=' . $name . ' value=\'"+e.latlng.lat.toPrecision(7)+\',\'+e.latlng.lng.toPrecision(7)+"\'>' . $data . '</form>';
  294. $windowCode = '<b>' . $title . '</b><br>' . $content;
  295. $result = 'var popup = L.popup();
  296. function onMapClick(e) {
  297. popup
  298. .setLatLng(e.latlng)
  299. .setContent("' . $windowCode . '<br>" + e.latlng.lat.toPrecision(7) + "," + e.latlng.lng.toPrecision(7))
  300. .openOn(map);
  301. }
  302. map.on(\'click\', onMapClick);';
  303. return ($result);
  304. }
  305. /**
  306. * Returns JS code to draw line within two points
  307. *
  308. * @param string $coord1
  309. * @param string $coord2
  310. * @param string $color
  311. * @param string $hint
  312. * @param string $width
  313. *
  314. * @return string
  315. */
  316. function generic_MapAddLine($coord1, $coord2, $color = '', $hint = '', $width = '') {
  317. $lineId = wf_InputId();
  318. $color = (!empty($color)) ? $color : '#000000';
  319. $width = (!empty($color)) ? $width + 1 : '1';
  320. $result = '';
  321. $result .= '
  322. var pointA = new L.LatLng(' . $coord1 . ');
  323. var pointB = new L.LatLng(' . $coord2 . ');
  324. var pointList = [pointA, pointB];
  325. var polyline_' . $lineId . ' = new L.Polyline(pointList, {
  326. color: \'' . $color . '\',
  327. weight: ' . $width . ',
  328. opacity: 0.8,
  329. smoothFactor: 1
  330. });
  331. polyline_' . $lineId . '.addTo(map);
  332. ';
  333. if (!empty($hint)) {
  334. $hint = str_replace('"', '\"', $hint);
  335. $result .= 'polyline_' . $lineId . '.bindTooltip("' . $hint . '", { sticky: true});';
  336. }
  337. return ($result);
  338. }