api.ymaps.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /*
  3. * Yandex maps API implementation
  4. */
  5. /**
  6. * Returns JS code to draw line within two points
  7. *
  8. * @param string $coord1
  9. * @param string $coord2
  10. * @param string $color
  11. * @param string $hint
  12. * @param string $width
  13. *
  14. * @return string
  15. */
  16. function generic_MapAddLine($coord1, $coord2, $color = '', $hint = '', $width = '') {
  17. $hint = (!empty($hint)) ? 'hintContent: "' . $hint . '"' : '';
  18. $color = (!empty($color)) ? $color : '#000000';
  19. $width = (!empty($color)) ? $width : '1';
  20. $result = '
  21. var myPolyline = new ymaps.Polyline([[' . $coord1 . '],[' . $coord2 . ']],
  22. {' . $hint . '},
  23. {
  24. draggable: false,
  25. strokeColor: \'' . $color . '\',
  26. strokeWidth: \'' . $width . '\'
  27. }
  28. );
  29. myMap.geoObjects.add(myPolyline);
  30. ';
  31. return ($result);
  32. }
  33. /**
  34. * Initalizes maps API with some params
  35. *
  36. * @param string $center
  37. * @param int $zoom
  38. * @param string $type
  39. * @param string $placemarks
  40. * @param bool $editor
  41. * @param string $lang
  42. * @param string $container
  43. *
  44. * @return string
  45. */
  46. function generic_MapInit($center, $zoom, $type, $placemarks = '', $editor = '', $lang = 'ru-RU', $container = 'ubmap') {
  47. global $ubillingConfig;
  48. if (empty($center)) {
  49. $center = 'ymaps.geolocation.latitude, ymaps.geolocation.longitude';
  50. } else {
  51. $center = $center;
  52. }
  53. $mapsCfg = $ubillingConfig->getYmaps();
  54. $yandexApiKey = @$mapsCfg['YMAPS_APIKEY'];
  55. if ($yandexApiKey) {
  56. $yandexApiKey = '&apikey=' . $yandexApiKey;
  57. } else {
  58. $yandexApiKey = '';
  59. }
  60. $apiUrl = 'https://api-maps.yandex.ru/2.0/';
  61. $result = wf_tag('script', false, '', 'src="' . $apiUrl . '?load=package.full&lang=' . $lang . $yandexApiKey . '" type="text/javascript"');
  62. $result .= wf_tag('script', true);
  63. $result .= wf_tag('script', false, '', 'type="text/javascript"');
  64. $result .= '
  65. ymaps.ready(init);
  66. function init () {
  67. var myMap = new ymaps.Map(\'' . $container . '\', {
  68. center: [' . $center . '],
  69. zoom: ' . $zoom . ',
  70. type: \'yandex#' . $type . '\',
  71. behaviors: [\'default\',\'scrollZoom\']
  72. })
  73. myMap.controls
  74. .add(\'zoomControl\')
  75. .add(\'typeSelector\')
  76. .add(\'mapTools\')
  77. .add(\'searchControl\');
  78. ' . $placemarks . '
  79. ' . $editor . '
  80. }';
  81. $result .= wf_tag('script', true);
  82. return ($result);
  83. }
  84. /**
  85. * Returns circle map placemark
  86. *
  87. * @param string $coords - map coordinates
  88. * @param int $radius - circle radius in meters
  89. * @param string $content - popup balloon content
  90. * @param string $hint - on mouseover hint
  91. * @param string $color - circle border color, default: 009d25
  92. * @param float $opacity - border opacity from 0 to 1, default: 0.8
  93. * @param string $fillColor - fill color of circle, default: 00a20b55
  94. * @param float $fillOpacity - fill opacity from 0 to 1, default: 0.5
  95. *
  96. * @return string
  97. */
  98. function generic_MapAddCircle($coords, $radius, $content = '', $hint = '', $color = '009d25', $opacity = 0.8, $fillColor = '00a20b55', $fillOpacity = 0.5) {
  99. $result = '
  100. myCircle = new ymaps.Circle([
  101. [' . $coords . '],
  102. ' . $radius . '
  103. ], {
  104. balloonContent: "' . $content . '",
  105. hintContent: "' . $hint . '"
  106. }, {
  107. draggable: true,
  108. fillColor: "#' . $fillColor . '",
  109. strokeColor: "#' . $color . '",
  110. strokeOpacity: ' . $opacity . ',
  111. fillOpacity: ' . $fillOpacity . ',
  112. strokeWidth: 1
  113. });
  114. myMap.geoObjects.add(myCircle);
  115. ';
  116. return ($result);
  117. }
  118. /**
  119. * Returns map mark
  120. *
  121. * @param string $coords - map coordinates
  122. * @param string $title - ballon title
  123. * @param string $content - ballon content
  124. * @param string $footer - ballon footer content
  125. * @param string $icon - YM icon class
  126. * @param string $iconlabel - icon label string
  127. * @param string $canvas
  128. *
  129. * @return string
  130. */
  131. function generic_mapAddMark($coords, $title = '', $content = '', $footer = '', $icon = 'twirl#lightblueIcon', $iconlabel = '', $canvas = '') {
  132. if ($canvas) {
  133. if ($iconlabel == '') {
  134. $overlay = 'overlayFactory: "default#interactiveGraphics"';
  135. } else {
  136. $overlay = '';
  137. }
  138. } else {
  139. $overlay = '';
  140. }
  141. if (!wf_CheckGet(array('clusterer'))) {
  142. $markType = 'myMap.geoObjects';
  143. } else {
  144. $markType = 'clusterer';
  145. }
  146. $result = '
  147. myPlacemark = new ymaps.Placemark([' . $coords . '], {
  148. iconContent: \'' . $iconlabel . '\',
  149. balloonContentHeader: \'' . $title . '\',
  150. balloonContentBody: \'' . $content . '\',
  151. balloonContentFooter: \'' . $footer . '\',
  152. hintContent: "' . $content . '",
  153. } , {
  154. draggable: false,
  155. preset: \'' . $icon . '\',
  156. ' . $overlay . '
  157. }),
  158. ' . $markType . '.add(myPlacemark);
  159. ';
  160. return($result);
  161. }
  162. /**
  163. * Returns maps empty container
  164. *
  165. * @param string $width
  166. * @param string $height
  167. * @param string $id
  168. *
  169. * @return string
  170. */
  171. function generic_MapContainer($width = '', $height = '', $id = '') {
  172. $width = (!empty($width)) ? $width : '100%';
  173. $height = (!empty($height)) ? $height : '800px';
  174. $id = (!empty($id)) ? $id : 'ubmap';
  175. $result = wf_tag('div', false, '', 'id="' . $id . '" style="width:' . $width . '; height:' . $height . ';"');
  176. $result .= wf_tag('div', true);
  177. return ($result);
  178. }
  179. /**
  180. * Return generic editor code
  181. *
  182. * @param string $name
  183. * @param string $title
  184. * @param string $data
  185. *
  186. * @return string
  187. */
  188. function generic_MapEditor($name, $title = '', $data = '') {
  189. $data = str_replace("'", '`', $data);
  190. $data = str_replace("\n", '', $data);
  191. $result = '
  192. myMap.events.add(\'click\', function (e) {
  193. if (!myMap.balloon.isOpen()) {
  194. var coords = e.get(\'coordPosition\');
  195. myMap.balloon.open(coords, {
  196. contentHeader: \'' . $title . '\',
  197. contentBody: \' \' +
  198. \'<p>\' + [
  199. coords[0].toPrecision(7),
  200. coords[1].toPrecision(7)
  201. ].join(\', \') + \'</p> <form action="" method="POST"><input type="hidden" name="' . $name . '" value="\'+coords[0].toPrecision(7)+\', \'+coords[1].toPrecision(7)+\'">' . $data . '</form> \'
  202. });
  203. } else {
  204. myMap.balloon.close();
  205. }
  206. });
  207. ';
  208. return ($result);
  209. }
  210. ?>