Development

/plugins/sfEasyGMapPlugin/trunk/lib/GMapMarker.class.php

You must first sign up to be able to contribute.

root/plugins/sfEasyGMapPlugin/trunk/lib/GMapMarker.class.php

Revision 21816, 5.6 kB (checked in by laurentb, 3 years ago)

[sfEasyGMapPlugin][trunk] Tabs to spaces

Line 
1 <?php
2
3 /*
4  *
5  * A GoogleMap Marker
6  * @author Fabrice Bernhard
7  *
8  */
9 class GMapMarker
10 {
11    /**
12    * javascript name of the marker
13    *
14    * @var string
15    */
16   protected $js_name        = null;
17   /**
18    * Latitude - deprecated
19    *
20    * @var float
21    */
22   protected $lat            = null;
23   /**
24    * Longitude - deprecated
25    *
26    * @var float
27    */
28   protected $lng            = null;
29   /**
30    * Coordinates
31    *
32    * @var GMapCoord
33    */
34   protected $coord          = null;
35   protected $icon           = null;
36   protected $events         = array();
37   protected $custom_properties = array();
38  
39   /**
40    * @param string $js_name Javascript name of the marker
41    * @param float $lat Latitude
42    * @param float $lng Longitude
43    * @param GMapIcon $icon
44    * @param GmapEvent[] array of GoogleMap Events linked to the marker
45    * @author Fabrice Bernhard
46    */
47   public function __construct($lat,$lng,$js_name='marker',$icon=null,$events=array())
48   {
49     $this->js_name = $js_name;
50     $this->coord   = new GMapCoord($lat,$lng);
51     $this->icon    = $icon;
52     $this->events  = $events;   
53   }
54  
55   /**
56    * Construct from a GMapGeocodedAddress object
57    *
58    * @param string $js_name
59    * @param GMapGeocodedAddress $gmap_geocoded_address
60    * @return GMapMarker
61    */
62   public static function constructFromGMapGeocodedAddress($gmap_geocoded_address,$js_name='marker')
63   {
64     if (!$gmap_geocoded_address instanceof GMapGeocodedAddress)
65     {
66       throw new sfException('object passed to constructFromGMapGeocodedAddress is not a GMapGeocodedAddress');
67     }
68     
69     return new GMapMarker($js_name,$gmap_geocoded_address->getLat(),$gmap_geocoded_address->getLng());
70   }
71  
72   /**
73   * @return string $js_name Javascript name of the marker 
74   */
75   public function getName()
76   {
77     
78     return $this->js_name;
79   }
80   /**   
81   * @return GMapIcon $icon 
82   */
83   public function getIcon()
84   {
85     return $this->icon;
86   }
87  
88   /**
89    * returns the coordinates object of the marker
90    *
91    * @return GMapCoord
92    * @author fabriceb
93    * @since 2009-05-02
94    */
95   public function getGMapCoord()
96   {
97  
98     return $this->coord;
99   }
100  
101   /**
102   * @return float $lat Javascript latitude 
103   */
104   public function getLat()
105   {
106     
107     return $this->getGMapCoord()->getLatitude();
108   }
109   /**
110   * @return float $lng Javascript longitude 
111   */
112   public function getLng()
113   {
114     
115     return $this->getGMapCoord()->getLongitude();
116   }
117  
118   public function getIconName()
119   {
120     if ($this->getIcon() instanceof GMapIcon)
121     {
122       
123       return $this->getIcon()->getName();
124     }
125     
126     return $this->getIcon();
127   }
128   /**
129   * @return string Javascript code to create the marker
130   * @author Fabrice Bernhard
131   */
132   public function getMarkerJs()
133   {
134     if ($this->getIconName() != '')
135     {
136       $markerOptionsJs = ', { icon:'.$this->getIconName().' }';
137     }
138     else
139     {
140       $markerOptionsJs = '';
141     }
142     $pointJs = 'new google.maps.LatLng('.$this->getLat().','.$this->getLng().')';
143     $return = '';
144     $return .= $this->getName().' = new google.maps.Marker('.$pointJs.$markerOptionsJs.');';
145     foreach ($this->custom_properties as $attribute=>$value)
146     {
147       $return .= $this->getName().".".$attribute." = '".$value."';";
148     }
149     foreach ($this->events as $event)
150     {
151       $return .= $event->getEventJs($this->getName());
152     }   
153     
154     return $return;
155   }
156  
157   /**
158    * Adds an event listener to the marker
159    *
160    * @param GMapEvent $event
161    */
162   public function addEvent($event)
163   {
164     array_push($this->events,$event);
165   }
166   /**
167    * Adds an onlick listener that open a html window with some text
168    *
169    * @param string $html_text
170    * @author fabriceb
171    * @since Feb 20, 2009 fabriceb removed the escape_javascript function which made the plugin incompatible with symfony 1.2
172    */
173   public function addHtmlInfoWindow($html_text)
174   {
175     $javascript = preg_replace('/\r\n|\n|\r/', "\\n", $html_text);
176     $javascript = preg_replace('/(["\'])/', '\\\\\1', $javascript);
177     
178     $this->addEvent(new GMapEvent('click',"this.openInfoWindowHtml('".$javascript."')"));
179   }
180
181   /**
182    * Returns the code for the static version of Google Maps
183    * @TODO Add support for color and alpha-char
184    * @author Laurent Bachelier
185    * @return string
186    */
187   public function getMarkerStatic()
188   {
189     
190     return $this->getLat().','.$this->getLng();
191   }
192   public function setCustomProperties($custom_properties)
193   {
194     $this->custom_properties=$custom_properties;
195   }
196   public function getCustomProperties()
197   {
198     
199     return $this->custom_properties;
200   }
201   /**
202    * Sets a custom property to the generated javascript object
203    *
204    * @param string $name
205    * @param string $value
206    */
207   public function setCustomProperty($name,$value)
208   {
209     $this->custom_properties[$name] = $value;
210   }
211  
212   /**
213   *
214   * @param GMapMarker[] $markers array of MArkers
215   * @return GMapCoord
216   * @author fabriceb
217   * @since 2009-05-02
218   *
219   **/
220   public static function getMassCenterCoord($markers)
221   {
222     $coords = array();
223     foreach($markers as $marker)
224     {
225       array_push($coords, $marker->getGMapCoord());
226     }
227   
228     return GMapCoord::getMassCenterCoord($coords);
229   }
230  
231   /**
232   *
233   * @param GMapMarker[] $markers array of MArkers
234   * @return GMapCoord
235   * @author fabriceb
236   * @since 2009-05-02
237   *
238   **/
239   public static function getCenterCoord($markers)
240   {
241     $bounds = GMapBounds::getBoundsContainingMarkers($markers);
242  
243     return $bounds->getCenterCoord();
244   }
245  
246   /**
247    *
248    * @param GMapBounds $gmap_bounds
249    * @return boolean $is_inside
250    * @author fabriceb
251    * @since Jun 2, 2009 fabriceb
252    */
253   public function isInsideBounds(GMapBounds $gmap_bounds)
254   {
255  
256     return $this->getGMapCoord()->isInsideBounds($gmap_bounds);
257   }
258  
259 }
260
Note: See TracBrowser for help on using the browser.