| 13 | | * Creates a new marker to place on a map. |
| 14 | | * |
| 15 | | * @param string|float address|longitude |
| 16 | | * @param float latitude |
| 17 | | * @return object Gmap_Marker |
| 18 | | */ |
| 19 | | public static function marker($lon, $lat = NULL) |
| 20 | | { |
| 21 | | if ($lat === NULL) |
| 22 | | { |
| 23 | | // Get the latitude and longitude by address |
| 24 | | list ($lon, $lat) = Gmap::address_to_ll($address = $lon); |
| 25 | | } |
| 26 | | |
| 27 | | // Create a new marker |
| 28 | | $marker = new Gmap_Marker((float) $lat, (float) $lon); |
| 29 | | |
| 30 | | // Set the address of the marker |
| 31 | | isset($address) and $marker->address = $address; |
| 32 | | |
| 33 | | return $marker; |
| 34 | | } |
| 35 | | |
| 36 | | |
| 37 | | /** |
| 117 | | public function __construct($config = NULL) |
| 118 | | { |
| 119 | | empty($config) and $config = Config::item('gmaps'); |
| 120 | | } |
| 121 | | |
| 122 | | public function add_marker($value='') |
| 123 | | { |
| 124 | | # code... |
| | 95 | // Map settings |
| | 96 | protected $id; |
| | 97 | protected $options; |
| | 98 | protected $center; |
| | 99 | protected $control; |
| | 100 | |
| | 101 | // Map markers |
| | 102 | protected $markers; |
| | 103 | |
| | 104 | /** |
| | 105 | * Set the GMap center point. |
| | 106 | * |
| | 107 | * @param string HTML map id attribute |
| | 108 | * @param array array of GMap constructor options |
| | 109 | * @return void |
| | 110 | */ |
| | 111 | public function __construct($id = 'map', $options = NULL) |
| | 112 | { |
| | 113 | // Set map ID and options |
| | 114 | $this->id = $id; |
| | 115 | $this->options = new Gmap_Options((array) $options); |
| | 116 | } |
| | 117 | |
| | 118 | /** |
| | 119 | * Set the GMap center point. |
| | 120 | * |
| | 121 | * @chainable |
| | 122 | * @param float latitude |
| | 123 | * @param float longitude |
| | 124 | * @param integer zoom level (1-16) |
| | 125 | * @return object |
| | 126 | */ |
| | 127 | public function center($lat, $lon, $zoom = 6) |
| | 128 | { |
| | 129 | // Set center location and zoom |
| | 130 | $this->center = array($lat, $lon, $zoom); |
| | 131 | |
| | 132 | return $this; |
| | 133 | } |
| | 134 | |
| | 135 | /** |
| | 136 | * Set the GMap controls size. |
| | 137 | * |
| | 138 | * @chainable |
| | 139 | * @param string small or large |
| | 140 | * @return object |
| | 141 | */ |
| | 142 | public function controls($size = NULL) |
| | 143 | { |
| | 144 | // Set the control type |
| | 145 | $this->controls = (strtolower($size) === 'small') ? 'Small' : 'Large'; |
| | 146 | |
| | 147 | return $this; |
| | 148 | } |
| | 149 | |
| | 150 | /** |
| | 151 | * Set the GMap marker point. |
| | 152 | * |
| | 153 | * @chainable |
| | 154 | * @param float latitude |
| | 155 | * @param float longitude |
| | 156 | * @param string HTML for info window |
| | 157 | * @return object |
| | 158 | */ |
| | 159 | public function add_marker($lat, $lon, $html = '') |
| | 160 | { |
| | 161 | // Add a new marker |
| | 162 | $this->markers[] = new Gmap_Marker($lat, $lon, $html); |
| | 163 | |
| | 164 | return $this; |
| | 165 | } |
| | 166 | |
| | 167 | /** |
| | 168 | * Render the map into GMap Javascript. |
| | 169 | * |
| | 170 | * @return string |
| | 171 | */ |
| | 172 | public function render() |
| | 173 | { |
| | 174 | // Latitude, longitude, and zoom |
| | 175 | list ($lat, $lon, $zoom) = $this->center; |
| | 176 | |
| | 177 | // Map |
| | 178 | $map = 'map = new GMap2(document.getElementById("'.$this->id.'"));'; |
| | 179 | |
| | 180 | // Map controls |
| | 181 | $controls = empty($this->controls) ? '' : 'map.addControl(new G'.$this->controls.'MapControl());'; |
| | 182 | |
| | 183 | // Map centering |
| | 184 | $center = 'map.setCenter(new GLatLng('.$lat.', '.$lon.'));'; |
| | 185 | |
| | 186 | // Map zoom |
| | 187 | $zoom = 'map.setZoom('.$zoom.');'; |
| | 188 | |
| | 189 | // Render the Javascript |
| | 190 | return View::factory('gmaps/javascript', array |
| | 191 | ( |
| | 192 | 'map' => $map, |
| | 193 | 'options' => $this->options, |
| | 194 | 'controls' => $controls, |
| | 195 | 'center' => $center, |
| | 196 | 'zoom' => $zoom, |
| | 197 | 'markers' => $this->markers, |
| | 198 | )) |
| | 199 | ->render(); |