api.gmaps.php 12 KB

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