api.lmaps.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. * Adds a dynamic map marker with AJAX-loaded popup content
  134. *
  135. * @param string $coords Coordinates in "lat,lng" format
  136. * @param string $title Marker tooltip text
  137. * @param string $contentUrl URL to load popup content from
  138. * @param string $icon Icon identifier
  139. *
  140. * @return string
  141. */
  142. function generic_MapAddMarkDynamic($coords, $title = '', $contentUrl = '', $icon = 'twirl#lightblueIcon') {
  143. $markerId = wf_InputId();
  144. $title = str_replace('"', '\"', $title);
  145. $contentUrl = str_replace('"', '\"', $contentUrl);
  146. $iconCode = '';
  147. $iconDefines = '';
  148. if (!empty($icon)) {
  149. $iconFile = lm_GetIconUrl($icon);
  150. $iconDefines .= "var LeafIcon = L.Icon.extend({
  151. options: {
  152. iconSize: [42, 42],
  153. iconAnchor: [22, 41],
  154. popupAnchor: [-3, -44]
  155. }
  156. });
  157. var customIcon_$markerId = new LeafIcon({iconUrl: '" . $iconFile . "'});\n";
  158. $iconCode = ", {icon: customIcon_$markerId}";
  159. }
  160. $js = "
  161. $iconDefines
  162. var marker_$markerId = L.marker([$coords]$iconCode).addTo(map);";
  163. if (!empty($contentUrl)) {
  164. $js .= "
  165. marker_$markerId.bindPopup('" . __('Loading') . "...');
  166. marker_$markerId._popupHtml = null;
  167. marker_$markerId.on('click', function (e) {
  168. var marker = e.target;
  169. if (marker._popupHtml !== null) {
  170. marker.setPopupContent(marker._popupHtml);
  171. marker.openPopup();
  172. return;
  173. }
  174. marker.setPopupContent('" . __('Loading') . "...');
  175. marker.openPopup();
  176. fetch('$contentUrl')
  177. .then(response => response.text())
  178. .then(html => {
  179. marker._popupHtml = html;
  180. marker.setPopupContent(html);
  181. marker.openPopup();
  182. })
  183. .catch(() => {
  184. marker.setPopupContent('" . __('Error') . ' ' . __('Loading') . "');
  185. marker.openPopup();
  186. });
  187. });";
  188. }
  189. if (!empty($title)) {
  190. $js .= "\nmarker_$markerId.bindTooltip(\"$title\", {sticky: true});";
  191. }
  192. return $js;
  193. }
  194. /**
  195. * Returns circle map placemark
  196. *
  197. * @param string $coords - map coordinates
  198. * @param int $radius - circle radius in meters
  199. * @param string $content - popup balloon content
  200. * @param string $hint - on mouseover hint
  201. * @param string $color - circle border color, default: 009d25
  202. * @param float $opacity - border opacity from 0 to 1, default: 0.8
  203. * @param string $fillColor - fill color of circle, default: 00a20b55
  204. * @param float $fillOpacity - fill opacity from 0 to 1, default: 0.5
  205. *
  206. * @return string
  207. */
  208. function generic_MapAddCircle($coords, $radius, $content = '', $hint = '', $color = '009d25', $opacity = 0.8, $fillColor = '00a20b55', $fillOpacity = 0.5) {
  209. $result = '
  210. var circle = L.circle([' . $coords . '], {
  211. color: \'#' . $color . '\',
  212. opacity: ' . $opacity . ',
  213. fillColor: \'#' . $fillColor . '\',
  214. fillOpacity: ' . $fillOpacity . ',
  215. radius: ' . $radius . '
  216. }).addTo(map);
  217. ';
  218. if (!empty($content)) {
  219. $result .= 'circle.bindPopup("' . $content . '");';
  220. }
  221. if (!empty($hint)) {
  222. $hint = str_replace('"', '\"', $hint);
  223. $result .= 'circle.bindTooltip("' . $hint . '", { sticky: true});';
  224. }
  225. return ($result);
  226. }
  227. /**
  228. * Initalizes leaflet maps API with some params
  229. *
  230. * @param string $center
  231. * @param int $zoom
  232. * @param string $type
  233. * @param string $placemarks
  234. * @param string $editor
  235. * @param string $lang
  236. * @param string $container
  237. * @param string $searchPrefill
  238. *
  239. * @return string
  240. */
  241. function generic_MapInit($center = '', $zoom = 15, $type = 'roadmap', $placemarks = '', $editor = '', $lang = 'uk-UA', $container = 'ubmap', $searchPrefill = '') {
  242. global $ubillingConfig;
  243. $mapsCfg = $ubillingConfig->getYmaps();
  244. $result = '';
  245. $tileLayerCustoms = '';
  246. $searchCode = '';
  247. $type = ($type == 'map') ? 'roadmap' : $type; // legacy config option
  248. $canvasRender = ($mapsCfg['CANVAS_RENDER']) ? 'true' : 'false'; //string values
  249. if (empty($center)) {
  250. //autolocator here
  251. $mapCenter = 'map.locate({setView: true, maxZoom: ' . $zoom . '});
  252. function onLocationError(e) {
  253. alert(e.message);
  254. }
  255. map.on(\'locationerror\', onLocationError);';
  256. } else {
  257. //explicit map center
  258. $mapCenter = 'map.setView([' . $center . '], ' . $zoom . ');';
  259. }
  260. //default OSM tile layer
  261. $tileLayerOSM = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
  262. //satellite map layers
  263. $tileLayerSatellite = 'https://mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}';
  264. $tileLayerHybrid = 'https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}';
  265. //custom OSM tile layer
  266. if (isset($mapsCfg['LEAFLET_TILE_LAYER'])) {
  267. if ($mapsCfg['LEAFLET_TILE_LAYER']) {
  268. $tileLayerOSM = $mapsCfg['LEAFLET_TILE_LAYER'];
  269. //Visicom custom options
  270. if (ispos($tileLayerOSM, 'visicom')) {
  271. $tileLayerCustoms = "subdomains: '123',
  272. tms: true";
  273. }
  274. //google satellite
  275. if (ispos($tileLayerOSM, 'google.com')) {
  276. $tileLayerCustoms = "subdomains:['mt0','mt1','mt2','mt3']";
  277. }
  278. }
  279. }
  280. if (!empty($searchPrefill)) {
  281. $searchCode = '
  282. const searchInput = document.querySelector(\'.leaflet-control-geocoder-form input\');
  283. if (searchInput) {
  284. searchInput.value = \'' . $searchPrefill . '\';
  285. }';
  286. }
  287. //Leaflet core libs
  288. $result .= wf_tag('link', false, '', 'rel="stylesheet" href="modules/jsc/leaflet/leaflet.css"');
  289. $result .= wf_tag('script', false, '', 'src="modules/jsc/leaflet/leaflet.js"') . wf_tag('script', true);
  290. //Geocoder libs init
  291. $result .= wf_tag('link', false, '', 'rel="stylesheet" href="modules/jsc/leaflet-geocoder/Control.Geocoder.css"');
  292. $result .= wf_tag('script', false, '', 'src="modules/jsc/leaflet-geocoder/Control.Geocoder.min.js"') . wf_tag('script', true);
  293. //Ruler libs init
  294. $result .= wf_tag('link', false, '', 'rel="stylesheet" href="modules/jsc/leaflet-ruler/src/leaflet-ruler.css"');
  295. $result .= wf_tag('script', false, '', 'src="modules/jsc/leaflet-ruler/src/leaflet-ruler.js"') . wf_tag('script', true);
  296. //Easyprint lib init
  297. $result .= wf_tag('script', false, '', 'src="modules/jsc/leaflet-easyprint/dist/bundle.js"') . wf_tag('script', true);
  298. //Fullscreen control
  299. $result .= wf_tag('link', false, '', 'rel="stylesheet" href="modules/jsc/leaflet-fullscreen/dist/leaflet.fullscreen.css"');
  300. $result .= wf_tag('script', false, '', 'src="modules/jsc/leaflet-fullscreen/dist/Leaflet.fullscreen.min.js"') . wf_tag('script', true);
  301. //basic map init
  302. $result .= wf_tag('script', false, '', 'type = "text/javascript"');
  303. $result .= '
  304. var map = L.map(\'' . $container . '\');
  305. ' . $mapCenter . '
  306. // Tile layers
  307. var roadmap = L.tileLayer(\'' . $tileLayerOSM . '\', {
  308. maxZoom: 18,
  309. attribution: \'\',
  310. ' . $tileLayerCustoms . '
  311. });
  312. var satellite = L.tileLayer(\'' . $tileLayerSatellite . '\', {
  313. maxZoom: 18,
  314. attribution: \'© Google\'
  315. });
  316. var hybrid = L.tileLayer(\'' . $tileLayerHybrid . '\', {
  317. maxZoom: 18,
  318. attribution: \'© Google\'
  319. });
  320. // Default tile layer
  321. ' . $type . '.addTo(map);
  322. // Base layers switcher
  323. var baseMaps = {
  324. "' . __('Map') . '": roadmap,
  325. "' . __('Hybrid') . '": hybrid,
  326. "' . __('Satellite') . '": satellite,
  327. };
  328. var geoControl = new L.Control.Geocoder({showResultIcons: true, errorMessage: "' . __('Nothing found') . '", placeholder: "' . __('Search') . '"});
  329. geoControl.addTo(map);
  330. L.easyPrint({
  331. title: \'' . __('Export') . '\',
  332. defaultSizeTitles: {Current: \'' . __('Current') . '\', A4Landscape: \'A4 Landscape\', A4Portrait: \'A4 Portrait\'},
  333. position: \'topright\',
  334. filename: \'ubillingmap_' . date("Y-m-d_H:i:s") . '\',
  335. exportOnly: true,
  336. hideControlContainer: true,
  337. sizeModes: [\'Current\', \'A4Landscape\', \'A4Portrait\']
  338. }).addTo(map);
  339. var options = {
  340. position: \'topright\',
  341. preferCanvas: \'' . $canvasRender . '\',
  342. lengthUnit: {
  343. display: \'' . __('meters') . '\',
  344. decimal: 2,
  345. factor: 1000,
  346. label: \'' . __('Distance') . ':\'
  347. },
  348. angleUnit: {
  349. display: \'&deg;\',
  350. decimal: 2,
  351. factor: null,
  352. label: \'' . __('Bearing') . ':\'
  353. }
  354. };
  355. L.control.ruler(options).addTo(map);
  356. map.addControl(new L.Control.Fullscreen({
  357. title: {
  358. \'false\': \'' . __('Fullscreen') . '\',
  359. \'true\': \'' . __('Exit fullscreen') . '\'
  360. }
  361. }));
  362. var layerControl = L.control.layers(baseMaps, null, { collapsed: true });
  363. map.addControl(layerControl);
  364. ' . $placemarks . '
  365. ' . $editor . '
  366. ' . $searchCode . '
  367. ';
  368. $result .= wf_tag('script', true);
  369. return ($result);
  370. }
  371. /**
  372. * Return generic editor code
  373. *
  374. * @param string $name
  375. * @param string $title
  376. * @param string $data
  377. * @param int $precision
  378. *
  379. * @return string
  380. */
  381. function generic_MapEditor($name, $title = '', $data = '', $precision = 8) {
  382. $data = str_replace("'", '`', $data);
  383. $data = str_replace("\n", '', $data);
  384. $data = str_replace('"', '\"', $data);
  385. $content = '<form action=\"\" method=\"POST\"><input type=\"hidden\" name=' . $name . ' value=\'"+e.latlng.lat.toPrecision(' . $precision . ')+\',\'+e.latlng.lng.toPrecision(' . $precision . ')+"\'>' . $data . '</form>';
  386. $windowCode = '<b>' . $title . '</b><br>' . $content;
  387. $result = 'var popup = L.popup();
  388. function onMapClick(e) {
  389. popup
  390. .setLatLng(e.latlng)
  391. .setContent("' . $windowCode . '<br>" + e.latlng.lat.toPrecision(' . $precision . ') + "," + e.latlng.lng.toPrecision(' . $precision . '))
  392. .openOn(map);
  393. }
  394. map.on(\'click\', onMapClick);';
  395. return ($result);
  396. }
  397. /**
  398. * Returns JS code to draw line within two points
  399. *
  400. * @param string $coord1
  401. * @param string $coord2
  402. * @param string $color
  403. * @param string $hint
  404. * @param string $width
  405. *
  406. * @return string
  407. */
  408. function generic_MapAddLine($coord1, $coord2, $color = '', $hint = '', $width = '') {
  409. $lineId = wf_InputId();
  410. $color = (!empty($color)) ? $color : '#000000';
  411. $width = (!empty($color)) ? $width + 1 : '1';
  412. $result = '';
  413. $result .= '
  414. var pointA = new L.LatLng(' . $coord1 . ');
  415. var pointB = new L.LatLng(' . $coord2 . ');
  416. var pointList = [pointA, pointB];
  417. var polyline_' . $lineId . ' = new L.Polyline(pointList, {
  418. color: \'' . $color . '\',
  419. weight: ' . $width . ',
  420. opacity: 0.8,
  421. smoothFactor: 1
  422. });
  423. polyline_' . $lineId . '.addTo(map);
  424. ';
  425. if (!empty($hint)) {
  426. $hint = str_replace('"', '\"', $hint);
  427. $result .= 'polyline_' . $lineId . '.bindTooltip("' . $hint . '", { sticky: true});';
  428. }
  429. return ($result);
  430. }