/
MarkerIcon.php
198 lines (171 loc) · 3.86 KB
/
MarkerIcon.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace PHPGoogleMaps\overlay;
/**
* Marker Icon class
* Attach these to markers to display custom marker icons
*
* Example:
* $icon = new \PHPGoogleMaps\Marker\MarkerIcon( $icon_url );
* $icon->setSize( 30, 30 );
* $shadow = \PHPGoogleMaps\Marker\MarkerIcon::create( $shadow_url )->setAnchor( 0, 30 );
* $marker->setIcon( $icon );
* $marker->setShadow( $shadow );
*/
class MarkerIcon extends \PHPGoogleMaps\Core\MapObject {
/**
* Url of the icon image
*
* @var string
*/
protected $icon;
/**
* Width of the icon
*
* @var integer
*/
protected $width;
/**
* Height of the icon
*
* @var integer
*/
protected $height;
/**
* X coord of the anchor
*
* @var integer
*/
protected $anchor_x;
/**
* Y coord of the anchor
*
* @var integer
*/
protected $anchor_y;
/**
* X coord of the origin
*
* @var integer
*/
protected $origin_x;
/**
* Y coord of the origin
*
* @var integer
*/
protected $origin_y;
/**
* Constructor
* Sets the icon's image URL, width, height, origin and anchor
* default origin ( 0, 0 )
* default anchor ( w/2, h )
*
* Throws an exception if the icon is invalid
*
* If allow_url_fopen is not enabled on your server you will need to explicity specify the dimensions of the icon
*
* @param strin $icon Absolute URL of the icon image
* @throws Exception
* @return MarkerIcon
*/
public function __construct( $icon, array $options = null ) {
$this->icon = $icon;
if ( !isset( $options['width'] ) || !isset( $options['height'] ) ) {
$size = @getimagesize( $icon );
list( $this->width, $this->height ) = $size;
}
if ( isset( $options['width'] ) ) {
$this->width = (int) $options['width'];
}
if ( isset( $options['height'] ) ) {
$this->height = (int) $options['height'];
}
$this->anchor_x = isset( $options['anchor_x'] ) ? $options['anchor_x'] : floor( $this->width / 2 );
$this->anchor_y = isset( $options['anchor_y'] ) ? $options['anchor_y'] : $this->height;
$this->origin_x = isset( $options['origin_x'] ) ? $options['origin_x'] : 0;
$this->origin_y = isset( $options['origin_y'] ) ? $options['origin_y'] : 0;
return $this;
}
/**
* Static create method useful for method chaining
*
* @param strin $icon URL of the icon image
* @throws Exception
* @return MarkerIcon
*/
public static function create( $icon, array $options=null ) {
return new MarkerIcon( $icon, $options );
}
/**
* Set the icon's height
*
* @param int $height
* @return MarkerIcon
*/
public function setHeight( $height ) {
$this->height = (int) $height;
return $this;
}
/**
* Set the icon's width
*
* @param int $width
* @return MarkerIcon
*/
public function setWidth( $width ) {
$this->width = (int) $width;
return $this;
}
/**
* Set the icon's size (width/height)
*
* @param int $width
* @param int $height
* @return MarkerIcon
*/
public function setSize( $width, $height ) {
$this->height = (int) $height;
$this->width = (int) $width;
return $this;
}
/**
* Set the icon's anchor point
* This is the point on the icon that will be placed on the map
*
* @param int $x X coord of the anchor
* @param int $y Y coord of the anchor
* @return MarkerIcon
*/
public function setAnchor( $x=null, $y=null ) {
if ( $x !== null ) {
$this->anchor_x = (int) $x;
}
if ( $y !== null ) {
$this->anchor_y = (int) $y;
}
return $this;
}
/**
* Set the icon's origin point
* This is used when you are using a sprite for the marker
*
* @param int $x X coord of the origin
* @param int $y Y coord of the origin
* @return MarkerIcon
*/
public function setOrigin( $x=null, $y=null ) {
if ( $x !== null ) {
$this->origin_x = (int) $x;
}
if ( $y !== null ) {
$this->origin_y = (int) $y;
}
return $this;
}
/**
* Return string
*/
public function __toString() {
return $this->icon;
}
}