1 : <?php
2 : /**
3 : * This file is part of NoiseLabs-PHP-ToolKit
4 : *
5 : * NoiseLabs-PHP-ToolKit is free software; you can redistribute it
6 : * and/or modify it under the terms of the GNU Lesser General Public
7 : * License as published by the Free Software Foundation; either
8 : * version 3 of the License, or (at your option) any later version.
9 : *
10 : * NoiseLabs-PHP-ToolKit is distributed in the hope that it will be
11 : * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 : * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : * Lesser General Public License for more details.
14 : *
15 : * You should have received a copy of the GNU Lesser General Public
16 : * License along with NoiseLabs-PHP-ToolKit; if not, see
17 : * <http://www.gnu.org/licenses/>.
18 : *
19 : * Copyright (C) 2011 Vítor Brandão
20 : *
21 : * @category NoiseLabs
22 : * @package GoogleAPI
23 : * @author Vítor Brandão <noisebleed@noiselabs.org>
24 : * @copyright (C) 2011 Vítor Brandão <noisebleed@noiselabs.org>
25 : * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL-3
26 : * @link http://www.noiselabs.org
27 : * @since 0.1.0
28 : */
29 :
30 : namespace NoiseLabs\ToolKit\GoogleAPI\Maps;
31 :
32 : use NoiseLabs\ToolKit\ParameterBag;
33 : use NoiseLabs\ToolKit\GoogleAPI\Maps\MapInterface;
34 : use NoiseLabs\ToolKit\GoogleAPI\Maps\Marker;
35 :
36 : /**
37 : * GoogleMaps base class (abstract).
38 : *
39 : * Inspired by a GoogleMaps implementation made by tirnanog06.
40 : * @see https://github.com/kriswallsmith/GoogleBundle
41 : */
42 0 : abstract class BaseMap implements MapInterface
43 : {
44 : /**
45 : * An unique ID to identify this map, useful when manipulating an array of
46 : * maps. This is also used as the ID for the <div> element.
47 : *
48 : * @var string
49 : */
50 : protected $id;
51 :
52 : /**
53 : * A collection of map markers. Each element of the array should be an
54 : * object of type Marker.
55 : */
56 : protected $markers = array();
57 :
58 : /**
59 : * A set of parameters to append to configure how the Maps JavaScript API is
60 : * loaded.
61 : *
62 : * @var \NoiseLabs\ToolKit\ParameterBag
63 : */
64 : public $parameters;
65 :
66 : /**
67 : * GoogleMaps configuration. This covers settings like zoom level to the
68 : * usage of HTTPS.
69 : *
70 : * @var \NoiseLabs\ToolKit\ParameterBag
71 : */
72 : public $options;
73 :
74 : public $https = false;
75 :
76 : /**
77 : *
78 : */
79 : public function __construct($id = 'map', array $options = array(), array $parameters = array())
80 : {
81 0 : $this->id = $id;
82 :
83 : // option defaults
84 0 : $this->options = new ParameterBag($this->getOptionsDefaults());
85 0 : $this->options->add($options);
86 :
87 : // parameter defaults
88 0 : $this->parameters = new ParameterBag(array(
89 : 'sensor' => 'false'
90 0 : ));
91 0 : $this->parameters->add($parameters);
92 0 : }
93 :
94 : protected function getOptionsDefaults()
95 : {
96 0 : $options = array();
97 :
98 : /**
99 : * Zoom level. Ranges from 0 (less zoom) to 18 (higher zoom).
100 : *
101 : * Offering a map of the entire Earth as a single image would either
102 : * require an immense map, or a small map with very low resolution.
103 : * As a result, map images within Google Maps and the Maps API are
104 : * broken up into map "tiles" and "zoom levels." At low zoom levels, a
105 : * small set of map tiles covers a wide area; at higher zoom levels,
106 : * the tiles are of higher resolution and cover a smaller area.
107 : *
108 : * You specify the resolution at which to display a map by setting the
109 : * Map's zoom property, where zoom 0 corresponds to a map of the Earth
110 : * fully zoomed out, and higher zoom levels zoom in at a higher
111 : * resolution.
112 : */
113 0 : $options['zoom'] = 12;
114 :
115 : /**
116 : * Map type. The following types are supported:
117 : * - 'ROADMAP' Displays the normal, default 2D tiles of Google Maps.
118 : * - 'SATELLITE' Displays photographic tiles.
119 : * - 'HYBRID' Displays a mix of photographic tiles and a tile layer
120 : * for prominent features (roads, city names).
121 : * - 'TERRAIN' Displays physical relief tiles for displaying elevation
122 : * and water features (mountains, rivers, etc.).
123 : */
124 0 : $options['type'] = 'ROADMAP';
125 :
126 0 : $options['width'] = '400px';
127 0 : $options['height'] = '400px';
128 :
129 0 : $options['https'] = false; // use https?
130 :
131 0 : $options['center'] = 0; // center map on desidered Marker (array index)
132 :
133 0 : return $options;
134 : }
135 :
136 : public function setId($id)
137 : {
138 0 : $this->id = (string) $id;
139 0 : }
140 :
141 : public function getId()
142 : {
143 0 : return $this->id;
144 : }
145 :
146 : public function hasMarkers()
147 : {
148 0 : if (!empty($this->markers)) {
149 0 : return true;
150 : }
151 0 : return false;
152 : }
153 :
154 : public function hasMarker(Marker $marker)
155 : {
156 0 : return in_array($marker, $this->markers, true);
157 : }
158 :
159 : public function addMarker(Marker $marker)
160 : {
161 0 : $this->markers[] = $marker;
162 0 : }
163 :
164 : public function removeMarker(Marker $marker)
165 : {
166 0 : if (!$this->hasMarker($marker)) {
167 0 : return null;
168 : }
169 :
170 0 : unset($this->markers[array_search($marker, $this->markers, true)]);
171 0 : return $marker;
172 : }
173 :
174 : public function setMarkers(array $markers)
175 : {
176 0 : $this->markers = $markers;
177 0 : }
178 :
179 : public function getMarkers()
180 : {
181 0 : return $this->markers;
182 : }
183 :
184 : public static function create()
185 : {
186 0 : $map = new static();
187 :
188 0 : return $map;
189 : }
190 : }
191 :
|