api.gmaps.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. /*
  3. * Google maps API implementation
  4. */
  5. /**
  6. * Returns google 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 gm_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. * Initalizes google maps API with some params
  93. *
  94. * @param string $center
  95. * @param int $zoom
  96. * @param string $type
  97. * @param string $placemarks
  98. * @param bool $editor
  99. * @param string $lang
  100. * @param string $container
  101. * @param string $searchPrefill
  102. *
  103. * @return string
  104. */
  105. function generic_MapInit($center, $zoom, $type, $placemarks = '', $editor = '', $lang = 'uk-UA', $container = 'ubmap', $searchPrefill='') {
  106. global $ubillingConfig;
  107. $mapsCfg = $ubillingConfig->getYmaps();
  108. @$apikey = $mapsCfg['GMAPS_APIKEY'];
  109. $mapType = $mapsCfg['TYPE'];
  110. if ($mapType == 'map') {
  111. $mapType = 'roadmap';
  112. }
  113. $result = '';
  114. if ((!empty($apikey)) AND ( $apikey != 'YOUR_API_KEY_HERE')) {
  115. if (!empty($center)) {
  116. $center = explode(',', $center);
  117. $centerLat = trim($center[0]);
  118. $centerLng = trim($center[1]);
  119. $centerCode = 'center: uluru';
  120. $autoLocator = '';
  121. } else {
  122. $autoLocator = '
  123. if (navigator.geolocation) {
  124. navigator.geolocation.getCurrentPosition(success, error);
  125. } else {
  126. alert(\'geolocation not supported\');
  127. }
  128. function success(position) {
  129. map.setCenter(new google.maps.LatLng(position.coords.latitude, position.coords.longitude),' . $zoom . ');
  130. }
  131. function error(msg) {
  132. alert(\'error: \' + msg);
  133. }
  134. ';
  135. $centerLat = '48.5319';
  136. $centerLng = '30.0350';
  137. $centerCode = 'center: uluru';
  138. }
  139. $result .= wf_tag('script', false, '', 'type = "text/javascript"');
  140. $result .= ' function initMap() {
  141. var uluru = {lat: ' . $centerLat . ', lng: ' . $centerLng . '};
  142. var map = new google.maps.Map(document.getElementById(\'' . $container . '\'), {
  143. zoom: ' . $zoom . ',
  144. mapTypeId: \'' . $mapType . '\',
  145. ' . $centerCode . '
  146. });
  147. ' . $placemarks . '
  148. ' . $editor . '
  149. ' . $autoLocator . '
  150. }
  151. ';
  152. $result .= wf_tag('script', true);
  153. $result .= wf_tag('script', false, '', 'async defer type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=' . $apikey . '&language=' . $lang . '&callback=initMap"');
  154. $result .= wf_tag('script', true);
  155. } else {
  156. $messages = new UbillingMessageHelper();
  157. $result = $messages->getStyledMessage(__('No valid GMAPS_APIKEY set in ymaps.ini'), 'error');
  158. }
  159. return ($result);
  160. }
  161. /**
  162. * Returns placemark code
  163. *
  164. * @param string $coords
  165. * @param string $title
  166. * @param string $content
  167. * @param string $footer
  168. * @param string $icon
  169. * @param string $iconlabel
  170. * @param bool $canvas
  171. *
  172. * @return string
  173. */
  174. function generic_MapAddMark($coords, $title = '', $content = '', $footer = '', $icon = 'twirl#lightblueIcon', $iconlabel = '', $canvas = false) {
  175. $markerId = wf_InputId();
  176. if (!empty($coords)) {
  177. $coords = explode(',', $coords);
  178. $coordLat = trim($coords[0]);
  179. $coordLng = trim($coords[1]);
  180. }
  181. if (!empty($icon)) {
  182. $iconUrl = gm_GetIconUrl($icon);
  183. } else {
  184. $iconUrl = gm_GetIconUrl('twirl#lightblueIcon');
  185. }
  186. if (!empty($iconUrl)) {
  187. $iconCode = "var image_" . $markerId . " = '" . $iconUrl . "';";
  188. }
  189. if (!empty($title)) {
  190. $titleCode = '<strong>' . $title . '</strong><br>';
  191. } else {
  192. $titleCode = '';
  193. }
  194. if (!empty($title)) {
  195. $labelCode = "title: '" . $iconlabel . "',";
  196. } else {
  197. $labelCode = '';
  198. }
  199. if ((!empty($content)) OR ( !empty($footer))) {
  200. if (!empty($footer)) {
  201. $footerCode = '<div id="footer_' . $markerId . '" class="row3">' . $footer . '</div>';
  202. } else {
  203. $footerCode = '';
  204. }
  205. $contentWindow = 'var contentString_' . $markerId . ' = \'<div id = "content_' . $markerId . '">' . $titleCode . $content . $footerCode . '</div>\';
  206. var infowindow_' . $markerId . ' = new google.maps.InfoWindow({
  207. content: contentString_' . $markerId . '
  208. });
  209. google.maps.event.addListener(marker_' . $markerId . ', \'click\', function() {
  210. infowindow_' . $markerId . '.open(map,marker_' . $markerId . ');
  211. });
  212. ';
  213. } else {
  214. $contentWindow = '';
  215. }
  216. $result = '
  217. var position_' . $markerId . ' = {lat: ' . $coordLat . ', lng: ' . $coordLng . '};
  218. ' . $iconCode . '
  219. var marker_' . $markerId . ' = new google.maps.Marker({
  220. ' . $labelCode . '
  221. position: position_' . $markerId . ',
  222. map: map,
  223. icon: image_' . $markerId . '
  224. });
  225. ' . $contentWindow . '
  226. ';
  227. return ($result);
  228. }
  229. /**
  230. * Returns JS code to draw line within two points
  231. *
  232. * @param string $coord1
  233. * @param string $coord2
  234. * @param string $color
  235. * @param string $hint
  236. * @param string $width
  237. *
  238. * @return string
  239. */
  240. function generic_MapAddLine($coord1, $coord2, $color = '', $hint = '', $width = '') {
  241. $lineId = wf_InputId();
  242. $color = (!empty($color)) ? $color : '#000000';
  243. $width = (!empty($color)) ? $width : '1';
  244. $coord1 = explode(',', $coord1);
  245. $coord2 = explode(',', $coord2);
  246. $lat1 = $coord1[0];
  247. $lng1 = $coord1[1];
  248. $lat2 = $coord2[0];
  249. $lng2 = $coord2[1];
  250. if (!empty($hint)) {
  251. $tooltipCode = '
  252. var infoWindow_' . $lineId . ' = new google.maps.InfoWindow({
  253. content: \'' . $hint . '\'
  254. });
  255. google.maps.event.addListener(line_' . $lineId . ', \'mouseover\', function(e) {
  256. infoWindow_' . $lineId . '.setPosition(e.latLng);
  257. infoWindow_' . $lineId . '.open(map);
  258. });
  259. google.maps.event.addListener(line_' . $lineId . ', \'mouseout\', function() {
  260. infoWindow_' . $lineId . '.close();
  261. });';
  262. } else {
  263. $tooltipCode = '';
  264. }
  265. $result = '
  266. var linecoords_' . $lineId . ' = [
  267. {lat: ' . $lat1 . ', lng: ' . $lng1 . '},
  268. {lat: ' . $lat2 . ', lng: ' . $lng2 . '}
  269. ];
  270. var line_' . $lineId . ' = new google.maps.Polyline({
  271. path: linecoords_' . $lineId . ',
  272. geodesic: true,
  273. strokeColor: \'' . $color . '\',
  274. strokeOpacity: 1.0,
  275. strokeWeight: ' . $width . '
  276. });
  277. line_' . $lineId . '.setMap(map);
  278. ' . $tooltipCode . '
  279. ';
  280. return ($result);
  281. }
  282. /**
  283. * Returns circle map placemark
  284. *
  285. * @param string $coords - map coordinates
  286. * @param int $radius - circle radius in meters
  287. * @param string $content - popup balloon content
  288. * @param string $hint - on mouseover hint
  289. * @param string $color - circle border color, default: 009d25
  290. * @param float $opacity - border opacity from 0 to 1, default: 0.8
  291. * @param string $fillColor - fill color of circle, default: 00a20b55
  292. * @param float $fillOpacity - fill opacity from 0 to 1, default: 0.5
  293. *
  294. * @return string
  295. */
  296. function generic_MapAddCircle($coords, $radius, $content = '', $hint = '', $color = '009d25', $opacity = 0.8, $fillColor = '00a20b55', $fillOpacity = 0.5) {
  297. $circelId = wf_InputId();
  298. $coords = explode(',', $coords);
  299. $lat = $coords[0];
  300. $lng = $coords[1];
  301. $result = '
  302. var circlecoords_' . $circelId . ' = {lat: ' . $lat . ', lng: ' . $lng . '} ;
  303. var cicrcle_' . $circelId . ' = new google.maps.Circle({
  304. strokeColor: \'#' . $color . '\',
  305. strokeOpacity: ' . $opacity . ',
  306. strokeWeight: 1,
  307. fillColor: \'#' . $fillColor . '\',
  308. fillOpacity: ' . $fillOpacity . ',
  309. map: map,
  310. center: circlecoords_' . $circelId . ',
  311. radius: ' . $radius . '
  312. });
  313. ';
  314. return ($result);
  315. }
  316. /**
  317. * Return generic editor code
  318. *
  319. * @param string $name
  320. * @param string $title
  321. * @param string $data
  322. *
  323. * @return string
  324. */
  325. function generic_MapEditor($name, $title = '', $data = '') {
  326. $windowId = wf_InputId();
  327. $data = str_replace("'", '`', $data);
  328. $data = str_replace("\n", '', $data);
  329. $content = '<form action="" method="POST"><input type="hidden" name="' . $name . '" value="\'+lat+\', \'+lng+\'">' . $data . '</form>';
  330. $windowCode = 'var contentString_' . $windowId . ' = \'<div id = "content_' . $windowId . '">' . $title . '<br> \'+lat+\', \'+lng+\' <br> ' . $content . '</div>\';
  331. var infowindow_' . $windowId . ' = new google.maps.InfoWindow({
  332. content: contentString_' . $windowId . '
  333. });';
  334. $result = '
  335. google.maps.event.addListener(map, \'click\', function(event) {
  336. var myLatLng = event.latLng;
  337. var lat = myLatLng.lat().toPrecision(7);
  338. var lng = myLatLng.lng().toPrecision(7);
  339. ' . $windowCode . '
  340. infowindow_' . $windowId . '.setPosition(event.latLng);
  341. infowindow_' . $windowId . '.open(map);
  342. });
  343. ';
  344. return ($result);
  345. }
  346. ?>