api.lmaps.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 google maps API with some params
  167. *
  168. * @param string $center
  169. * @param int $zoom
  170. * @param string $type
  171. * @param string $placemarks
  172. * @param bool $editor
  173. * @param string $lang
  174. * @param string $container
  175. *
  176. * @return string
  177. */
  178. function generic_MapInit($center, $zoom, $type, $placemarks = '', $editor = '', $lang = 'ru-RU', $container = 'ubmap') {
  179. global $ubillingConfig;
  180. $mapsCfg = $ubillingConfig->getYmaps();
  181. $result = '';
  182. $tileLayerCustoms = '';
  183. $canvasRender = ($mapsCfg['CANVAS_RENDER']) ? 'true' : 'false'; //string values
  184. if (empty($center)) {
  185. //autolocator here
  186. $mapCenter = 'map.locate({setView: true, maxZoom: ' . $zoom . '});';
  187. //error notice if autolocation failed
  188. $mapCenter .= 'function onLocationError(e) {
  189. alert(e.message);
  190. }
  191. map.on(\'locationerror\', onLocationError)';
  192. } else {
  193. //explicit map center
  194. $mapCenter = 'map.setView([' . $center . '], ' . $zoom . ');';
  195. }
  196. //default OSM tile layer
  197. $tileLayer = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
  198. //custom tile layer
  199. if (isset($mapsCfg['LEAFLET_TILE_LAYER'])) {
  200. if ($mapsCfg['LEAFLET_TILE_LAYER']) {
  201. $tileLayer = $mapsCfg['LEAFLET_TILE_LAYER'];
  202. //Visicom custom options
  203. if (ispos($tileLayer, 'visicom')) {
  204. $tileLayerCustoms = "subdomains: '123',
  205. tms: true";
  206. }
  207. //google satellite
  208. if (ispos($tileLayer, 'google.com')) {
  209. $tileLayerCustoms = "subdomains:['mt0','mt1','mt2','mt3']";
  210. }
  211. }
  212. }
  213. //Leaflet core libs
  214. $result .= wf_tag('link', false, '', 'rel="stylesheet" href="modules/jsc/leaflet/leaflet.css"');
  215. $result .= wf_tag('script', false, '', 'src="modules/jsc/leaflet/leaflet.js"') . wf_tag('script', true);
  216. //Geocoder libs init
  217. $result .= wf_tag('link', false, '', 'rel="stylesheet" href="modules/jsc/leaflet-geocoder/Control.Geocoder.css"');
  218. $result .= wf_tag('script', false, '', 'src="modules/jsc/leaflet-geocoder/Control.Geocoder.min.js"') . wf_tag('script', true);
  219. //Ruler libs init
  220. $result .= wf_tag('link', false, '', 'rel="stylesheet" href="modules/jsc/leaflet-ruler/src/leaflet-ruler.css"');
  221. $result .= wf_tag('script', false, '', 'src="modules/jsc/leaflet-ruler/src/leaflet-ruler.js"') . wf_tag('script', true);
  222. //Easyprint libs init
  223. $result .= wf_tag('script', false, '', 'src="modules/jsc/leaflet-easyprint/dist/bundle.js"') . wf_tag('script', true);
  224. //basic map init
  225. $result .= wf_tag('script', false, '', 'type = "text/javascript"');
  226. $result .= '
  227. var map = L.map(\'' . $container . '\');
  228. ' . $mapCenter . '
  229. L.tileLayer(\'' . $tileLayer . '\', {
  230. maxZoom: 18,
  231. attribution: \'\',
  232. id: \'mapbox.streets\',
  233. ' . $tileLayerCustoms . '
  234. }).addTo(map);
  235. var geoControl = new L.Control.Geocoder({showResultIcons: true, errorMessage: "' . __('Nothing found') . '", placeholder: "' . __('Search') . '"});
  236. geoControl.addTo(map);
  237. L.easyPrint({
  238. title: \'' . __('Export') . '\',
  239. defaultSizeTitles: {Current: \'' . __('Current') . '\', A4Landscape: \'A4 Landscape\', A4Portrait: \'A4 Portrait\'},
  240. position: \'topright\',
  241. filename: \'ubillingmap_' . date("Y-m-d_H:i:s") . '\',
  242. exportOnly: true,
  243. hideControlContainer: true,
  244. sizeModes: [\'Current\', \'A4Landscape\', \'A4Portrait\'],
  245. }).addTo(map);
  246. var options = {
  247. position: \'topright\',
  248. preferCanvas: \'' . $canvasRender . '\',
  249. lengthUnit: {
  250. display: \'' . __('meters') . '\',
  251. decimal: 2,
  252. factor: 1000,
  253. label: \'' . __('Distance') . ':\'
  254. },
  255. angleUnit: {
  256. display: \'&deg;\',
  257. decimal: 2,
  258. factor: null,
  259. label: \'' . __('Bearing') . ':\'
  260. }
  261. };
  262. L.control.ruler(options).addTo(map);
  263. ' . $placemarks . '
  264. ' . $editor . '
  265. ';
  266. $result .= wf_tag('script', true);
  267. return($result);
  268. }
  269. /**
  270. * Return generic editor code
  271. *
  272. * @param string $name
  273. * @param string $title
  274. * @param string $data
  275. *
  276. * @return string
  277. */
  278. function generic_MapEditor($name, $title = '', $data = '') {
  279. $data = str_replace("'", '`', $data);
  280. $data = str_replace("\n", '', $data);
  281. $data = str_replace('"', '\"', $data);
  282. $content = '<form action=\"\" method=\"POST\"><input type=\"hidden\" name=' . $name . ' value=\'"+e.latlng.lat.toPrecision(7)+\',\'+e.latlng.lng.toPrecision(7)+"\'>' . $data . '</form>';
  283. $windowCode = '<b>' . $title . '</b><br>' . $content;
  284. $result = 'var popup = L.popup();
  285. function onMapClick(e) {
  286. popup
  287. .setLatLng(e.latlng)
  288. .setContent("' . $windowCode . '<br>" + e.latlng.lat.toPrecision(7) + "," + e.latlng.lng.toPrecision(7))
  289. .openOn(map);
  290. }
  291. map.on(\'click\', onMapClick);';
  292. return ($result);
  293. }
  294. /**
  295. * Returns JS code to draw line within two points
  296. *
  297. * @param string $coord1
  298. * @param string $coord2
  299. * @param string $color
  300. * @param string $hint
  301. * @param string $width
  302. *
  303. * @return string
  304. */
  305. function generic_MapAddLine($coord1, $coord2, $color = '', $hint = '', $width = '') {
  306. $lineId = wf_InputId();
  307. $color = (!empty($color)) ? $color : '#000000';
  308. $width = (!empty($color)) ? $width + 1 : '1';
  309. $result = '';
  310. $result .= '
  311. var pointA = new L.LatLng(' . $coord1 . ');
  312. var pointB = new L.LatLng(' . $coord2 . ');
  313. var pointList = [pointA, pointB];
  314. var polyline_' . $lineId . ' = new L.Polyline(pointList, {
  315. color: \'' . $color . '\',
  316. weight: ' . $width . ',
  317. opacity: 0.8,
  318. smoothFactor: 1
  319. });
  320. polyline_' . $lineId . '.addTo(map);
  321. ';
  322. if (!empty($hint)) {
  323. $hint = str_replace('"', '\"', $hint);
  324. $result .= 'polyline_' . $lineId . '.bindTooltip("' . $hint . '", { sticky: true});';
  325. }
  326. return ($result);
  327. }
  328. ?>