├── README.md ├── classes ├── gmap.php └── gmap │ └── core.php ├── config └── gmap.php ├── tests └── gmap.php └── views └── gmap.php /README.md: -------------------------------------------------------------------------------- 1 | # Kohana Google Maps Module V1.3 2 | With this module you can easily add a Google Map to your Kohana installation! 3 | 4 | The Google Map module has NO dependency to other modules! 5 | 6 | ## Features 7 | 8 | * Display a Google-Map by just echo'ing an instance of the "Gmap" class 9 | * Setting several map-types (road, satellite, hybrid and terrain) 10 | * Setting sensor-parameter (for mobile devices) 11 | * Adding markers to the map (including custom icon and Google Maps popups) 12 | * Adding polylines to the map 13 | * Adding polygons to the map 14 | * Setting the positions and types for the map-controls 15 | 16 | ## Features to come 17 | The following features shall be implemented during development: 18 | 19 | * Cleaning up the Google Map-view (Maybe moving the javascript to a sub-views?) 20 | 21 | ## Usage 22 | The usage of this module is as easy as it could be! Simply activate the module in your bootstrap.php 23 | 24 | /** 25 | * Enable modules. Modules are referenced by a relative or absolute path. 26 | */ 27 | Kohana::modules(array( 28 | // ... 29 | 'gmap' => MODPATH.'gmap', // A simple google-maps module 30 | )); 31 | 32 | Then you'll just echo an instance of the gmap class in your action... For example: 33 | 34 | public function action_index() 35 | { 36 | $this->template->map = Gmap::factory(); 37 | } // function 38 | 39 | This is a more advanced example with usage of various options... 40 | 41 | public function action_index() 42 | { 43 | $gmap = Gmap::factory( 44 | array( 45 | 'zoom' => 4, 46 | 'sensor' => FALSE, 47 | )) 48 | ->add_marker('Marker A', 51.15, 6.83) 49 | ->add_marker('Marker B', 51.15, 6.93, 50 | array( 51 | 'content' => '

Put HTML here. "Quotes" and \'singlequotes\'

', 52 | 'icon' => '/path/to/your.icon' 53 | )) 54 | ->set_gmap_size('100%', 500); // Will output "width: 100%; height: 500px" 55 | 56 | // This will render the Google Map. 57 | $this->template->map = $gmap; 58 | 59 | // Or... 60 | $this->template->map = $gmap->render(); 61 | 62 | // Or rendering a own Google Map view. 63 | $this->template->map = $gmap->render('gmap_view_2'); 64 | } // function 65 | 66 | Yes, it's that easy ;) 67 | 68 | ## More! 69 | For more information look up the wiki! -------------------------------------------------------------------------------- /classes/gmap.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright (c) 2011 Leonard Fischer 8 | * @version 1.3 9 | */ 10 | class Gmap extends Gmap_Core {} -------------------------------------------------------------------------------- /classes/gmap/core.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright (c) 2011 Leonard Fischer 8 | * @version 1.3 9 | */ 10 | class Gmap_Core 11 | { 12 | protected $_config = NULL; 13 | protected $_options = array( 14 | 'lat', 15 | 'lng', 16 | 'zoom', 17 | 'sensor', 18 | 'instance', 19 | 'maptype', 20 | 'view', 21 | 'gmap_size_x', 22 | 'gmap_size_y', 23 | 'gmap_controls', 24 | ); 25 | protected static $instances = array(); 26 | protected $marker = array(); 27 | protected $polylines = array(); 28 | protected $polygons = array(); 29 | protected $view = NULL; 30 | protected static $maptypes = array( 31 | 'road' => 'google.maps.MapTypeId.ROADMAP', 32 | 'satellite' => 'google.maps.MapTypeId.SATELLITE', 33 | 'hybrid' => 'google.maps.MapTypeId.HYBRID', 34 | 'terrain' => 'google.maps.MapTypeId.TERRAIN', 35 | ); 36 | protected static $control_maptypes = array( 37 | 'horizontal_bar' => 'google.maps.MapTypeControlStyle.HORIZONTAL_BAR', 38 | 'dropdown_menu' => 'google.maps.MapTypeControlStyle.DROPDOWN_MENU', 39 | 'default' => 'google.maps.MapTypeControlStyle.DEFAULT', 40 | ); 41 | protected static $control_navigation = array( 42 | 'small' => 'google.maps.NavigationControlStyle.SMALL', 43 | 'zoom_pan' => 'google.maps.NavigationControlStyle.ZOOM_PAN', 44 | 'android' => 'google.maps.NavigationControlStyle.ANDROID', 45 | 'default' => 'google.maps.NavigationControlStyle.DEFAULT', 46 | ); 47 | protected static $control_positions = array( 48 | 'top' => 'google.maps.ControlPosition.TOP', 49 | 'top_left' => 'google.maps.ControlPosition.TOP_LEFT', 50 | 'top_right' => 'google.maps.ControlPosition.TOP_RIGHT', 51 | 'bottom' => 'google.maps.ControlPosition.BOTTOM', 52 | 'bottom_left' => 'google.maps.ControlPosition.BOTTOM_LEFT', 53 | 'bottom_right' => 'google.maps.ControlPosition.BOTTOM_RIGHT', 54 | 'left' => 'google.maps.ControlPosition.LEFT', 55 | 'right' => 'google.maps.ControlPosition.RIGHT', 56 | ); 57 | 58 | /** 59 | * The factory method for instant method-chaining. 60 | * 61 | * @param array $options 62 | * @return Gmap 63 | */ 64 | public static function factory(array $options = array()) 65 | { 66 | return new Gmap($options); 67 | } // function 68 | 69 | /** 70 | * Constructor for the Google-Map class. 71 | * 72 | * @param array $options 73 | */ 74 | public function __construct(array $options = array()) 75 | { 76 | $available_keys = $this->_options; 77 | 78 | $this->_config = Kohana::config('gmap'); 79 | $this->_options = array(); 80 | 81 | // Check if each available key is set. Using Arr::extract filled everything up with NULL. 82 | foreach ($available_keys as $key) 83 | { 84 | if (isset($options[$key])) 85 | { 86 | $this->_options[$key] = $options[$key]; 87 | } // if 88 | } // foreach 89 | 90 | unset($available_keys); 91 | } // function 92 | 93 | /** 94 | * Renders the google-map template. 95 | * 96 | * @return string 97 | */ 98 | public function __toString() 99 | { 100 | return $this->render(); 101 | } // function 102 | 103 | /** 104 | * Add a marker to the map. 105 | * 106 | * @uses URL::title 107 | * @uses Arr::extract 108 | * @param string $id 109 | * @param float $lat 110 | * @param float $lng 111 | * @param array $options 112 | * @return Gmap 113 | */ 114 | public function add_marker($id, $lat, $lng, array $options = array()) 115 | { 116 | Gmap::validate_latitude($lat); 117 | Gmap::validate_longitude($lng); 118 | 119 | $available_options = array( 120 | 'title', 121 | 'content', 122 | 'icon', 123 | ); 124 | 125 | if (! isset($options['title']) OR empty($options['title'])) 126 | { 127 | $options['title'] = $id; 128 | } // if 129 | 130 | $this->marker[$id] = array( 131 | 'id' => URL::title($id, '_', TRUE), 132 | 'lat' => $lat, 133 | 'lng' => $lng, 134 | 'options' => Arr::extract($options, $available_options), 135 | ); 136 | 137 | return $this; 138 | } // function 139 | 140 | /** 141 | * Add a polygon to the map. 142 | * 143 | * @uses URL::title 144 | * @uses Arr::extract 145 | * @uses Validate::color 146 | * @param string $id 147 | * @param array $coordinates 148 | * @param array $options 149 | * @return Gmap 150 | */ 151 | public function add_polygon($id, array $coordinates, array $options = array()) 152 | { 153 | if (! isset($options['strokeColor']) OR ! Validate::color($options['strokeColor'])) 154 | { 155 | $options['strokeColor'] = $this->_config->default_polygon_options['strokeColor']; 156 | } // if 157 | 158 | if (! isset($options['strokeOpacity'])) 159 | { 160 | $options['strokeOpacity'] = $this->_config->default_polygon_options['strokeOpacity']; 161 | } // if 162 | 163 | if (! isset($options['strokeWeight'])) 164 | { 165 | $options['strokeWeight'] = $this->_config->default_polygon_options['strokeWeight']; 166 | } // if 167 | 168 | if (! isset($options['fillColor']) OR ! Validate::color($options['fillColor'])) 169 | { 170 | $options['fillColor'] = $this->_config->default_polygon_options['fillColor']; 171 | } // if 172 | 173 | if (! isset($options['fillOpacity'])) 174 | { 175 | $options['fillOpacity'] = $this->_config->default_polygon_options['fillOpacity']; 176 | } // if 177 | 178 | $available_options = array( 179 | 'strokeColor', 180 | 'strokeOpacity', 181 | 'strokeWeight', 182 | 'fillColor', 183 | 'fillOpacity', 184 | ); 185 | 186 | $this->polygons[$id] = array( 187 | 'id' => URL::title($id, '_', TRUE), 188 | 'coords' => $coordinates, 189 | 'options' => Arr::extract($options, $available_options), 190 | ); 191 | 192 | return $this; 193 | } // function 194 | 195 | /** 196 | * Add a polyline to the map. 197 | * 198 | * @uses URL::title 199 | * @uses Arr::extract 200 | * @uses Validate::color 201 | * @param string $id 202 | * @param array $coordinates 203 | * @param array $options 204 | * @return Gmap 205 | */ 206 | public function add_polyline($id, array $coordinates, array $options = array()) 207 | { 208 | if (! isset($options['strokeColor']) OR ! Validate::color($options['strokeColor'])) 209 | { 210 | $options['strokeColor'] = $this->_config->default_polyline_options['strokeColor']; 211 | } // if 212 | 213 | if (! isset($options['strokeOpacity'])) 214 | { 215 | $options['strokeOpacity'] = $this->_config->default_polyline_options['strokeOpacity']; 216 | } // if 217 | 218 | if (! isset($options['strokeWeight'])) 219 | { 220 | $options['strokeWeight'] = $this->_config->default_polyline_options['strokeWeight']; 221 | } // if 222 | 223 | $available_options = array( 224 | 'strokeColor', 225 | 'strokeOpacity', 226 | 'strokeWeight', 227 | ); 228 | 229 | $this->polylines[$id] = array( 230 | 'id' => URL::title($id, '_', TRUE), 231 | 'coords' => $coordinates, 232 | 'options' => Arr::extract($options, $available_options), 233 | ); 234 | 235 | return $this; 236 | } // function 237 | 238 | /** 239 | * Cleanes the JSON strings by removing the quotes from google-variables. 240 | * 241 | * @param string $str 242 | * @return string 243 | */ 244 | public static function clean_json_string($str) 245 | { 246 | return preg_replace('~"(google\.(.*?))"~', '$1', $str); 247 | } // function 248 | 249 | /** 250 | * Get the current options from this instance. 251 | * 252 | * @param string $key 253 | * @return mixed 254 | */ 255 | public function get_option($key = NULL) 256 | { 257 | if ($key === NULL) 258 | { 259 | return $this->_options; 260 | } // if 261 | 262 | if (array_key_exists($key, $this->_options)) 263 | { 264 | return $this->_options[$key]; 265 | } // if 266 | 267 | return FALSE; 268 | } // function 269 | 270 | /** 271 | * Renders the google-map template. 272 | * 273 | * @uses Text::random() 274 | * @uses Arr::merge() 275 | * @param string $view Defines a view for rendering. 276 | * @return string 277 | */ 278 | public function render($view = '') 279 | { 280 | // Look, if there's a name for this instance. If not, set one. 281 | if (empty($this->_options['instance'])) 282 | { 283 | $this->set_instance_name(Text::random('alpha', 8)); 284 | } // if 285 | 286 | Gmap::$instances[] = $this->_options['instance']; 287 | 288 | $temp = array(); 289 | 290 | foreach ((Array) $this->_config as $key => $value) 291 | { 292 | $temp[str_replace('default_', '', $key)] = $value; 293 | } // foreach 294 | 295 | // Override the config-defaults with your setted options. 296 | $this->_options = Arr::merge($temp, $this->_options); 297 | unset($temp); 298 | 299 | // Set the map-type. 300 | $this->set_maptype($this->_options['maptype']); 301 | 302 | // Set the latitude. 303 | $this->set_pos($this->_options['lat'], NULL); 304 | 305 | // Set the longitude. 306 | $this->set_pos(NULL, $this->_options['lng']); 307 | 308 | // Set the Google Map size. 309 | $this->set_gmap_size($this->_options['gmap_size_x'], $this->_options['gmap_size_y']); 310 | 311 | // Set the styles for the Google Map controls. 312 | $this->_options['gmap_controls']['maptype']['style'] = Gmap::validate_control_maptype($this->_options['gmap_controls']['maptype']['style']); 313 | $this->_options['gmap_controls']['navigation']['style'] = Gmap::validate_control_navigation($this->_options['gmap_controls']['navigation']['style']); 314 | 315 | // Set the positions for the Google Map controls. 316 | $this->_options['gmap_controls']['maptype']['position'] = Gmap::validate_control_position($this->_options['gmap_controls']['maptype']['position']); 317 | $this->_options['gmap_controls']['navigation']['position'] = Gmap::validate_control_position($this->_options['gmap_controls']['navigation']['position']); 318 | $this->_options['gmap_controls']['scale']['position'] = Gmap::validate_control_position($this->_options['gmap_controls']['scale']['position']); 319 | 320 | // If we set the view parameter in this method, use it! 321 | if (! empty($view)) 322 | { 323 | $this->_options['view'] = $view; 324 | } 325 | elseif ($this->_options['view'] === NULL) 326 | { 327 | $this->_options['view'] = $this->_config->default_view; 328 | } // if 329 | 330 | // Bind the necessary variables. 331 | $this->view = View::factory($this->_options['view']) 332 | ->bind('options', $this->_options) 333 | ->bind('marker', $this->marker) 334 | ->bind('polylines', $this->polylines) 335 | ->bind('polygons', $this->polygons) 336 | ->bind('instances', Gmap::$instances); 337 | 338 | // Render the view. 339 | return $this->view->render(); 340 | } // function 341 | 342 | /** 343 | * Set some controls for your gmap. 344 | * You can specify how to display the map-type and navigation control. 345 | * For more information visit https://github.com/solidsnake/Kohana-Google-Maps-Module/wiki . 346 | * 347 | * @param array $options Set the options for the gmap-controls here. 348 | * @return Gmap 349 | */ 350 | public function set_gmap_controls(array $options) 351 | { 352 | if (isset($options['maptype'])) 353 | { 354 | $this->set_gmap_controls_maptype($options['maptype']); 355 | } // if 356 | 357 | if (isset($options['navigation'])) 358 | { 359 | $this->set_gmap_controls_navigation($options['navigation']); 360 | } // if 361 | 362 | if (isset($options['scale'])) 363 | { 364 | $this->set_gmap_controls_scale($options['scale']); 365 | } // if 366 | 367 | return $this; 368 | } // function 369 | 370 | /** 371 | * Set the maptype controls for your gmap. 372 | * For more information visit https://github.com/solidsnake/Kohana-Google-Maps-Module/wiki . 373 | * 374 | * @param array $options Set the options for the controls here. 375 | * @return Gmap 376 | */ 377 | public function set_gmap_controls_maptype(array $options) 378 | { 379 | if (isset($options['display'])) 380 | { 381 | $this->_options['gmap_controls']['maptype']['display'] = (Bool) $options['display']; 382 | } // if 383 | 384 | if (isset($options['style'])) 385 | { 386 | $this->_options['gmap_controls']['maptype']['style'] = Gmap::validate_control_maptype($options['style']); 387 | } // if 388 | 389 | if (isset($options['position'])) 390 | { 391 | $this->_options['gmap_controls']['maptype']['position'] = Gmap::validate_control_position($options['position']); 392 | } // if 393 | 394 | return $this; 395 | } // function 396 | 397 | /** 398 | * Set the navigation controls for your gmap. 399 | * For more information visit https://github.com/solidsnake/Kohana-Google-Maps-Module/wiki . 400 | * 401 | * @param array $options Set the options for the controls here. 402 | * @return Gmap 403 | */ 404 | public function set_gmap_controls_navigation(array $options) 405 | { 406 | if (isset($options['display'])) 407 | { 408 | $this->_options['gmap_controls']['navigation']['display'] = (Bool) $options['display']; 409 | } // if 410 | 411 | if (isset($options['style'])) 412 | { 413 | $this->_options['gmap_controls']['navigation']['style'] = Gmap::validate_control_navigation($options['style']); 414 | } // if 415 | 416 | if (isset($options['position'])) 417 | { 418 | $this->_options['gmap_controls']['navigation']['position'] = Gmap::validate_control_position($options['position']); 419 | } // if 420 | 421 | return $this; 422 | } // function 423 | 424 | /** 425 | * Set the scale controls for your gmap. 426 | * For more information visit https://github.com/solidsnake/Kohana-Google-Maps-Module/wiki 427 | * 428 | * @param array $options Set the options for the controls here. 429 | * @return Gmap 430 | */ 431 | public function set_gmap_controls_scale(array $options) 432 | { 433 | if (isset($options['display'])) 434 | { 435 | $this->_options['gmap_controls']['scale']['display'] = (Bool) $options['display']; 436 | } // if 437 | 438 | if (isset($options['position'])) 439 | { 440 | $this->_options['gmap_controls']['scale']['position'] = Gmap::validate_control_position($options['position']); 441 | } // if 442 | 443 | return $this; 444 | } // function 445 | 446 | /** 447 | * Set the options for this instance. 448 | * 449 | * @param array $values 450 | * @return mixed 451 | */ 452 | public function set_gmap_options(array $values) 453 | { 454 | foreach ($values as $key => $value) 455 | { 456 | $this->_options[$key] = $value; 457 | } // foreach 458 | 459 | return $this; 460 | } // function 461 | 462 | /** 463 | * Set a size for the rendered Google-Map. 464 | * You may set a CSS attribute like for example "500px", "50%" or "10em". 465 | * If you just set an integer, "px" will be used. 466 | * 467 | * @param mixed $x May be a CSS attribute ("500px", "50%", "10em") or an int. 468 | * @param mixed $y May be a CSS attribute ("500px", "50%", "10em") or an int. 469 | * @return Gmap 470 | */ 471 | public function set_gmap_size($x = NULL, $y = NULL) 472 | { 473 | if ($x != NULL) 474 | { 475 | $this->_options['gmap_size_x'] = (is_numeric($x)) ? $x . 'px' : $x; 476 | } // if 477 | 478 | if ($y != NULL) 479 | { 480 | $this->_options['gmap_size_y'] = (is_numeric($y)) ? $y . 'px' : $y; 481 | } // if 482 | 483 | return $this; 484 | } // function 485 | 486 | /** 487 | * Set a name for this instance. 488 | * 489 | * @uses URL::title() 490 | * @param string $name 491 | * @return Gmap 492 | */ 493 | public function set_instance_name($name) 494 | { 495 | $this->_options['instance'] = URL::title($name, '_', TRUE); 496 | 497 | return $this; 498 | } // function 499 | 500 | /** 501 | * Set another map-type. Possible types are 'road', 'satellite', 'hybrid' and 'terrain'. 502 | * 503 | * @param string $maptype 504 | * @return Gmap 505 | */ 506 | public function set_maptype($maptype) 507 | { 508 | $this->_options['maptype'] = Gmap::validate_maptype($maptype); 509 | 510 | return $this; 511 | } // function 512 | 513 | /** 514 | * Set a new position to show, when starting up the map. 515 | * 516 | * @param float $lat 517 | * @param float $lng 518 | * @return Gmap 519 | */ 520 | public function set_pos($lat = NULL, $lng = NULL) 521 | { 522 | if ($lat != NULL) 523 | { 524 | $this->_options['lat'] = Gmap::validate_latitude($lat); 525 | } // if 526 | 527 | if ($lng != NULL) 528 | { 529 | $this->_options['lng'] = Gmap::validate_longitude($lng); 530 | } // if 531 | 532 | return $this; 533 | } // function 534 | 535 | /** 536 | * Set the sensor-parameter for the google-api. 537 | * 538 | * @param boolean $sensor 539 | * @return Gmap 540 | */ 541 | public function set_sensor($sensor) 542 | { 543 | if (! is_bool($sensor)) 544 | { 545 | throw new Kohana_Exception('The parameter must be boolean. ":sensor" given', 546 | array(':sensor' => $sensor)); 547 | } // if 548 | 549 | $this->_options['sensor'] = $sensor; 550 | 551 | return $this; 552 | } // function 553 | 554 | /** 555 | * Set the view for displaying the Google-map. 556 | * 557 | * @param string $view 558 | * @return Gmap 559 | */ 560 | public function set_view($view) 561 | { 562 | $this->_options['view'] = $view; 563 | 564 | return $this; 565 | } // function 566 | 567 | /** 568 | * Set the zoom level for the Google-map. 569 | * 570 | * @param int $zoom 571 | * @return Gmap 572 | */ 573 | public function set_zoom($zoom) 574 | { 575 | $this->_options['zoom'] = $zoom; 576 | 577 | return $this; 578 | } // function 579 | 580 | /** 581 | * Validate, if the given maptype-control is valid. 582 | * 583 | * @param string $maptype 584 | * @return string 585 | */ 586 | protected static function validate_control_maptype($maptype) 587 | { 588 | $maptype = trim($maptype); 589 | 590 | if (! array_key_exists($maptype, Gmap::$control_maptypes) AND ! in_array($maptype, Gmap::$control_maptypes)) 591 | { 592 | throw new Kohana_Exception('Given Map-Type ":maptype" control is not valid.', 593 | array(':maptype' => $maptype)); 594 | } // if 595 | 596 | if (array_key_exists($maptype, Gmap::$control_maptypes)) 597 | { 598 | return Gmap::$control_maptypes[$maptype]; 599 | } 600 | elseif (in_array($maptype, Gmap::$control_maptypes)) 601 | { 602 | return $maptype; 603 | } // if 604 | } // function 605 | 606 | /** 607 | * Validate, if the given maptype-control is valid. 608 | * 609 | * @param string $navigation 610 | * @return string 611 | */ 612 | protected static function validate_control_navigation($navigation) 613 | { 614 | $navigation = trim($navigation); 615 | 616 | if (! array_key_exists($navigation, Gmap::$control_navigation) AND ! in_array($navigation, Gmap::$control_navigation)) 617 | { 618 | throw new Kohana_Exception('Given Navigation-control ":navigation" is not valid.', 619 | array(':navigation' => $navigation)); 620 | } // if 621 | 622 | if (array_key_exists($navigation, Gmap::$control_navigation)) 623 | { 624 | return Gmap::$control_navigation[$navigation]; 625 | } 626 | elseif (in_array($navigation, Gmap::$control_navigation)) 627 | { 628 | return $navigation; 629 | } // if 630 | } // function 631 | 632 | /** 633 | * Validate, if the given position is supported. 634 | * 635 | * @param string $position 636 | * @return string 637 | */ 638 | protected static function validate_control_position($position = NULL) 639 | { 640 | if ($position === NULL) 641 | { 642 | return NULL; 643 | } // if 644 | 645 | $position = trim($position); 646 | 647 | if (! array_key_exists($position, Gmap::$control_positions) AND ! in_array($position, Gmap::$control_positions)) 648 | { 649 | throw new Kohana_Exception('":position" is no supported position.', 650 | array(':position' => $position)); 651 | } // if 652 | 653 | if (array_key_exists($position, Gmap::$control_positions)) 654 | { 655 | return Gmap::$control_positions[$position]; 656 | } 657 | elseif (in_array($position, Gmap::$control_positions)) 658 | { 659 | return $position; 660 | } // if 661 | } // function 662 | 663 | /** 664 | * Validate, if the latitude is in bounds. 665 | * 666 | * @param float $lat 667 | * @return float 668 | */ 669 | protected static function validate_latitude($lat) 670 | { 671 | 672 | if ($lat > 90 OR $lat < -90) 673 | { 674 | throw new Kohana_Exception('Latitude has to lie between -90.0 and 90.0! Set to ":lat"', 675 | array(':lng' => $lat)); 676 | } // if 677 | 678 | return $lat; 679 | } // function 680 | 681 | /** 682 | * Validate, if the longitude is in bounds. 683 | * 684 | * @param float $lng 685 | * @return float 686 | */ 687 | protected static function validate_longitude($lng) 688 | { 689 | if ($lng > 180 OR $lng < -180) 690 | { 691 | throw new Kohana_Exception('Longitude has to lie between -180.0 and 180.0! Set to ":lng"', 692 | array(':lng' => $lng)); 693 | } // if 694 | 695 | return $lng; 696 | } // function 697 | 698 | /** 699 | * Validate, if the given map-type is supported. 700 | * 701 | * @param string $maptype 702 | * @return string 703 | */ 704 | protected static function validate_maptype($maptype) 705 | { 706 | $maptype = trim($maptype); 707 | 708 | if (! array_key_exists($maptype, Gmap::$maptypes) AND ! in_array($maptype, Gmap::$maptypes)) 709 | { 710 | throw new Kohana_Exception('":maptype" is no supported map-type.', 711 | array(':maptype' => $maptype)); 712 | } // if 713 | 714 | if (array_key_exists($maptype, Gmap::$maptypes)) 715 | { 716 | return Gmap::$maptypes[$maptype]; 717 | } 718 | elseif (in_array($maptype, Gmap::$maptypes)) 719 | { 720 | return $maptype; 721 | } // if 722 | } // function 723 | } // class -------------------------------------------------------------------------------- /config/gmap.php: -------------------------------------------------------------------------------- 1 | 51.1731, 8 | 'default_lng' => 6.8328, 9 | 10 | // Default zoom-level. 11 | 'default_zoom' => 12, 12 | 13 | // Default "sensor" setting - Used for mobile devices. 14 | 'default_sensor' => FALSE, 15 | 16 | // Default map-type. 17 | 'default_maptype' => 'road', 18 | 19 | // The instance will be set in the render method to a random string (if this value is empty). 20 | 'instance' => '', 21 | 22 | // Default view-options. 23 | 'default_view' => 'gmap', 24 | 'default_gmap_size_x' => '100%', 25 | 'default_gmap_size_y' => '100%', 26 | 27 | // Default Google Maps controls. 28 | 'default_gmap_controls' => array( 29 | 'maptype' => array( 30 | 'display' => TRUE, 31 | 'style' => 'default', 32 | 'position' => NULL, 33 | ), 34 | 'navigation' => array( 35 | 'display' => TRUE, 36 | 'style' => 'default', 37 | 'position' => NULL, 38 | ), 39 | 'scale' => array( 40 | 'display' => TRUE, 41 | 'position' => NULL, 42 | ), 43 | ), 44 | 45 | // Default options for polylines. 46 | 'default_polyline_options' => array( 47 | 'strokeColor' => '#000', 48 | 'strokeOpacity' => 1, 49 | 'strokeWeight' => 3, 50 | ), 51 | 52 | // Default options for polygons. 53 | 'default_polygon_options' => array( 54 | 'strokeColor' => '#000', 55 | 'strokeOpacity' => 1, 56 | 'strokeWeight' => 3, 57 | 'fillColor' => '#000', 58 | 'fillOpacity' => .5, 59 | ), 60 | ); -------------------------------------------------------------------------------- /tests/gmap.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | Class GmapTest extends Kohana_Unittest_TestCase 11 | { 12 | /** 13 | * Provides maptype test data() 14 | * 15 | * @return array 16 | */ 17 | public function provider_maptype() 18 | { 19 | return array( 20 | array('road'), 21 | array('satellite'), 22 | array('hybrid'), 23 | array('terrain'), 24 | ); 25 | } 26 | 27 | /** 28 | * Tests the exception, thrown by the validate_latitude method. 29 | * 30 | * @test 31 | * @covers Gmap::set_pos 32 | * @covers Gmap::validate_latitude 33 | * @expectedException Kohana_Exception 34 | */ 35 | function test_validate_latitude_exception_if_higher_than_180() 36 | { 37 | $map = new Gmap(); 38 | $map->set_pos(180.1, NULL); 39 | } // function 40 | 41 | /** 42 | * Tests the exception, thrown by the validate_latitude method. 43 | * 44 | * @test 45 | * @covers Gmap::set_pos 46 | * @covers Gmap::validate_latitude 47 | * @expectedException Kohana_Exception 48 | */ 49 | function test_validate_latitude_exception_if_lower_than_minus180() 50 | { 51 | $map = new Gmap(); 52 | $map->set_pos(-180.1, NULL); 53 | } // function 54 | 55 | /** 56 | * Tests the exception, thrown by the validate_longitude method. 57 | * 58 | * @test 59 | * @covers Gmap::set_pos 60 | * @covers Gmap::validate_longitude 61 | * @expectedException Kohana_Exception 62 | */ 63 | function test_validate_longitude_exception_if_higher_than_90() 64 | { 65 | $map = new Gmap(); 66 | $map->set_pos(NULL, 90.1); 67 | } // function 68 | 69 | /** 70 | * Tests the exception, thrown by the validate_longitude method. 71 | * 72 | * @test 73 | * @covers Gmap::set_pos 74 | * @covers Gmap::validate_longitude 75 | * @expectedException Kohana_Exception 76 | */ 77 | function test_validate_longitude_exception_if_lower_than_minus90() 78 | { 79 | $map = new Gmap(); 80 | $map->set_pos(NULL, -90.1); 81 | } // function 82 | 83 | /** 84 | * Tests the validate_maptype method. 85 | * 86 | * @test 87 | * @covers Gmap::set_maptype 88 | * @covers Gmap::validate_maptype 89 | * @dataProvider provider_maptype 90 | */ 91 | function test_validate_maptype($maptype) 92 | { 93 | $map = new Gmap(); 94 | $this->assertSame($map ,$map->set_maptype($maptype)); 95 | } // function 96 | 97 | /** 98 | * Tests the exception, thrown by the validate_maptype method. 99 | * 100 | * @test 101 | * @covers Gmap::set_maptype 102 | * @covers Gmap::validate_maptype 103 | * @expectedException Kohana_Exception 104 | */ 105 | function test_validate_maptype_exception() 106 | { 107 | $map = new Gmap(); 108 | $map->set_maptype('NotExisting'); 109 | } // function 110 | 111 | /** 112 | * Tests the setting options through the constructor. 113 | * 114 | * @test 115 | * @covers Gmap::__construct 116 | * @covers Gmap::get_option 117 | */ 118 | function test_constructor_options_parameter() 119 | { 120 | $options = array( 121 | 'abc' => 123, 122 | 'def' => TRUE, 123 | 'ghi' => FALSE, 124 | 'jkl' => array(), 125 | 'lat' => 12.34, 126 | 'lng' => 34.56, 127 | 'zoom' => 10, 128 | 'sensor' => TRUE, 129 | 'maptype' => 'terrain', 130 | 'view' => 'gmap_demo', 131 | 'gmap_size_x' => 666, 132 | 'gmap_size_y' => 333, 133 | 'gmap_controls' => array( 134 | 'maptype' => array('display' => FALSE), 135 | 'navigation' => array('display' => FALSE), 136 | 'scale' => array('display' => FALSE), 137 | ), 138 | ); 139 | $map = new Gmap($options); 140 | 141 | $expected = array( 142 | 'lat' => 12.34, 143 | 'lng' => 34.56, 144 | 'zoom' => 10, 145 | 'sensor' => TRUE, 146 | 'maptype' => 'terrain', 147 | 'view' => 'gmap_demo', 148 | 'gmap_size_x' => 666, 149 | 'gmap_size_y' => 333, 150 | 'gmap_controls' => array( 151 | 'maptype' => array('display' => FALSE), 152 | 'navigation' => array('display' => FALSE), 153 | 'scale' => array('display' => FALSE), 154 | ), 155 | ); 156 | 157 | $this->assertEquals($expected, $map->get_option()); 158 | } // function 159 | 160 | /** 161 | * Tests the factory method. 162 | * 163 | * @test 164 | * @covers Gmap::__construct 165 | * @covers Gmap::factory 166 | */ 167 | function test_factory_metod() 168 | { 169 | $this->assertEquals(new Gmap(), Gmap::factory()); 170 | } // function 171 | } // class -------------------------------------------------------------------------------- /views/gmap.php: -------------------------------------------------------------------------------- 1 | 2 | 101 |
--------------------------------------------------------------------------------