You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.1KB

  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2013-2015 2amigOS! Consulting Group LLC
  4. * @link http://2amigos.us
  5. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  6. */
  7. namespace dosamigos\leaflet\layers;
  8. use dosamigos\leaflet\types\Icon;
  9. use yii\base\InvalidConfigException;
  10. use yii\web\JsExpression;
  11. /**
  12. * Marker is used to put a marker on the map
  13. *
  14. * @see http://leafletjs.com/reference.html#circle
  15. * @author Antonio Ramirez <amigo.cobos@gmail.com>
  16. * @link http://www.ramirezcobos.com/
  17. * @link http://www.2amigos.us/
  18. * @package dosamigos\leaflet\layers
  19. */
  20. /**
  21. * @property string $name
  22. * @property \dosamigos\leaflet\types\LatLng $latLng
  23. * @property string $popupContent
  24. * @property bool $openPopup
  25. */
  26. class Marker extends Layer
  27. {
  28. use LatLngTrait;
  29. use PopupTrait;
  30. /**
  31. * Sets the marker's icon
  32. *
  33. * @param Icon $icon
  34. */
  35. public function setIcon($icon) //Icon - if you force the icon as type, the makimarker won't work...:(
  36. {
  37. $this->clientOptions['icon'] = $icon;
  38. }
  39. /**
  40. * @return \dosamigos\leaflet\types\Icon
  41. */
  42. public function getIcon()
  43. {
  44. return isset($this->clientOptions['icon']) ? $this->clientOptions['icon'] : null;
  45. }
  46. /**
  47. * Initializes the marker.
  48. * @throws \yii\base\InvalidConfigException
  49. */
  50. public function init()
  51. {
  52. parent::init();
  53. if (empty($this->latLng)) {
  54. throw new InvalidConfigException("'latLng' attribute cannot be empty.");
  55. }
  56. }
  57. /**
  58. * @return \yii\web\JsExpression the marker constructor string
  59. */
  60. public function encode()
  61. {
  62. $latLon = $this->getLatLng()->toArray(true);
  63. $options = $this->getOptions();
  64. $name = $this->name;
  65. $map = $this->map;
  66. $js = $this->bindPopupContent("L.marker($latLon, $options)") . ($map !== null ? ".addTo($map)" : "");
  67. if (!empty($name)) {
  68. $js = "var $name = $js;";
  69. }
  70. $js .= $this->getEvents() . ($map !== null && empty($name)? ";" : "");
  71. return new JsExpression($js);
  72. }
  73. }