/Model/Map.php

https://github.com/asm89/IvoryGoogleMapBundle · PHP · 497 lines · 212 code · 62 blank · 223 comment · 36 complexity · d75060cfaa4be975bb3b99665a8f3a9f MD5 · raw file

  1. <?php
  2. namespace Ivory\GoogleMapBundle\Model;
  3. /**
  4. * Map wich describes a google map
  5. *
  6. * @see http://code.google.com/apis/maps/documentation/javascript/reference.html#Map
  7. * @author GeLo <geloen.eric@gmail.com>
  8. */
  9. class Map extends AbstractAsset
  10. {
  11. /**
  12. * @var string HTML container ID
  13. */
  14. protected $htmlContainerId = 'map_canvas';
  15. /**
  16. * @var boolean TRUE if the map autozoom else FALSE
  17. */
  18. protected $autoZoom = false;
  19. /**
  20. * @var Ivory\GoogleMapBundle\Model\Coordinate Map center
  21. */
  22. protected $center = null;
  23. /**
  24. * @var Ivory\GoogleMapBundle\Model\Bound Map bound
  25. */
  26. protected $bound = null;
  27. /**
  28. * @var array Map options
  29. * @see http://code.google.com/apis/maps/documentation/javascript/reference.html#MapOptions
  30. */
  31. protected $mapOptions = array(
  32. 'mapTypeId' => 'roadmap',
  33. 'zoom' => 10
  34. );
  35. /**
  36. * @var array Map stylesheet options
  37. */
  38. protected $stylesheetOptions = array(
  39. 'width' => '300px',
  40. 'height' => '300px'
  41. );
  42. /**
  43. * @var Ivory\GoogleMapBundle\Model\EventManager Map event manager
  44. */
  45. protected $eventManager = null;
  46. /**
  47. * @var array Map markers
  48. */
  49. protected $markers = array();
  50. /**
  51. * @var array Map info windows
  52. */
  53. protected $infoWindows = array();
  54. /**
  55. * @var array Map polylines
  56. */
  57. protected $polylines = array();
  58. /**
  59. * @var array Map polygons
  60. */
  61. protected $polygons = array();
  62. /**
  63. * @var array Map rectangles
  64. */
  65. protected $rectangles = array();
  66. /**
  67. * @var array Map circles
  68. */
  69. protected $circles = array();
  70. /**
  71. * @var array Map ground overlays
  72. */
  73. protected $groundOverlays = array();
  74. /**
  75. * Create a map
  76. */
  77. public function __construct()
  78. {
  79. parent::__construct();
  80. $this->setPrefixJavascriptVariable('map_');
  81. $this->center = new Coordinate();
  82. $this->bound = new Bound();
  83. $this->eventManager = new EventManager();
  84. }
  85. /**
  86. * Gets the map HTML container ID
  87. *
  88. * @return string
  89. */
  90. public function getHtmlContainerId()
  91. {
  92. return $this->htmlContainerId;
  93. }
  94. /**
  95. * Sets the map HTML container ID
  96. *
  97. * @param string $htmlContainerId
  98. */
  99. public function setHtmlContainerId($htmlContainerId)
  100. {
  101. $this->htmlContainerId = $htmlContainerId;
  102. }
  103. /**
  104. * Check if the map autozoom
  105. *
  106. * @return boolean TRUE if the map autozoom else FALSE
  107. */
  108. public function isAutoZoom()
  109. {
  110. return $this->autoZoom;
  111. }
  112. /**
  113. * Sets if the map autozoom
  114. *
  115. * @param boolean $autoZoom TRUE if the map autozoom else FALSE
  116. */
  117. public function setAutoZoom($autoZoom)
  118. {
  119. $this->autoZoom = $autoZoom;
  120. }
  121. /**
  122. * Gets the map center
  123. *
  124. * @return Ivroy\GoogleMapBundle\Model\Coordinate
  125. */
  126. public function getCenter()
  127. {
  128. return $this->center;
  129. }
  130. /**
  131. * Sets the map center
  132. *
  133. * Available prototype:
  134. *
  135. * public function setCenter(Ivory\GoogleMapBundle\Model\Coordinate $center)
  136. * public function setCenter(integer $latitude, integer $longitude, boolean $noWrap = true)
  137. */
  138. public function setCenter()
  139. {
  140. $args = func_get_args();
  141. if(isset($args[0]) && is_int($args[0]) && isset($args[1]) && is_int($args[1]))
  142. {
  143. $this->center->setLatitude($args[0]);
  144. $this->center->setLongitude($args[1]);
  145. if(isset($args[2]) && is_bool($args[2]))
  146. $this->center->setNoWrap($args[2]);
  147. }
  148. else if(isset($args[0]) && ($args[0] instanceof Coordinate))
  149. $this->center = $args[0];
  150. else
  151. throw new \InvalidArgumentException();
  152. }
  153. /**
  154. * Gets the map bound
  155. *
  156. * @return Ivory\GoogleMapBundle\Model\Bound
  157. */
  158. public function getBound()
  159. {
  160. return $this->bound;
  161. }
  162. /**
  163. * Sets the map bound
  164. *
  165. * Available prototype:
  166. *
  167. * public function setBound(Ivory\GoogleMapBundle\Model\Bound $bound)
  168. * public function setBount(Ivory\GoogleMapBundle\Model\Coordinate $southWest, Ivory\GoogleMapBundle\Model\Coordinate $northEast)
  169. * public function setBound(integer $southWestLatitude, integer $southWestLongitude, integer $northEastLatitude, integer $northEastLongitude, boolean southWestNoWrap = true, boolean $northEastNoWrap = true)
  170. */
  171. public function setBound()
  172. {
  173. $args = func_get_args();
  174. if(isset($args[0]) && ($args[0] instanceof Bound))
  175. $this->bound = $args[0];
  176. else if(isset($args[0]) && ($args[0] instanceof Coordinate) && isset($args[1]) && ($args[1] instanceof Coordinate))
  177. {
  178. $this->bound->setSouthWest($args[0]);
  179. $this->bound->setNorthEast($args[1]);
  180. }
  181. else if(isset($args[0]) && is_int($args[0]) && isset($args[1]) && is_int($args[1]) && isset($args[2]) && is_int($args[2]) && isset($args[3]) && is_int($args[3]))
  182. {
  183. $this->bound->setSouthWest(new Coordinate($args[0], $args[1]));
  184. $this->bound->setNorthEast(new Coordinate($args[2], $args[3]));
  185. if(isset($args[4]) && is_bool($args[4]))
  186. {
  187. $this->bound->getSouthWest()->setNoWrap($args[4]);
  188. if(isset($args[5]) && is_bool($args[5]))
  189. $this->bound->getNorthEast()->setNoWrap($args[5]);
  190. }
  191. }
  192. else
  193. throw new \InvalidArgumentException();
  194. }
  195. /**
  196. * Gets the map options
  197. *
  198. * @return array
  199. */
  200. public function getMapOptions()
  201. {
  202. return $this->mapOptions;
  203. }
  204. /**
  205. * Sets the map options
  206. *
  207. * @param array $mapOptions
  208. */
  209. public function setMapOptions(array $mapOptions)
  210. {
  211. $this->mapOptions = array_merge(
  212. $this->mapOptions,
  213. $mapOptions
  214. );
  215. }
  216. /**
  217. * Gets a specific map option
  218. *
  219. * @param string $mapOption
  220. * @return mixed
  221. */
  222. public function getMapOption($mapOption)
  223. {
  224. return isset($this->mapOptions[$mapOption]) ? $this->mapOptions[$mapOption] : null;
  225. }
  226. /**
  227. * Sets a specific map option
  228. *
  229. * @param string $mapOption
  230. * @param mixed $value
  231. */
  232. public function setMapOption($mapOption, $value)
  233. {
  234. $this->mapOptions[$mapOption] = $value;
  235. }
  236. /**
  237. * Gets the stylesheet map options
  238. *
  239. * @return array
  240. */
  241. public function getStylesheetOptions()
  242. {
  243. return $this->stylesheetOptions;
  244. }
  245. /**
  246. * Sets the stylesheet map options
  247. *
  248. * @param array $stylesheetOptions
  249. */
  250. public function setStylesheetOptions(array $stylesheetOptions)
  251. {
  252. $this->stylesheetOptions = array_merge(
  253. $this->stylesheetOptions,
  254. $stylesheetOptions
  255. );
  256. }
  257. /**
  258. * Gets a specific stylesheet map option
  259. *
  260. * @param string $stylesheetOption
  261. * @return mixed
  262. */
  263. public function getStylesheetOption($stylesheetOption)
  264. {
  265. return isset($this->stylesheetOptions[$stylesheetOption]) ? $this->stylesheetOptions[$stylesheetOption] : null;
  266. }
  267. /**
  268. * Sets a specific stylesheet map option
  269. *
  270. * @param string $stylesheetOption
  271. * @param mixed $value
  272. */
  273. public function setStylesheetOption($stylesheetOption, $value)
  274. {
  275. $this->stylesheetOptions[$stylesheetOption] = $value;
  276. }
  277. /**
  278. * Gets the map event manager
  279. *
  280. * @return Ivory\GoogleMapBundle\Model\EventManager
  281. */
  282. public function getEventManager()
  283. {
  284. return $this->eventManager;
  285. }
  286. /**
  287. * Sets the map event manager
  288. *
  289. * @param Ivory\GoogleMapBundle\Model\EventManager $eventManager
  290. */
  291. public function setEventManager(EventManager $eventManager)
  292. {
  293. $this->eventManager = $eventManager;
  294. }
  295. /**
  296. * Gets the map markers
  297. *
  298. * @return array
  299. */
  300. public function getMarkers()
  301. {
  302. return $this->markers;
  303. }
  304. /**
  305. * Add a map marker
  306. *
  307. * @param Ivory\GoogleMapBundle\Model\Marker $marker
  308. */
  309. public function addMarker(Marker $marker)
  310. {
  311. $this->markers[] = $marker;
  312. if($this->autoZoom)
  313. $this->bound->extend($marker);
  314. }
  315. /**
  316. * Gets the map info windows
  317. *
  318. * @return array
  319. */
  320. public function getInfoWindows()
  321. {
  322. return $this->infoWindows;
  323. }
  324. /**
  325. * Add a map info window
  326. *
  327. * @param Ivory\GoogleMapBundle\Model\InfoWindow $infoWindow
  328. */
  329. public function addInfoWindow(InfoWindow $infoWindow)
  330. {
  331. $this->infoWindows[] = $infoWindow;
  332. if($this->autoZoom)
  333. $this->bound->extend($infoWindow);
  334. }
  335. /**
  336. * Gets the map polylines
  337. *
  338. * @return Ivory\GoogleMapBundle\Model\Polyline
  339. */
  340. public function getPolylines()
  341. {
  342. return $this->polylines;
  343. }
  344. /**
  345. * Add a map polyline
  346. *
  347. * @param Ivory\GoogleMapBundle\Model\Polyline $polyline
  348. */
  349. public function addPolyline(Polyline $polyline)
  350. {
  351. $this->polylines[] = $polyline;
  352. if($this->autoZoom)
  353. $this->bound->extend($polyline);
  354. }
  355. /**
  356. * Gets the map polygons
  357. *
  358. * @return array
  359. */
  360. public function getPolygons()
  361. {
  362. return $this->polygons;
  363. }
  364. /**
  365. * Add a map polygon
  366. *
  367. * @param Ivory\GoogleMapBundle\Model\Polygon $polygon
  368. */
  369. public function addPolygon(Polygon $polygon)
  370. {
  371. $this->polygons[] = $polygon;
  372. if($this->autoZoom)
  373. $this->bound->extend($polygon);
  374. }
  375. /**
  376. * Gets the map rectangles
  377. *
  378. * @return array
  379. */
  380. public function getRectangles()
  381. {
  382. return $this->rectangles;
  383. }
  384. /**
  385. * Add a map rectangle to the map
  386. *
  387. * @param Ivory\GoogleMapBundle\Model\Rectangle $rectangle
  388. */
  389. public function addRectangle(Rectangle $rectangle)
  390. {
  391. $this->rectangles[] = $rectangle;
  392. if($this->autoZoom)
  393. $this->bound->extend($rectangle);
  394. }
  395. /**
  396. * Gets the map circles
  397. *
  398. * @return array
  399. */
  400. public function getCircles()
  401. {
  402. return $this->circles;
  403. }
  404. /**
  405. * Add a circle to the map
  406. *
  407. * @param Ivory\GoogleMapBundle\Model\Circle $circle
  408. */
  409. public function addCircle(Circle $circle)
  410. {
  411. $this->circles[] = $circle;
  412. if($this->autoZoom)
  413. $this->bound->extend($circle);
  414. }
  415. /**
  416. * Gets the map ground overlays
  417. *
  418. * @return array
  419. */
  420. public function getGroundOverlays()
  421. {
  422. return $this->groundOverlays;
  423. }
  424. /**
  425. * Add a ground overlay to the map
  426. *
  427. * @param Ivory\GoogleMapBundle\Model\GroupOverlay $groundOverlay
  428. */
  429. public function addGroundOverlay(GroundOverlay $groundOverlay)
  430. {
  431. $this->groundOverlays[] = $groundOverlay;
  432. if($this->autoZoom)
  433. $this->bound->extend($groundOverlay);
  434. }
  435. }