├── Core ├── Autoloader.php ├── CustomControl.php ├── CustomControlDecorator.php ├── LatLng.php ├── MapObject.php ├── MapObjectDecorator.php ├── PositionAbstract.php └── StaticMapObject.php ├── Event ├── DomEventListener.php ├── EventListener.php └── EventListenerDecorator.php ├── Layer ├── FusionTable.php ├── FusionTableDecorator.php ├── KmlLayer.php ├── KmlLayerDecorator.php ├── PanoramioLayer.php └── PanoramioLayerDecorator.php ├── Map.php ├── Overlay ├── Circle.php ├── GroundOverlay.php ├── GroundOverlayDecorator.php ├── MapStyle.php ├── Marker.php ├── MarkerDecorator.php ├── MarkerGroup.php ├── MarkerGroupDecorator.php ├── MarkerIcon.php ├── MarkerShape.php ├── Poly.php ├── PolyDecorator.php ├── Polygon.php ├── Polyline.php ├── Rectangle.php ├── Shape.php └── ShapeDecorator.php ├── README.md ├── Service ├── BicyclingDirections.php ├── CachedGeocodeResult.php ├── CachingGeocoder.php ├── Directions.php ├── DirectionsDecorator.php ├── DrivingDirections.php ├── GeocodeCache.php ├── GeocodeCachePDO.php ├── GeocodeError.php ├── GeocodeException.php ├── GeocodeResult.php ├── Geocoder.php └── WalkingDirections.php ├── Utility └── Scraper.php └── composer.json /Core/Autoloader.php: -------------------------------------------------------------------------------- 1 | register(); 13 | * 14 | * @author Jonathan H. Wage 15 | * @author Roman S. Borschel 16 | * @author Matthew Weier O'Phinney 17 | * @author Kris Wallsmith 18 | * @author Fabien Potencier 19 | */ 20 | class SplClassLoader 21 | { 22 | private $_fileExtension = '.php'; 23 | private $_namespace; 24 | private $_includePath; 25 | private $_namespaceSeparator = '\\'; 26 | 27 | /** 28 | * Creates a new SplClassLoader that loads classes of the 29 | * specified namespace. 30 | * 31 | * @param string $ns The namespace to use. 32 | */ 33 | public function __construct($ns = null, $includePath = null) 34 | { 35 | $this->_namespace = $ns; 36 | $this->_includePath = $includePath; 37 | } 38 | 39 | /** 40 | * Sets the namespace separator used by classes in the namespace of this class loader. 41 | * 42 | * @param string $sep The separator to use. 43 | */ 44 | public function setNamespaceSeparator($sep) 45 | { 46 | $this->_namespaceSeparator = $sep; 47 | } 48 | 49 | /** 50 | * Gets the namespace seperator used by classes in the namespace of this class loader. 51 | * 52 | * @return void 53 | */ 54 | public function getNamespaceSeparator() 55 | { 56 | return $this->_namespaceSeparator; 57 | } 58 | 59 | /** 60 | * Sets the base include path for all class files in the namespace of this class loader. 61 | * 62 | * @param string $includePath 63 | */ 64 | public function setIncludePath($includePath) 65 | { 66 | $this->_includePath = $includePath; 67 | } 68 | 69 | /** 70 | * Gets the base include path for all class files in the namespace of this class loader. 71 | * 72 | * @return string $includePath 73 | */ 74 | public function getIncludePath() 75 | { 76 | return $this->_includePath; 77 | } 78 | 79 | /** 80 | * Sets the file extension of class files in the namespace of this class loader. 81 | * 82 | * @param string $fileExtension 83 | */ 84 | public function setFileExtension($fileExtension) 85 | { 86 | $this->_fileExtension = $fileExtension; 87 | } 88 | 89 | /** 90 | * Gets the file extension of class files in the namespace of this class loader. 91 | * 92 | * @return string $fileExtension 93 | */ 94 | public function getFileExtension() 95 | { 96 | return $this->_fileExtension; 97 | } 98 | 99 | /** 100 | * Installs this class loader on the SPL autoload stack. 101 | */ 102 | public function register() 103 | { 104 | spl_autoload_register(array($this, 'loadClass')); 105 | } 106 | 107 | /** 108 | * Uninstalls this class loader from the SPL autoloader stack. 109 | */ 110 | public function unregister() 111 | { 112 | spl_autoload_unregister(array($this, 'loadClass')); 113 | } 114 | 115 | /** 116 | * Loads the given class or interface. 117 | * 118 | * @param string $className The name of the class to load. 119 | * @return void 120 | */ 121 | public function loadClass($className) 122 | { 123 | if (null === $this->_namespace || $this->_namespace.$this->_namespaceSeparator === substr($className, 0, strlen($this->_namespace.$this->_namespaceSeparator))) { 124 | $fileName = ''; 125 | $namespace = ''; 126 | if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) { 127 | $namespace = substr($className, 0, $lastNsPos); 128 | $className = substr($className, $lastNsPos + 1); 129 | $fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; 130 | } 131 | $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension; 132 | 133 | require ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName; 134 | } 135 | } 136 | } -------------------------------------------------------------------------------- /Core/CustomControl.php: -------------------------------------------------------------------------------- 1 | options['outer'] = $outer_options; 40 | $this->options['inner'] = $inner_options; 41 | if ( $position ) { 42 | $this->position = $position; 43 | } 44 | } 45 | 46 | /** 47 | * Add Listener 48 | * 49 | * Add a listener to the control 50 | * 51 | * @param string $event Event to listen for (click) 52 | * @param string $function Function to call 53 | * @return void 54 | */ 55 | public function addListener( $event, $function ) { 56 | $this->listeners[] = array( 57 | 'event' => $event, 58 | 'function' => $function 59 | ); 60 | } 61 | 62 | } -------------------------------------------------------------------------------- /Core/CustomControlDecorator.php: -------------------------------------------------------------------------------- 1 | $id, '_map' => $map ) ); 36 | } 37 | 38 | /** 39 | * Returns the javascript variable of the custom control 40 | * 41 | * @return string 42 | */ 43 | public function getJsVar() { 44 | return sprintf( '%s.custom_controls[%s]', $this->_map, $this->_id ); 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /Core/LatLng.php: -------------------------------------------------------------------------------- 1 | lat = $lat; 42 | $this->lng = $lng; 43 | $this->location = $location; 44 | } 45 | 46 | public function getLatLng() { 47 | return $this; 48 | } 49 | public function getLat() { 50 | return $this->lat; 51 | } 52 | public function getLng() { 53 | return $this->lng; 54 | } 55 | 56 | /** 57 | * Returns a string in the format lat,lng 58 | * 59 | * @return string 60 | */ 61 | public function __toString() { 62 | return sprintf( '%s,%s', $this->lat, $this->lng ); 63 | } 64 | } -------------------------------------------------------------------------------- /Core/MapObject.php: -------------------------------------------------------------------------------- 1 | options[$var] = $val; 30 | } 31 | } 32 | 33 | /** 34 | * Also used for setting options, some people prefer object::setVar() to object->var = 35 | * 36 | * @param string $method 37 | * @param mixed $val 38 | * @return void 39 | */ 40 | public function __call( $method, $val ) { 41 | if ( substr( $method, 0, 3 ) == 'set' ) { 42 | $this->options[strtolower( substr( $method, 3 ) )] = $val[0]; 43 | } 44 | } 45 | 46 | /** 47 | * Return an object variable 48 | * Will check for a property first 49 | * Then an option 50 | * If neither is found, null is returned 51 | * 52 | * @param string $var 53 | * @return mixed 54 | */ 55 | public function __get( $var ) { 56 | if ( property_exists( $this, $var ) ) { 57 | return $this->$var; 58 | } 59 | elseif ( isset( $this->options[$var] ) ) { 60 | return $this->options[$var]; 61 | } 62 | else { 63 | return null; 64 | } 65 | } 66 | 67 | /** 68 | * Return an option 69 | * 70 | * @param string $option Option to return 71 | * @return mixed 72 | */ 73 | public function getOption( $option ) { 74 | return isset( $this->options[$option] ) ? $this->options[$option] : false; 75 | } 76 | 77 | /** 78 | * Return the options 79 | * 80 | * @return array 81 | */ 82 | public function getOptions() { 83 | return $this->options; 84 | } 85 | 86 | /** 87 | * Remove an option 88 | * 89 | * @param string $option Option to remove 90 | * @return void 91 | */ 92 | public function removeOption( $option ) { 93 | unset( $this->options[$option] ); 94 | } 95 | 96 | /** 97 | * Magic isset method 98 | * If a protected property with the passed variable name exists it returns isset() of that property 99 | * Otherwise it will return isset() of the option 100 | * 101 | * @return boolean 102 | */ 103 | public function __isset( $var ) { 104 | if ( property_exists( $this, $var ) ) { 105 | return isset( $this->$var ); 106 | } 107 | else { 108 | return isset( $this->options[$var] ); 109 | } 110 | } 111 | 112 | } -------------------------------------------------------------------------------- /Core/MapObjectDecorator.php: -------------------------------------------------------------------------------- 1 | decoratee = $decoratee; 31 | foreach( $properties as $var => $val ) { 32 | $this->$var = $val; 33 | } 34 | } 35 | 36 | /** 37 | * Sends all function calls to the decorated object 38 | * 39 | * @param string $name Name of the variable 40 | * @param array $arguments Array of arguments 41 | * @return mixed 42 | */ 43 | public function __call( $name, $arguments ) { 44 | return $this->decoratee->$name( implode( ',', $arguments ) ); 45 | } 46 | 47 | /** 48 | * Sends all variable requests to the marker 49 | * 50 | * @param string $var Name of the variable 51 | * @return mixed 52 | */ 53 | public function __get( $var ) { 54 | if ( property_exists( $this, $var ) ) { 55 | return $this->$var; 56 | } 57 | return $this->decoratee->$var; 58 | } 59 | 60 | /** 61 | * Sends all variable assignments to the decorated object 62 | * 63 | * @param string $var Name of the variable 64 | * @param mixed $val Value of the variable 65 | * @return void 66 | */ 67 | public function __set( $var, $val ) { 68 | if ( isset( $this->$var ) || property_exists( $this, $var ) ) { 69 | $this->$var = $val; 70 | } 71 | else { 72 | $this->decoratee->$var = $val; 73 | } 74 | } 75 | 76 | /** 77 | * Magic isset function 78 | * 79 | * @param string $var Variable to test 80 | * @return boolean 81 | */ 82 | public function __isset( $var ) { 83 | if ( property_exists( $this, $var ) ) { 84 | return isset( $this->$var ); 85 | } 86 | else { 87 | return isset( $this->decoratee->$var ); 88 | } 89 | } 90 | 91 | 92 | public function __toString() { 93 | return $this->getJsVar(); 94 | } 95 | } -------------------------------------------------------------------------------- /Core/PositionAbstract.php: -------------------------------------------------------------------------------- 1 | getLat() ) ) * cos( deg2rad( $position->getLat() ) ) * cos( deg2rad( $this->getLng() ) - deg2rad( $position->getLng() ) ) + sin( deg2rad($this->getLat() ) ) * sin( deg2rad( $position->getLat() ) ) ); 52 | switch( strtolower( $units ) ) { 53 | case 'k': // kilometers 54 | return $miles * 1.609344; 55 | break; 56 | case 'n': // nautical mile 57 | return $miles * 0.868976242; 58 | break; 59 | case 'f': // feet 60 | return $miles * 5280; 61 | break; 62 | case 'i': // inches 63 | return $miles * 63360; 64 | break; 65 | case 'm': 66 | default: 67 | return $miles; 68 | break; 69 | } 70 | } 71 | 72 | } -------------------------------------------------------------------------------- /Core/StaticMapObject.php: -------------------------------------------------------------------------------- 1 | static ) ) { 24 | $this->static = new \StdClass; 25 | } 26 | $this->static->$var = $val; 27 | } 28 | 29 | /** 30 | * Get static variable 31 | * 32 | * @param string $var variable to get 33 | * @return mixed 34 | */ 35 | public function getStaticVar( $var ) { 36 | return isset( $this->static, $this->static->$var ) ? $this->static->$var : null; 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /Event/DomEventListener.php: -------------------------------------------------------------------------------- 1 | addObject( $event ); 15 | */ 16 | 17 | class DomEventListener extends \PHPGoogleMaps\Event\EventListener {} -------------------------------------------------------------------------------- /Event/EventListener.php: -------------------------------------------------------------------------------- 1 | addObject( $event ); 15 | */ 16 | 17 | class EventListener extends \PHPGoogleMaps\Core\MapObject { 18 | 19 | /** 20 | * The object to listen to 21 | * This should be the ID of the DOM element to listen to 22 | * 23 | * @var string 24 | */ 25 | protected $object; 26 | 27 | /** 28 | * The event to listen for 29 | * 30 | * @var string 31 | */ 32 | protected $event; 33 | 34 | /** 35 | * The function to call 36 | * Can be a name of a function that you've defined or a complete function. 37 | * e.g. function(){ alert('Click!') } 38 | * 39 | * @var string 40 | */ 41 | protected $function; 42 | 43 | /** 44 | * Once flag 45 | * If true the event listener will be removed after the first firing. 46 | * 47 | * @var boolean 48 | */ 49 | protected $once; 50 | 51 | /** 52 | * Constructor 53 | * 54 | * @param MapObjectDecorator|String $object Object to listen to. This should be the ID of the DOM element to listen to. 55 | * @param string $event Event to listen for. 56 | * @param string $function Function to call. 57 | * @param boolean $once Once flag. If true the event listener will be removed after first call. 58 | * @return DOMEventListener 59 | */ 60 | public function __construct( $object, $event, $function, $once=false ) { 61 | if ( $object instanceof \PHPGoogleMaps\Core\MapObject ) { 62 | trigger_error( 'You must pass a MapObjectDecorator to an EventListener. e.g. $decorator = $map->addObject( $object ); $event = new EventListener( $decorator… )', E_USER_ERROR ); 63 | } 64 | $this->event = $event; 65 | $this->function = $function; 66 | $this->object = $object; 67 | $this->once = $once; 68 | } 69 | 70 | } -------------------------------------------------------------------------------- /Event/EventListenerDecorator.php: -------------------------------------------------------------------------------- 1 | $id, '_map' => $map ) ); 39 | } 40 | 41 | /** 42 | * Get javascript variable 43 | * 44 | * @return string 45 | */ 46 | public function getJsVar() { 47 | return sprintf( '%s.event_listeners[%s]', $this->_map, $this->_id ); 48 | } 49 | 50 | } -------------------------------------------------------------------------------- /Layer/FusionTable.php: -------------------------------------------------------------------------------- 1 | table_id = $table_id; 32 | if ( $options ) { 33 | unset( $options['map'] ); 34 | $this->options = $options; 35 | } 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /Layer/FusionTableDecorator.php: -------------------------------------------------------------------------------- 1 | $id, '_map' => $map ) ); 37 | } 38 | 39 | /** 40 | * Returns the javascript variable of the Fusion Table 41 | * 42 | * @return string 43 | */ 44 | public function getJsVar() { 45 | return sprintf( '%s.fusion_tables[%s]', $this->_map, $this->_id ); 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /Layer/KmlLayer.php: -------------------------------------------------------------------------------- 1 | options = $options; 32 | } 33 | $this->url = $url; 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /Layer/KmlLayerDecorator.php: -------------------------------------------------------------------------------- 1 | $id, '_map' => $map ) ); 36 | } 37 | 38 | /** 39 | * Returns the javascript variable of the KML layer 40 | * 41 | * @return string 42 | */ 43 | public function getJsVar() { 44 | return sprintf( '%s.kml_layers[%s]', $this->_map, $this->_id ); 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /Layer/PanoramioLayer.php: -------------------------------------------------------------------------------- 1 | options = $options; 23 | } 24 | } 25 | 26 | /** 27 | * Set tag 28 | * 29 | * @param string $tag Tag 30 | * @return void 31 | */ 32 | public function setTag( $tag ) { 33 | $this->options['tag'] = $tag; 34 | } 35 | 36 | /** 37 | * Set User ID 38 | * 39 | * @param integer $user_id User ID 40 | * @return void 41 | */ 42 | public function setUserID( $user_id ) { 43 | $this->options['user_id'] = (int)$user_id; 44 | } 45 | 46 | } -------------------------------------------------------------------------------- /Layer/PanoramioLayerDecorator.php: -------------------------------------------------------------------------------- 1 | $id, '_map' => $map ) ); 38 | } 39 | 40 | /** 41 | * Returns the javascript variable of the Panoramio layer 42 | * 43 | * @return string 44 | */ 45 | public function getJsVar() { 46 | return sprintf( '%s.panoramio_layers[%s]', $this->_map, $this->_id ); 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /Map.php: -------------------------------------------------------------------------------- 1 | 7 | * @package PHPGoogleMaps 8 | */ 9 | 10 | 11 | namespace PHPGoogleMaps; 12 | 13 | /** 14 | * Google Map 15 | * 16 | * The main class that controls the output of a map 17 | * All map objects must be added to an instance of a GoogleMap via addObject() or addObjects() 18 | * 19 | * Using addObject() decorates the object to provide extra functionality 20 | * $marker = \PHPGoogleMaps\Overlay\Marker::createFromLocation( 'New York, NY' ); 21 | * $map->addObject( $marker ); 22 | * echo $marker->getJsVar(); // Echos map.markers[0] 23 | * 24 | * If adding multiple objects with addObjects() you can do the same by passing the object by 25 | * reference, but this is deprecated as of PHP 5.3 26 | * $marker1 = \PHPGoogleMaps\Overlay\Marker::createFromLocation( 'New York, NY' ); 27 | * $marker2 = \PHPGoogleMaps\Overlay\Marker::createFromLocation( 'San Diego, CA' ); 28 | * $map->addObjects( array( &$marker1, $marker2 ) ); 29 | * echo $marker1->getJsVar(); // Echos map.markers[0] 30 | * 31 | */ 32 | 33 | class Map { 34 | 35 | /** 36 | * Map ID 37 | * 38 | * ID of the map. This will be used for CSS and javascript. 39 | * 40 | * @var string 41 | */ 42 | private $map_id = 'map'; 43 | 44 | /** 45 | * API Key 46 | * 47 | * {@link https://code.google.com/apis/console} 48 | * 49 | * @var string 50 | */ 51 | private $api_key = null; 52 | 53 | /** 54 | * Use HTTPS protocol 55 | * 56 | * @var boolean 57 | */ 58 | private $use_https = false; 59 | 60 | /** 61 | * Map language 62 | * 63 | * Language of the map. 64 | * @link http://code.google.com/apis/maps/documentation/javascript/basics.html#Localization 65 | * 66 | * @var string 67 | */ 68 | private $language; 69 | 70 | /** 71 | * Map region 72 | * 73 | * Region of the map. 74 | * @link http://code.google.com/apis/maps/documentation/javascript/basics.html#Localization 75 | * 76 | * @var string 77 | */ 78 | private $region; 79 | 80 | /** 81 | * Sensor 82 | * 83 | * Device's GPS abilities 84 | * @link http://code.google.com/apis/maps/documentation/javascript/basics.html#SpecifyingSensor 85 | * 86 | * @var string 87 | */ 88 | private $sensor = false; 89 | 90 | /** 91 | * Version of the API to use 92 | * Leave this at 3 to use the latest version 93 | * @link http://code.google.com/apis/maps/documentation/javascript/basics.html#Versioning 94 | * 95 | * @var int 96 | */ 97 | private $api_version = 3; 98 | 99 | 100 | /** 101 | * Map type 102 | * This controls what type of map to display (roadmap, satellite, terrain, hybrid) 103 | * @link http://code.google.com/apis/maps/documentation/javascript/reference.html#MapTypeId 104 | * 105 | *@var string 106 | */ 107 | private $map_type = 'roadmap'; 108 | 109 | /** 110 | * Zoom level 111 | * 112 | * @var int 113 | */ 114 | private $zoom = 7; 115 | 116 | /** 117 | * Auto encompass flag 118 | * If enabled the map will automatically encompass all markers on the map 119 | * If disabled a zoom level and center must be set 120 | * 121 | * @var boolean 122 | */ 123 | private $auto_encompass = true; 124 | 125 | /** 126 | * Units 127 | * If unset map will use default units of the users location 128 | * 129 | * @var string 130 | */ 131 | private $units; 132 | 133 | /** 134 | * Map height 135 | * Example: 500px, 100% 136 | * 137 | * @var string 138 | */ 139 | private $height = '500px'; 140 | 141 | /** 142 | * Map width 143 | * Example: 500px, 100% 144 | * 145 | * @var string 146 | */ 147 | private $width = '500px'; 148 | 149 | /** 150 | * Map center 151 | * 152 | * @var LatLng 153 | */ 154 | private $center; 155 | 156 | /** 157 | * Center on user flag 158 | * If set the map will attempt to set the center on the user's location 159 | * 160 | * @var boolean 161 | */ 162 | private $center_on_user = false; 163 | 164 | /** 165 | * Navigation control flag 166 | * Show/hide the navigation control 167 | * 168 | * @var boolean 169 | */ 170 | private $navigation_control = true; 171 | 172 | /** 173 | * Navigation control position 174 | * 175 | * @var string 176 | */ 177 | private $navigation_control_position; 178 | 179 | /** 180 | * Navigation control style 181 | * 182 | * @var string 183 | */ 184 | private $navigation_control_style; 185 | 186 | /** 187 | * Map type control flag 188 | * Show/hide the map type control 189 | * 190 | * @var boolean 191 | */ 192 | private $map_type_control = true; 193 | 194 | /** 195 | * Map type control position 196 | * 197 | * @var string 198 | */ 199 | private $map_type_control_position; 200 | 201 | /** 202 | * Map type control style 203 | * 204 | * @var string 205 | */ 206 | private $map_type_control_style; 207 | 208 | /** 209 | * Available map types 210 | * Array of map types that will be available for the user to choose 211 | * 212 | * @var array 213 | */ 214 | private $map_types = array(); 215 | 216 | /** 217 | * Map styles 218 | * Custom map styles 219 | * 220 | * @var array 221 | */ 222 | private $map_styles = array(); 223 | 224 | /** 225 | * Scale control flag 226 | * 227 | * @var boolean 228 | */ 229 | private $scale_control = false; 230 | 231 | /** 232 | * Scale control position 233 | * 234 | * @var string 235 | */ 236 | private $scale_control_position; 237 | 238 | /** 239 | * Map custom controls 240 | * Array of custom controls added to the map 241 | * 242 | * @var array 243 | */ 244 | private $custom_controls = array(); 245 | 246 | /** 247 | * Map shapes 248 | * Array of shapes added to the map 249 | * 250 | * @var array 251 | */ 252 | private $shapes = array(); 253 | 254 | /** 255 | * Map polys 256 | * Array of polygons and polylines added to the map 257 | * 258 | * @var array 259 | */ 260 | private $polys = array(); 261 | 262 | 263 | /** 264 | * Scrollable flag 265 | * 266 | * Allows the map to be zoomed with the scrollbar 267 | * 268 | * @var boolean 269 | */ 270 | private $scrollable = true; 271 | 272 | /** 273 | * Draggable flag 274 | * 275 | * Allows the map to be dragged 276 | * 277 | * @var boolean 278 | */ 279 | private $draggable = true; 280 | 281 | /** 282 | * Default marker icon 283 | * Default marker icon of the map 284 | * 285 | * @var MarkerIcon 286 | */ 287 | private $default_marker_icon; 288 | 289 | /** 290 | * Default marker shadow 291 | * Default marker shadow of the map 292 | * 293 | * @var MarkerIcon 294 | */ 295 | private $default_marker_shadow = null; 296 | 297 | /** 298 | * Stagger markers 299 | * Time in milliseconds to stagger the marker additions 300 | * 301 | * @var integer 302 | */ 303 | private $stagger_markers = 0; 304 | 305 | /** 306 | * Map markers 307 | * 308 | * @var array 309 | */ 310 | private $markers = array(); 311 | 312 | /** 313 | * Hash of the marker data 314 | * This is used to keep from extracting the same marker data 315 | * 316 | * @var string 317 | */ 318 | private $marker_data_hash; 319 | 320 | /** 321 | * Marker icons 322 | * All the maps marker icons 323 | * 324 | * @var array 325 | */ 326 | private $marker_icons = array(); 327 | 328 | /** 329 | * Marker shapes 330 | * All the maps marker shapes 331 | * 332 | * @var array 333 | */ 334 | private $marker_shapes = array(); 335 | 336 | /** 337 | * Marker Groups 338 | * All the maps marker groups 339 | * 340 | * @var array 341 | */ 342 | private $marker_groups = array(); 343 | 344 | /** 345 | * Fusion tables 346 | * Array of fusion tables added to the map 347 | * 348 | * @var array 349 | */ 350 | private $fusion_tables = array(); 351 | 352 | /** 353 | * KML layers 354 | * Array of KML layers added to the map 355 | * 356 | * @var array 357 | */ 358 | private $kml_layers = array(); 359 | 360 | /** 361 | * Panoramio layers 362 | * Array of Panoramio layers added to the map 363 | * 364 | * @var array 365 | */ 366 | private $panoramio_layers = array(); 367 | 368 | /** 369 | * Ground overlays 370 | * Array of ground overlays added to the map 371 | * 372 | * @var array 373 | */ 374 | private $ground_overlays = array(); 375 | 376 | /** 377 | * Traffic layer flag 378 | * 379 | * @var boolean 380 | */ 381 | private $traffic_layer = false; 382 | 383 | /** 384 | * Streetview 385 | * 386 | * @var boolean 387 | */ 388 | private $streetview; 389 | 390 | /** 391 | * Bicycle layer flag 392 | * 393 | * @var boolean 394 | */ 395 | private $bicycle_layer = false; 396 | 397 | /** 398 | * Event listeners 399 | * Holds the array of event listeners 400 | * 401 | * @var array 402 | */ 403 | private $event_listeners = array(); 404 | 405 | /** 406 | * Infowindow flag 407 | * 408 | * @var boolean 409 | */ 410 | private $info_windows = true; 411 | 412 | /** 413 | * Compressed output flag 414 | * Removes all unecessary white space from the javascript code 415 | * 416 | * @var boolean 417 | */ 418 | private $compress_output = false; 419 | 420 | /** 421 | * Geolocation flag 422 | * 423 | * @var boolean 424 | */ 425 | private $geolocation = false; 426 | 427 | /** 428 | * Geolocation timeout 429 | * This is the amount of time in milliseconds that the browser will attempt geolocation 430 | * @link http://dev.w3.org/geo/api/spec-source.html#position_options_interface 431 | * 432 | * @var int 433 | */ 434 | private $geolocation_timeout = 6000; 435 | 436 | /** 437 | * Geolocation high accuracy 438 | * Attempt high accuracy geolocation 439 | * @link http://dev.w3.org/geo/api/spec-source.html#position_options_interface 440 | * 441 | * @var boolean 442 | */ 443 | private $geolocation_high_accuracy = false; 444 | 445 | /** 446 | * Geolocation fail callback 447 | * Function to call if geolocation fails 448 | * 449 | * @var string 450 | */ 451 | private $geolocation_fail_callback; 452 | 453 | /** 454 | * Geolocation success callback 455 | * Function to call if geolocation succeeds 456 | * 457 | * @var string 458 | */ 459 | private $geolocation_success_callback; 460 | 461 | /** 462 | * Backup geolocation location 463 | * If the map was centered via geolocation and geolocation fails 464 | * this will set as the map center 465 | * 466 | * @var LatLng 467 | */ 468 | private $geolocation_backup; 469 | 470 | /** 471 | * Mobile flag 472 | * This will output a special meta tag for mobile devices 473 | * @link http://code.google.com/apis/maps/documentation/javascript/basics.html#Mobile 474 | * 475 | * @var boolean 476 | */ 477 | private $mobile = false; 478 | 479 | /** 480 | * Mobile iphone fullscreen flag 481 | * This will hide the navigation bar on the iphone 482 | * 483 | * @var boolean 484 | */ 485 | private $mobile_iphone_fullscreen = false; 486 | 487 | /** 488 | * Map Directions 489 | * 490 | * @var Directions 491 | */ 492 | private $directions; 493 | 494 | /** 495 | * Array of map binds 496 | * 497 | * @var array 498 | */ 499 | private $binds = array(); 500 | 501 | /** 502 | * Array of google map libraries to load 503 | * 504 | * @var array 505 | */ 506 | private $libraries = array(); 507 | 508 | /** 509 | * Adsense flag 510 | * 511 | * @var boolean 512 | */ 513 | private $adsense = false; 514 | 515 | /** 516 | * Adsense publisher id 517 | * 518 | * @var string 519 | */ 520 | private $adsense_publisher_id = null; 521 | 522 | /** 523 | * Adsense format 524 | * 525 | * @var string 526 | */ 527 | private $adsense_format = 'banner'; 528 | 529 | /** 530 | * Adsense position 531 | * 532 | * @var 533 | */ 534 | private $adsense_position = 'bottom'; 535 | 536 | /** 537 | * Adsense visibility 538 | * 539 | * @var boolean 540 | */ 541 | private $adsense_visible = true; 542 | 543 | /** 544 | * Clustering javascript 545 | * 546 | * This will hold the location of the clustering file 547 | * 548 | * @var string 549 | */ 550 | private $clustering_js; 551 | 552 | /** 553 | * Clustering options 554 | * 555 | * @var array 556 | */ 557 | private $clustering_options = array(); 558 | 559 | /** 560 | * Places library flag 561 | * 562 | * @var bool 563 | */ 564 | private $places = false; 565 | 566 | /** 567 | * Autocomplete input id 568 | * 569 | * This is the id of the text input that will be used to search places 570 | * 571 | * @var string 572 | */ 573 | private $autocomplete_input_id = null; 574 | 575 | /** 576 | * Autocomplete options 577 | * 578 | * Array of options to be passed to the autocomplete constructor 579 | * 580 | * @var array 581 | */ 582 | private $autocomplete_options = array(); 583 | 584 | /** 585 | * Loading content 586 | * 587 | * @var string 588 | */ 589 | private $loading_content; 590 | 591 | /** 592 | * Constructor 593 | * 594 | * @var string $map_id ID to give the map 595 | * @return GoogleMap 596 | */ 597 | public function __construct( array $options=null ) { 598 | if ( $options ) { 599 | foreach( $options as $option_var => $option_val ) { 600 | switch ( $option_var ) { 601 | case 'use_https': 602 | $this->useHttps(); 603 | break; 604 | case 'api_key': 605 | $this->setApiKey( $option_val ); 606 | break; 607 | case 'zoom': 608 | $this->setZoom( $option_val ); 609 | break; 610 | case 'map_id': 611 | $this->map_id = $this->normalizeVariable( $option_val ); 612 | break; 613 | case 'center': 614 | $this->setCenter( $option_val ); 615 | break; 616 | case 'language': 617 | $this->setLanguage( $option_val ); 618 | break; 619 | case 'region': 620 | $this->setRegion($option_val); 621 | break; 622 | case 'sensor': 623 | $option_val ? $this->enableSensor() : $this->disableSensor(); 624 | break; 625 | case 'api_version': 626 | $this->setApiVersion($option_val); 627 | break; 628 | case 'auto_encompass': 629 | $option_val ? $this->enableAutoEncompass() : $this->disableAutoEncompass(); 630 | break; 631 | case 'units': 632 | $this->setUnits($option_val); 633 | break; 634 | case 'height': 635 | $this->setHeight($option_val); 636 | break; 637 | case 'width': 638 | $this->setWidth($option_val); 639 | break; 640 | case 'center_on_user': 641 | $this->setCenterByUserLocation( isset( $options['center_on_user_backup'] ) ? $options['center_on_user_backup'] : null ); 642 | break; 643 | case 'navigation_control': 644 | $option_val ? $this->enableNavigationControl() : $this->disableNavigationControl(); 645 | break; 646 | case 'navigation_control_position': 647 | $this->setNavigationControlPosition($option_val); 648 | break; 649 | case 'navigation_control_style': 650 | $this->setNavigationControlStyle($option_val); 651 | break; 652 | case 'map_type_control': 653 | $option_val ? $this->enableMapTypeControl() : $this->disableMapTypeControl(); 654 | break; 655 | case 'map_type_control_position': 656 | $this->setMapTypeControlPosition($option_val); 657 | break; 658 | case 'map_type_control_style': 659 | $this->setMapTypeControlStyle($option_val); 660 | break; 661 | case 'scale_control': 662 | $option_val ? $this->enableScaleControl() : $this->disableScaleControl(); 663 | break; 664 | case 'scale_control_position': 665 | $this->setScaleControlPosition($option_val); 666 | break; 667 | case 'scrollable': 668 | $option_val ? $this->enableScrolling() : $this->disableScrolling(); 669 | break; 670 | case 'draggable': 671 | $option_val ? $this->enableDragging() : $this->disableDragging(); 672 | break; 673 | case 'bicycle_layer': 674 | $option_val ? $this->enableBicycleLayer() : $this->disableBicycleLayer(); 675 | break; 676 | case 'traffic_layer': 677 | $option_val ? $this->enableTrafficLayer() : $this->disableTrafficLayer(); 678 | break; 679 | case 'geolocation': 680 | $timeout = isset( $options['geolocation_timeout'] ) ? (int) $options['geolocation_timeout'] : null; 681 | $high_accuracy = isset( $options['geolocation_high_accuracy'] ) ? (bool) $options['geolocation_high_accuracy'] : null; 682 | $this->enableGeolocation( $timeout, $high_accuracy ); 683 | break; 684 | case 'info_windows': 685 | $option_val ? $this->enableInfoWindows() : $this->disableInfoWindows(); 686 | break; 687 | case 'compress_output': 688 | $option_val ? $this->enableCompressedOutput() : $this->disableCompressedOutput(); 689 | break; 690 | case 'places_autocomplete': 691 | $this->enablePlacesAutocomplete( $option_val ); 692 | break; 693 | 694 | } 695 | } 696 | } 697 | } 698 | 699 | /** 700 | * Set API Key 701 | * 702 | * {@link https://code.google.com/apis/console} 703 | * 704 | * @param string $api_key Your API Key 705 | */ 706 | function setApiKey( $api_key ) { 707 | $this->api_key = $api_key; 708 | } 709 | 710 | /** 711 | * Uee HTTPS protocol 712 | */ 713 | function useHttps() { 714 | $this->use_https = true; 715 | } 716 | 717 | /** 718 | * Set loading content 719 | * 720 | * This will set the content of the map container before it's loaded. 721 | * When the map loads this content will be removed. 722 | * 723 | * 724 | * @param string $content Content to display while the map is loading 725 | */ 726 | function setLoadingContent( $content ) { 727 | $this->loading_content = $content; 728 | } 729 | 730 | /************************* 731 | * 732 | * Static map 733 | * 734 | ************************/ 735 | 736 | /** 737 | * Get a static map url 738 | * 739 | * This will turn your map into a static map url 740 | * 741 | * 742 | * @param string $format Image format 743 | * @param array $visible Array of visible locations 744 | * @return string 745 | */ 746 | 747 | function getStaticMap( $format='png', array $visible = null ) { 748 | 749 | $url = "http://maps.google.com/maps/api/staticmap?"; 750 | $request = ''; 751 | $request .= sprintf( "size=%sx%s&", intval( $this->width ), intval( $this->height ) ); 752 | $request .= sprintf( "sensor=%s&", $this->sensor ? 'true' : 'false' ); 753 | $request .= sprintf( "format=%s&", $format ); 754 | $request .= sprintf( "maptype=%s&", $this->map_type ); 755 | $request .= sprintf( "language=%s&", $this->language ); 756 | if ( is_array( $visible ) ) { 757 | $request .= sprintf( "visible=%s&", implode( '|', $visible ) ); 758 | } 759 | if ( $this->center instanceof \PHPGoogleMaps\Core\PositionAbstract ) { 760 | $request .= sprintf( "center=%s,%s&", $this->center->getLat(), $this->center->getLng() ); 761 | $request .= sprintf( "zoom=%s&", $this->zoom ); 762 | } 763 | foreach( $this->markers as $marker ) { 764 | $request .= sprintf( "markers=size:%s|color:%s|label:%s|%sshadow:%s|%s,%s&", 765 | isset( $marker->static->size ) ? $marker->static->size : '', 766 | isset( $marker->static->color ) ? $marker->static->color : '', 767 | isset( $marker->static->label ) ? strtoupper( (string) $marker->static->label[0] ) : '', 768 | $marker->icon ? sprintf( 'icon:%s|', $marker->icon ) : '', 769 | isset( $marker->static->flat ) ? ( ( $marker->static->flat ) ? 'false' : 'true' ) : '', 770 | $marker->position->getLat(), $marker->position->getLng() 771 | ); 772 | } 773 | return sprintf( "%s%s", $url, $request ); 774 | 775 | } 776 | 777 | /** 778 | * Validate a static map 779 | * 780 | * Wil return true or an http error code 781 | * 782 | * @param string $format Image format 783 | * @param array $visible Array of visible locations 784 | * @return boolean|int 785 | */ 786 | function validateStaticMap( $format='png', array $visible = null ) { 787 | $headers = get_headers($this->getStaticMap( $format, $visible )); 788 | if ( $headers ) { 789 | if ( strpos( $headers[0], '200' ) ) { 790 | return true; 791 | } 792 | return array_slice( explode( ' ', $headers[0] ), 1, 1 ); 793 | } 794 | } 795 | 796 | /** 797 | * Print the satic map url 798 | * 799 | * @param string $format Image format 800 | * @param array $visible Array of visible locations 801 | * @return void 802 | */ 803 | function printStaticMap( $format='png', array $visible = null ) { 804 | echo $this->getStaticMap( $format, $visible ); 805 | } 806 | 807 | /************************* 808 | * 809 | * Directions 810 | * 811 | *************************/ 812 | 813 | /** 814 | * Add directions to the map 815 | * 816 | * @param Directions $dir Directions object 817 | * @return DirectionsDecorator 818 | */ 819 | protected function addDirections( \PHPGoogleMaps\Service\Directions $dir ) { 820 | return $this->directions = new \PHPGoogleMaps\Service\DirectionsDecorator( $dir, $this->map_id ); 821 | } 822 | 823 | /** 824 | * Get the map directions 825 | * 826 | * @return DirectionsDecorator 827 | */ 828 | public function getDirections() { 829 | return $this->directions; 830 | } 831 | 832 | 833 | /************************************************ 834 | * 835 | * Layers 836 | * 837 | *************************************************/ 838 | 839 | /** 840 | * Enable streetview 841 | * 842 | * @return void 843 | */ 844 | public function enableStreetView( array $options=null, $container=null ) { 845 | $default_options = array( 846 | 'visible' => isset( $options['position'] ) && $options['position'] == true ? true : false, 847 | 'enableCloseButton' => true, 848 | 'pov' => array( 849 | 'heading' => 90, 850 | 'zoom' => 1, 851 | 'pitch' => 0 852 | ) 853 | ); 854 | $this->streetview = new \StdClass; 855 | if ( $container ) { 856 | $this->streetview->container = $container; 857 | } 858 | else { 859 | $this->streetview->container = $this->map_id; 860 | } 861 | 862 | if ( isset( $options['position'] ) ) { 863 | if ( $options['position'] == 'geolocation' ) { 864 | $this->enableGeolocation(); 865 | $options['position'] = "geolocation"; 866 | } 867 | elseif ( $options['position'] instanceof \PHPGoogleMaps\Core\PositionAbstract ) { 868 | $options['position'] = $options['position']->getLatLng(); 869 | } 870 | else { 871 | $geocode_result = \PHPGoogleMaps\Service\Geocoder::geocode( $options['position'] ); 872 | if ( $geocode_result instanceof \PHPGoogleMaps\Core\PositionAbstract ) { 873 | $options['position'] = $geocode_result->getLatLng(); 874 | } 875 | else { 876 | throw new \PHPGoogleMaps\Service\GeocodeException( $geocode_result ); 877 | } 878 | } 879 | } 880 | $this->streetview->options = (array)$options + $default_options; 881 | } 882 | 883 | /** 884 | * Enable sensor 885 | * 886 | * @return void 887 | */ 888 | public function enableSensor() { 889 | $this->sensor = true; 890 | } 891 | 892 | /** 893 | * Disable sensor 894 | * 895 | * @return void 896 | */ 897 | public function disableSensor() { 898 | $this->sensor = false; 899 | } 900 | 901 | /** 902 | * Enable traffic layer 903 | * 904 | * @return void 905 | */ 906 | public function enableTrafficLayer() { 907 | $this->traffic_layer = true; 908 | } 909 | 910 | /** 911 | * disable traffic layer 912 | * 913 | * @return void 914 | */ 915 | public function disableTrafficLayer() { 916 | $this->traffic_layer = false; 917 | } 918 | 919 | /** 920 | * Enable bicycle layer 921 | * 922 | * @return void 923 | */ 924 | public function enableBicycleLayer() { 925 | $this->bicycle_layer = true; 926 | } 927 | 928 | /** 929 | * Disable bicycle layer 930 | * 931 | * @return void 932 | */ 933 | public function disableBicycleLayer() { 934 | $this->bicycle_layer = false; 935 | } 936 | 937 | /** 938 | * Enable info windows 939 | * 940 | * @return void 941 | */ 942 | public function enableInfoWindows() { 943 | $this->info_windows = true; 944 | } 945 | 946 | /** 947 | * Disable info windows 948 | * 949 | * @return void 950 | */ 951 | public function disableInfoWindows() { 952 | $this->info_windows = false; 953 | } 954 | 955 | /** 956 | * Return info window status 957 | * 958 | * @return boolean 959 | */ 960 | public function infoWindows() { 961 | return $this->info_windows; 962 | } 963 | 964 | /** 965 | * Add a fusion table to the map 966 | * @link http://code.google.com/apis/maps/documentation/javascript/reference.html#FusionTablesLayer 967 | * @link http://tables.googlelabs.com/ 968 | * 969 | * @param FusionTable $ft Fusion table to add to the map 970 | * @return FusionTableDecorator 971 | * @access protected 972 | */ 973 | protected function addFusionTable( \PHPGoogleMaps\Layer\FusionTable $ft ) { 974 | return $this->fusion_tables[] = new \PHPGoogleMaps\Layer\FusionTableDecorator( $ft, count( $this->fusion_tables ), $this->map_id ); 975 | } 976 | 977 | /** 978 | * Get the array of the map's fusion tables 979 | * 980 | * @return array 981 | */ 982 | public function getFusionTables() { 983 | return $this->fusion_tables; 984 | } 985 | 986 | /** 987 | * Add a KML layer to the map 988 | * @link http://code.google.com/apis/maps/documentation/javascript/reference.html#KmlLayer 989 | * @link http://code.google.com/apis/maps/documentation/javascript/overlays.html#KMLLayers 990 | * 991 | * @param KmlLayer $kml KML layer to add to the map 992 | * @return KmlLayerDecorator 993 | * @access protected 994 | */ 995 | protected function addKmlLayer( \PHPGoogleMaps\Layer\KmlLayer $kml ) { 996 | return $this->kml_layers[] = new \PHPGoogleMaps\Layer\KmlLayerDecorator( $kml, count( $this->kml_layers ), $this->map_id ); 997 | } 998 | 999 | /** 1000 | * Add a Panoramio layer to the map 1001 | * @link http://code.google.com/apis/maps/documentation/javascript/overlays.html#PanoramioLibrary 1002 | * 1003 | * @param PanoramioLayer $panoramio Panoramio layer to add to the map 1004 | * @return PanoramioLayerDecorator 1005 | * @access protected 1006 | */ 1007 | protected function addPanoramioLayer( \PHPGoogleMaps\Layer\PanoramioLayer $panoramio ) { 1008 | return $this->panoramio_layers[] = new \PHPGoogleMaps\Layer\PanoramioLayerDecorator( $panoramio, count( $this->panoramio_layers ), $this->map_id ); 1009 | } 1010 | 1011 | /** 1012 | * Add a ground overlay to the map 1013 | * 1014 | * @param GroundOverlay $ground_overlay ground overlay to add to the map 1015 | * @return GroundOverlayDecorator 1016 | * @access protected 1017 | */ 1018 | protected function addGroundOverlay( \PHPGoogleMaps\Overlay\GroundOverlay $ground_overlay ) { 1019 | return $this->ground_overlays[] = new \PHPGoogleMaps\Overlay\GroundOverlayDecorator( $ground_overlay, count( $this->ground_overlays ), $this->map_id ); 1020 | } 1021 | 1022 | /** 1023 | * Get the array of the map's KML layers 1024 | * 1025 | * @return array 1026 | */ 1027 | public function getKmlLayers() { 1028 | return $this->kml_layers; 1029 | } 1030 | 1031 | /** 1032 | * Add a shape to the map 1033 | * 1034 | * @param Shape $shape Shape to add to the map 1035 | * @return ShapeDecorator 1036 | * @access protected 1037 | */ 1038 | protected function addShape( \PHPGoogleMaps\Overlay\Shape $shape ) { 1039 | return $this->shapes[] = new \PHPGoogleMaps\Overlay\ShapeDecorator( $shape, count( $this->shapes ), $this->map_id ); 1040 | } 1041 | 1042 | /** 1043 | * Get the array of the map's shapes 1044 | * 1045 | * @return array 1046 | */ 1047 | public function getShapes() { 1048 | return $this->shapes; 1049 | } 1050 | 1051 | /** 1052 | * Add a polygon to the map 1053 | * 1054 | * @param Shape $shape Shape to add to the map 1055 | * @return ShapeDecorator 1056 | * @access protected 1057 | */ 1058 | protected function addPoly( \PHPGoogleMaps\Overlay\Poly $poly ) { 1059 | return $this->polys[] = new \PHPGoogleMaps\Overlay\PolyDecorator( $poly, count( $this->polys ), $this->map_id ); 1060 | } 1061 | 1062 | /************************************************ 1063 | * 1064 | * Map Options 1065 | * 1066 | ************************************************/ 1067 | 1068 | /** 1069 | * Add a custom control to the map 1070 | * 1071 | * @param Marker $marker Marker to add 1072 | * @return MarkerDecorator 1073 | * @access protected 1074 | */ 1075 | protected function addCustomControl( \PHPGoogleMaps\Core\CustomControl $control ) { 1076 | return $this->custom_controls[] = new \PHPGoogleMaps\Core\CustomControlDecorator( $control, count( $this->custom_controls ), $this->map_id ); 1077 | } 1078 | 1079 | /** 1080 | * Set map center 1081 | * 1082 | * @param string|PositionAbstract $center Location of the center. Can be a 1083 | * location or an object that extends PositionAbstract. 1084 | * @return boolean 1085 | */ 1086 | public function setCenter( $center ) { 1087 | if ( $center instanceof \PHPGoogleMaps\Core\PositionAbstract ) { 1088 | $this->center = $center->getLatLng(); 1089 | return true; 1090 | } 1091 | $geocode_result = \PHPGoogleMaps\Service\Geocoder::geocode( (string)$center ); 1092 | if ( $geocode_result instanceof \PHPGoogleMaps\Service\GeocodeResult ) { 1093 | $this->center = $geocode_result->getLatLng(); 1094 | return true; 1095 | } 1096 | return false; 1097 | } 1098 | /** 1099 | * Set map center by coordinates 1100 | * 1101 | * Convenience function for setting the center via a lat/lng 1102 | * 1103 | * @var float $lat latitude 1104 | * @var float $lng longitude 1105 | * @return void 1106 | */ 1107 | public function setCenterCoords( $lat, $lng ) { 1108 | $this->setCenter( new \PHPGoogleMaps\Core\LatLng( floatval( $lat ), floatval( $lng ) ) ); 1109 | } 1110 | 1111 | /** 1112 | * Set map center by user location 1113 | * 1114 | * @param string $backup_location Backup location incase geolocation fails 1115 | * @return void 1116 | */ 1117 | public function centerOnUser( \PHPGoogleMaps\Core\PositionAbstract $backup_location=null ) { 1118 | $this->enableGeolocation(); 1119 | $this->center_on_user = true; 1120 | if ( $backup_location !== null ) { 1121 | $this->geolocation_backup = $backup_location; 1122 | } 1123 | } 1124 | 1125 | /** 1126 | * Add a map style 1127 | * 1128 | * @param MapStyle $style The map style 1129 | * @return void 1130 | */ 1131 | protected function addMapStyle( \PHPGoogleMaps\Overlay\MapStyle $style ) { 1132 | return $this->map_styles[$style->var_name] = $style; 1133 | } 1134 | 1135 | /** 1136 | * Set the map's types 1137 | * This sets what type of map's will be selectable by the user 1138 | * This must be called explicitly if you are adding custom map styles 1139 | * 1140 | * @param array $map_types Array of map types to enable 1141 | * @return void 1142 | */ 1143 | function setMapTypes( array $map_types ) { 1144 | foreach ( $map_types as $map_type ) { 1145 | if ( $map_type instanceof \PHPGoogleMaps\Overlay\MapStyle ) { 1146 | $this->map_types[] = $map_type->var_name; 1147 | } 1148 | else { 1149 | $this->map_types[] = $map_type; 1150 | } 1151 | } 1152 | } 1153 | 1154 | /** 1155 | * Get sidebar HTML 1156 | * 1157 | * @param string $marker_html Custom HTML to use for each marker in the sidebar 1158 | * use {title}, {content}, {icon} in the HTML as placeholders 1159 | * @param integer $tabs_deep The amount of tabs to indent the code 1160 | * @return string 1161 | */ 1162 | public function getSidebar( $marker_html='', $tabs_deep=0 ) { 1163 | $sidebar_html = sprintf( "%s
\n%s
    \n", str_repeat( "\t", $tabs_deep ), $this->map_id, str_repeat( "\t", $tabs_deep+1 ) ); 1164 | foreach( $this->getMarkers() as $marker ) { 1165 | if ( isset( $marker->sidebar ) && $marker->sidebar === false ) { 1166 | continue; 1167 | } 1168 | if ( $marker_html ) { 1169 | $marker_parsed_html = str_replace( array( '{title}', '{content}', '{icon}' ), array( $marker->title, $marker->content, $marker->icon instanceof \PHPGoogleMaps\Overlay\MarkerIcon ? $marker->icon->icon : '' ), $marker_html ); 1170 | } 1171 | $sidebar_html .= sprintf( "%s
  • \n%s%s%s
  • \n", 1172 | str_repeat( "\t", $tabs_deep+2 ), 1173 | $marker->getJsVar(), 1174 | str_repeat( "\t", $tabs_deep+3 ), 1175 | $marker_html ? $marker_parsed_html : sprintf( '

    %s

    ', $marker->title ), 1176 | str_repeat( "\t", $tabs_deep+2 ) 1177 | ); 1178 | } 1179 | $sidebar_html .= sprintf( "%s
\n
", str_repeat( "\t", $tabs_deep+1 ), str_repeat( "\t", $tabs_deep ) ); 1180 | return $sidebar_html; 1181 | } 1182 | 1183 | /** 1184 | * Enable geolocation 1185 | * This enables HTML5's geolocation 1186 | * This tests for browser compatibility so as to not cause errors 1187 | * 1188 | * @param int $geolocation_timeout Timeout in milliseconds 1189 | * @param boolean $geolocation_high_accuracy High accuracy flag 1190 | * @return void 1191 | */ 1192 | public function enableGeolocation( $geolocation_timeout=null, $geolocation_high_accuracy=null ) { 1193 | if ( $geolocation_timeout ) $this->geolocation_timeout = (int) $geolocation_timeout; 1194 | if ( $geolocation_high_accuracy ) $this->geolocation_high_accuracy = (bool) $geolocation_high_accuracy; 1195 | $this->geolocation = true; 1196 | } 1197 | 1198 | /** 1199 | * Set the geolocation fail callback 1200 | * 1201 | * @param string $callback Function to call if geolocation fails 1202 | * e.g. geofail 1203 | * @return void 1204 | */ 1205 | public function setGeolocationFailCallback( $callback ) { 1206 | $this->geolocation_fail_callback = $callback; 1207 | } 1208 | 1209 | /** 1210 | * Set the geolocation success callback 1211 | * 1212 | * @param string $callback Function to call if geolocation succeeds 1213 | * e.g. geosuccess 1214 | * @return void 1215 | */ 1216 | public function setGeolocationSuccessCallback( $callback ) { 1217 | $this->geolocation_success_callback = $callback; 1218 | } 1219 | 1220 | /** 1221 | * Set the map zoom 1222 | * 1223 | * @param int $zoom Map zoom 1224 | * @return void 1225 | */ 1226 | public function setZoom( $zoom ) { 1227 | $this->zoom = abs( (int) $zoom ); 1228 | } 1229 | 1230 | /** 1231 | * Set the map width 1232 | * e.g. 50x, 100% 1233 | * 500px is default 1234 | * 1235 | * @param string|int $width Width of the map 1236 | * @return void 1237 | */ 1238 | public function setWidth( $width ) { 1239 | $this->width = sprintf( '%s%s', $width, is_int( $width ) ? 'px' : '' ); 1240 | } 1241 | 1242 | /** 1243 | * Set the map height 1244 | * e.g. 500, 100% 1245 | * 500px is default 1246 | * 1247 | * @param string|int $height Height of the map 1248 | * @return void 1249 | */ 1250 | public function setHeight( $height ) { 1251 | $this->height = sprintf( '%s%s', $height, is_int( $height ) ? 'px' : '' ); 1252 | } 1253 | 1254 | /** 1255 | * Set the map to metric units 1256 | * 1257 | * @return void 1258 | */ 1259 | public function setUnitsMetric() { 1260 | $this->setUnits( 'METRIC' ); 1261 | } 1262 | 1263 | /** 1264 | * Set the map to imperial units 1265 | * 1266 | * @return void 1267 | */ 1268 | public function setUnitsImperial() { 1269 | $this->setUnits( 'IMPERIAL' ); 1270 | } 1271 | 1272 | /** 1273 | * Set the map units 1274 | * 1275 | * @return void 1276 | * @access private 1277 | */ 1278 | private function setUnits( $units ) { 1279 | $this->units = $units; 1280 | } 1281 | /** 1282 | * Enable map scrolling 1283 | * 1284 | * @return void 1285 | */ 1286 | public function enableScrolling() { 1287 | $this->scrollable = true; 1288 | } 1289 | 1290 | /** 1291 | * Disable map scrolling 1292 | * 1293 | * @return void 1294 | */ 1295 | public function disableScrolling() { 1296 | $this->scrollable = false; 1297 | } 1298 | 1299 | /** 1300 | * Enable map dragging 1301 | * 1302 | * @return void 1303 | */ 1304 | public function enableDragging() { 1305 | $this->draggable = true; 1306 | } 1307 | 1308 | /** 1309 | * Disable map dragging 1310 | * 1311 | * @return void 1312 | */ 1313 | public function disableDragging() { 1314 | $this->draggable = false; 1315 | } 1316 | 1317 | /** 1318 | * Enable auto encompass 1319 | * Enabled by defualt 1320 | * When markers are present this causes the map to pick an appropriate center 1321 | * and zoom. Therefore if markers are present on the map a center and zoom do 1322 | * not need to be set 1323 | * 1324 | * @return void 1325 | */ 1326 | public function enableAutoEncompass() { 1327 | $this->auto_encompass = true; 1328 | } 1329 | 1330 | /** 1331 | * Disable auto encompass 1332 | * 1333 | * @return void 1334 | */ 1335 | public function disableAutoEncompass() { 1336 | $this->auto_encompass = false; 1337 | } 1338 | 1339 | /** 1340 | * Enable compressed output 1341 | * This removed unnecessary spaces from the code to space space 1342 | * 1343 | * @return void 1344 | */ 1345 | public function enableCompressedOutput() { 1346 | $this->compress_output = true; 1347 | } 1348 | 1349 | /** 1350 | * Disable compressed output 1351 | * Disbaled is default 1352 | * 1353 | * @return void 1354 | */ 1355 | public function disbleCompressedOutput() { 1356 | $this->compress_output = false; 1357 | } 1358 | 1359 | /** 1360 | * Set the map type 1361 | * 1362 | * @param string $map_type Map type to set 1363 | * @return void 1364 | */ 1365 | public function setMapType( $map_type ) { 1366 | $this->map_type = $map_type; 1367 | } 1368 | 1369 | /** 1370 | * Set the navigation control's style 1371 | * 1372 | * @param string $style Navigation control style 1373 | * @return void 1374 | */ 1375 | public function setNavigationControlStyle( $style ) { 1376 | $this->navigation_control_style = $style; 1377 | } 1378 | 1379 | /** 1380 | * Set the map type control's style 1381 | * 1382 | * @param string $position Map type control style 1383 | * @return void 1384 | */ 1385 | public function setMapTypeControlStyle( $style ) { 1386 | $this->map_type_control_style = $style; 1387 | } 1388 | 1389 | /** 1390 | * Set the map type control's position 1391 | * 1392 | * @param string $position Position of the map type control 1393 | * @return void 1394 | */ 1395 | public function setMapTypeControlPosition( $position ) { 1396 | $this->map_type_control_position = $position; 1397 | } 1398 | 1399 | /** 1400 | * Set the navigation control's position 1401 | * 1402 | * @param string $position Position of the navigation control 1403 | * @return void 1404 | */ 1405 | public function setNavigationControlPosition( $position ) { 1406 | $this->navigation_control_position = $position; 1407 | } 1408 | 1409 | /** 1410 | * Set the scale control's position 1411 | * 1412 | * @param string $position Position of the scale control 1413 | * @return void 1414 | */ 1415 | public function setScaleControlPosition( $position ) { 1416 | $this->scale_control_position = $position; 1417 | } 1418 | 1419 | /** 1420 | * Enable the scale control 1421 | * 1422 | * @return void 1423 | */ 1424 | public function enableScaleControl() { 1425 | $this->scale_control = true; 1426 | } 1427 | 1428 | /** 1429 | * Disable the scale control 1430 | * 1431 | * @return void 1432 | */ 1433 | public function disableScaleControl() { 1434 | $this->scale_control = true; 1435 | } 1436 | 1437 | /** 1438 | * Enable the navigation control 1439 | * 1440 | * @return void 1441 | */ 1442 | public function enableNavigationControl() { 1443 | $this->navigation_control = false; 1444 | } 1445 | 1446 | /** 1447 | * Disable the navigation control 1448 | * 1449 | * @return void 1450 | */ 1451 | public function disableNavigationControl() { 1452 | $this->navigation_control = false; 1453 | } 1454 | 1455 | /** 1456 | * Enable the map type control 1457 | * 1458 | * @return void 1459 | */ 1460 | public function enableMapTypeControl() { 1461 | $this->map_type_control = false; 1462 | } 1463 | 1464 | /** 1465 | * Disable the map type control 1466 | * 1467 | * @return void 1468 | */ 1469 | public function disableMapTypeControl() { 1470 | $this->map_type_control = false; 1471 | } 1472 | 1473 | /** 1474 | * Add an event listener to the map 1475 | * 1476 | * @param DomEventListener $event_listener 1477 | * @return EventListenerDecorator 1478 | * @access protected 1479 | */ 1480 | protected function addEventListener( \PHPGoogleMaps\Event\EventListener $event_listener ) { 1481 | return $this->event_listeners[] = new \PHPGoogleMaps\Event\EventListenerDecorator( $event_listener, count( $this->event_listeners ), $this->map_id ); 1482 | } 1483 | 1484 | /** 1485 | * Set the map language 1486 | * @link http://code.google.com/apis/maps/documentation/javascript/basics.html#Localization 1487 | * 1488 | * @param string $language Language of the map 1489 | * @return void 1490 | */ 1491 | public function setLanguage( $language ) { 1492 | $this->language = $language; 1493 | } 1494 | 1495 | /** 1496 | * Set the API version 1497 | * @link http://code.google.com/apis/maps/documentation/javascript/basics.html#Versioning 1498 | * 1499 | * @param string $api_version API version 1500 | * @return void 1501 | */ 1502 | public function setApiVersion( $api_version ) { 1503 | $this->api_version = (float) $api_version; 1504 | } 1505 | 1506 | /** 1507 | * Set the map region 1508 | * @link http://code.google.com/apis/maps/documentation/javascript/basics.html#Localization 1509 | * 1510 | * @param string $region Map region 1511 | * @return void 1512 | */ 1513 | public function setRegion( $region ) { 1514 | $this->region = $region; 1515 | } 1516 | 1517 | /** 1518 | * Enable mobile display 1519 | * 1520 | * @var boolean $mobile_iphone_fullscreen If you have set the height of the map to 100% and 1521 | * $mobile_iphone_fullscreen is true, then the iphone navigation bar will be hidden on page load 1522 | * @link http://code.google.com/apis/maps/documentation/javascript/basics.html#Mobile 1523 | * 1524 | * @return void 1525 | */ 1526 | public function enableMobile( $mobile_iphone_fullscreen=null) { 1527 | $this->mobile = true; 1528 | if ( isset( $mobile_iphone_fullscreen ) ) { 1529 | $this->mobile_iphone_fullscreen = $mobile_iphone_fullscreen; 1530 | } 1531 | } 1532 | 1533 | /************************************* 1534 | * 1535 | * Markers 1536 | * 1537 | **************************************/ 1538 | 1539 | /** 1540 | * Stagger markers 1541 | * This will set a small timeout between marker additions 1542 | * 1543 | * @param integer $timeout Milliseconds 1544 | * @return void 1545 | */ 1546 | public function enableMarkerStaggering( $timeout=100 ) { 1547 | $this->stagger_markers = $timeout; 1548 | $this->addObject( 1549 | new \PHPGoogleMaps\Event\EventListener( 1550 | $this, 1551 | 'idle', 1552 | sprintf( 'function(){j=0;for(var i=0;i<%1$s.length-1;i++){setTimeout(function(){%1$s[j] = new google.maps.Marker(%1$s[j++])},i*%2$s)};setTimeout(function(){%1$s[%1$s.length-1] = new google.maps.Marker(%1$s[%1$s.length-1])},((%1$s.length-1)*%2$s))}', 1553 | $this->getMarkersJsVar(), 1554 | $this->stagger_markers 1555 | ), 1556 | true 1557 | ) 1558 | ); 1559 | } 1560 | 1561 | /** 1562 | * Add a marker to the map 1563 | * 1564 | * @param Marker $marker Marker to add 1565 | * @return MarkerDecorator 1566 | * @access protected 1567 | */ 1568 | protected function addMarker( \PHPGoogleMaps\Overlay\Marker $marker ) { 1569 | if ( !$marker->getIcon() && $this->default_marker_icon ) { 1570 | $marker->setIcon( $this->default_marker_icon, $this->default_marker_shadow ?: null ); 1571 | } 1572 | return $this->markers[] = new \PHPGoogleMaps\Overlay\MarkerDecorator( $marker, count( $this->markers ), $this->map_id ); 1573 | } 1574 | 1575 | 1576 | 1577 | /** 1578 | * Get map markers 1579 | * Returns an array of the map's markers 1580 | * 1581 | * @return array 1582 | */ 1583 | public function getMarkers() { 1584 | return $this->markers; 1585 | } 1586 | 1587 | /** 1588 | * Set default marker icon 1589 | * Sets the default icon to be used by the map 1590 | * 1591 | * @param MarkerIcon $icon Default marker icon 1592 | * @param MarkerIcon $shadow Shadow of the default marker icon 1593 | * @return void 1594 | */ 1595 | public function setDefaultMarkerIcon( \PHPGoogleMaps\Overlay\MarkerIcon $icon, \PHPGoogleMaps\Overlay\MarkerIcon $shadow = null ) { 1596 | $this->default_marker_icon = $icon; 1597 | if ( $shadow ) { 1598 | $this->default_marker_shadow = $shadow; 1599 | } 1600 | } 1601 | 1602 | /** 1603 | * Get marker groups 1604 | * Returns the map's marker groups 1605 | * 1606 | * @return array 1607 | */ 1608 | public function getMarkerGroups() { 1609 | $this->extractMarkerData(); 1610 | foreach( $this->marker_groups as $mg ) { 1611 | $groups[] = new \PHPGoogleMaps\Overlay\MarkerGroupDecorator( new \PHPGoogleMaps\Overlay\MarkerGroup( $mg['name'] ), $mg['id'], $this->map_id ); 1612 | } 1613 | return $groups; 1614 | } 1615 | 1616 | 1617 | /************************************** 1618 | * 1619 | * Objects 1620 | * 1621 | **************************************/ 1622 | 1623 | /** 1624 | * Add an object to the map 1625 | * 1626 | * This method calls the various protected add* methods() which 1627 | * decorate the objects to allow added functionality 1628 | * 1629 | * If the objected is already decorated this will strip the 1630 | * decoration and redecorate it with the new map's info 1631 | * 1632 | * @param object $object Object to add to the map 1633 | * @return object Returns a decorated object 1634 | */ 1635 | public function addObject( $object ) { 1636 | if ( !is_object( $object ) ) { 1637 | return false; 1638 | } 1639 | if ( $object instanceof \PHPGoogleMaps\Core\MapObjectDecorator ) { 1640 | $object = $object->decoratee; 1641 | } 1642 | switch( get_class( $object ) ) { 1643 | case 'PHPGoogleMaps\Core\CustomControl': 1644 | $object = $this->addCustomControl( $object ); 1645 | break; 1646 | case 'PHPGoogleMaps\Overlay\Marker': 1647 | $object = $this->addMarker( $object ); 1648 | break; 1649 | case 'PHPGoogleMaps\Overlay\MapStyle': 1650 | $object = $this->addMapStyle( $object ); 1651 | break; 1652 | case 'PHPGoogleMaps\Layer\FusionTable': 1653 | $object = $this->addFusionTable( $object ); 1654 | break; 1655 | case 'PHPGoogleMaps\Layer\KmlLayer': 1656 | $object = $this->addKmlLayer( $object ); 1657 | break; 1658 | case 'PHPGoogleMaps\Layer\PanoramioLayer': 1659 | $this->libraries[] = 'panoramio'; 1660 | $object = $this->addPanoramioLayer( $object ); 1661 | break; 1662 | case 'PHPGoogleMaps\Overlay\GroundOverlay': 1663 | $object = $this->addGroundOverlay( $object ); 1664 | break; 1665 | case 'PHPGoogleMaps\Event\EventListener': 1666 | case 'PHPGoogleMaps\Event\DomEventListener': 1667 | $object = $this->addEventListener( $object ); 1668 | break; 1669 | case 'PHPGoogleMaps\Overlay\Polygon': 1670 | case 'PHPGoogleMaps\Overlay\Polyline': 1671 | $object = $this->addPoly( $object ); 1672 | break; 1673 | case 'PHPGoogleMaps\Overlay\Circle': 1674 | case 'PHPGoogleMaps\Overlay\Rectangle': 1675 | $object = $this->addShape( $object ); 1676 | break; 1677 | case 'PHPGoogleMaps\Service\WalkingDirections': 1678 | case 'PHPGoogleMaps\Service\BicyclingDirections': 1679 | case 'PHPGoogleMaps\Service\DrivingDirections': 1680 | $object = $this->addDirections( $object ); 1681 | break; 1682 | default: 1683 | return false; 1684 | } 1685 | return $object; 1686 | } 1687 | 1688 | /** 1689 | * Add an array objects to the map 1690 | * 1691 | * @param array $objects Array of objects to add to the map 1692 | * @return void 1693 | */ 1694 | 1695 | public function addObjects( array $objects ) { 1696 | foreach( $objects as $object ) { 1697 | $this->addObject( $object ); 1698 | } 1699 | } 1700 | 1701 | /** 1702 | * Bind a map object to another 1703 | * 1704 | * Example 1705 | * 1706 | * $map = new \PHPGoogleMaps\GoogleMap(); 1707 | * $circle = \PHPGoogleMaps\Overlay\Circle::createFromLocation( 'San Diego, CA', 100, $circle_options ); 1708 | * $marker = \PHPGoogleMaps\Overlay\Marker::createFromLocation( 'San Diego, CA', array( 'draggable' => true ) ); 1709 | * $objects = array( &$circle, &$marker ); 1710 | * $map->addObjects( $objects ); 1711 | * $map->bind( $circle, 'center', $marker, 'position' ); 1712 | * 1713 | * @return void 1714 | */ 1715 | public function bind( \PHPGoogleMaps\Core\MapObjectDecorator $binder, $binder_property, \PHPGoogleMaps\Core\MapObjectDecorator $bindee, $bindee_property ) { 1716 | $this->binds[] = array( 1717 | 'binder' => $binder, 1718 | 'binder_property' => $binder_property, 1719 | 'bindee' => $bindee, 1720 | 'bindee_property' => $bindee_property 1721 | ); 1722 | } 1723 | 1724 | /****************************************** 1725 | * 1726 | * Javascript output 1727 | * 1728 | ******************************************/ 1729 | 1730 | /** 1731 | * Print the map HTML 1732 | * 1733 | * @return void 1734 | */ 1735 | function printMap() { 1736 | echo $this->getMap(); 1737 | } 1738 | 1739 | /** 1740 | * Get the map HTML 1741 | * 1742 | * @return string 1743 | */ 1744 | function getMap() { 1745 | $iphone_safari_hide_navbar = sprintf( "\n", $this->map_id ); 1746 | return sprintf( '
%s
%s', $this->map_id, ( $this->width ? 'width:' . $this->width . ';' : '' ), ( $this->height ? 'height:' . $this->height . ';' : '' ), $this->loading_content ? $this->loading_content : '', $this->mobile_iphone_fullscreen ? $iphone_safari_hide_navbar : '' ); 1747 | } 1748 | 1749 | 1750 | /** 1751 | * Print the map header javascript 1752 | * 1753 | * @return void 1754 | */ 1755 | function printHeaderJS() { 1756 | echo $this->getHeaderJS(); 1757 | } 1758 | 1759 | /** 1760 | * Get the map header javascript 1761 | * 1762 | * @return string 1763 | */ 1764 | function getHeaderJS() { 1765 | $header_js = sprintf( 1766 | "%s\n\n", 1767 | ( $this->mobile ? "\n" : '' ), 1768 | $this->use_https ? 's' : '', 1769 | json_encode( $this->sensor ), 1770 | $this->api_version, 1771 | $this->language, 1772 | $this->region, 1773 | ( count( $this->libraries ) ? sprintf( implode( ',', $this->libraries ) ) : '' ), 1774 | $this->api_key ? $this->api_key : '' 1775 | ); 1776 | if ( $this->clustering_js ) { 1777 | $header_js .= sprintf( "\n", $this->clustering_js ); 1778 | } 1779 | return $header_js; 1780 | } 1781 | 1782 | /** 1783 | * Print the map javascript 1784 | * 1785 | * @return void 1786 | */ 1787 | function printMapJS() { 1788 | echo $this->getMapJS(); 1789 | } 1790 | 1791 | /** 1792 | * Get the map javascript 1793 | * 1794 | * @return string 1795 | */ 1796 | function getMapJS() { 1797 | 1798 | $output = sprintf( "var %s;\nfunction phpgooglemap_%s() {\n\nthis.initialize = function() {\n\n", $this->map_id, $this->map_id ); 1799 | $output .= "\tvar self = this;\n"; 1800 | $output .= "\tthis.map_options = {\n"; 1801 | $output .= sprintf("\t\tzoom: %s,\n", $this->zoom ); 1802 | 1803 | if ( !$this->scrollable ) { 1804 | $output .= "\t\tscrollwheel: false,\n"; 1805 | } 1806 | if ( !$this->streetview ) { 1807 | $output .= "\t\tstreetViewControl: false,\n"; 1808 | } 1809 | if ( !$this->draggable ) { 1810 | $output .= "\t\tdraggable: false,\n"; 1811 | } 1812 | 1813 | $output .= sprintf( "\t\tnavigationControl: %s,\n", $this->phpToJs( $this->navigation_control ) ); 1814 | $output .= sprintf( "\t\tmapTypeControl: %s,\n", $this->phpToJs( $this->map_type_control ) ); 1815 | $output .= sprintf( "\t\tscaleControl: %s,\n", $this->phpToJs( $this->scale_control ) ); 1816 | 1817 | $output .= "\t\tnavigationControlOptions: {\n"; 1818 | if ( $this->navigation_control_style ) { 1819 | $output .= sprintf( "\t\t\tstyle: google.maps.NavigationControlStyle.%s,\n", strtoupper( $this->navigation_control_style ) ); 1820 | } 1821 | if ( $this->navigation_control_position ) { 1822 | $output .= sprintf( "\t\t\tposition: google.maps.ControlPosition.%s,\n", strtoupper( $this->navigation_control_position ) ); 1823 | } 1824 | $output .= "\t\t},\n"; 1825 | 1826 | $output .= "\t\tmapTypeControlOptions: {\n"; 1827 | if ( $this->map_type_control_style ) { 1828 | $output .= sprintf( "\t\t\tstyle: google.maps.MapTypeControlStyle.%s,\n", strtoupper( $this->map_type_control_style ) ); 1829 | } 1830 | if ( $this->map_type_control_position ) { 1831 | $output .= sprintf( "\t\t\tposition: google.maps.ControlPosition.%s,\n", strtoupper( $this->map_type_control_position ) ); 1832 | } 1833 | if ( count( $this->map_types ) ) { 1834 | $map_types_string = ''; 1835 | foreach( $this->map_types as $map_type ) { 1836 | if ( isset( $this->map_styles[$map_type] ) ) { 1837 | $map_types_string .= sprintf( "'%s',", $this->map_styles[$map_type]->var_name ); 1838 | } 1839 | else { 1840 | $map_types_string .= sprintf( "google.maps.MapTypeId.%s,", strtoupper( $map_type ) ); 1841 | } 1842 | } 1843 | $output .= sprintf( "\t\t\tmapTypeIds: [%s],\n", rtrim( $map_types_string, ',' ) ); 1844 | } 1845 | $output .= "\t\t},\n"; 1846 | $output .= "\t\tscaleControlOptions: {\n"; 1847 | if ( $this->scale_control_position ) { 1848 | $output .= sprintf( "\t\t\tposition: google.maps.ControlPosition.%s,\n", strtoupper( $this->scale_control_position ) ); 1849 | } 1850 | $output .= "\t\t},\n"; 1851 | 1852 | $output .= sprintf("\t\tmapTypeId: google.maps.MapTypeId.%s,\n", strtoupper( $this->map_type ) ); 1853 | $output .= "\t};\n\n"; 1854 | $output .= sprintf( "\tthis.map = new google.maps.Map(document.getElementById(\"%s\"), this.map_options);\n", $this->map_id ); 1855 | 1856 | foreach( $this->map_styles as $map_style ) { 1857 | $output .= sprintf( "\t%sMapStyle = %s;\n", $map_style->var_name, $map_style->style ); 1858 | $output .= sprintf( "\t%sStyleOptions = { name: \"%s\"};\n", $map_style->var_name, $map_style->name ); 1859 | $output .= sprintf( "\t%sMapType = new google.maps.StyledMapType(%sMapStyle, %sStyleOptions);\n", $map_style->var_name, $map_style->var_name, $map_style->var_name ); 1860 | $output .= sprintf( "\tthis.map.mapTypes.set('%s', %sMapType);\n", $map_style->var_name, $map_style->var_name ); 1861 | } 1862 | 1863 | if ( count( $this->custom_controls ) ) { 1864 | $output .= "\n\tthis.custom_controls = [];\n"; 1865 | foreach( $this->custom_controls as $n => $custom_control ) { 1866 | $output .= sprintf( "\tvar cc%s_outer = document.createElement('DIV');\n", $n ); 1867 | foreach( $custom_control->options['outer'] as $var => $val ) { 1868 | $output .= sprintf( "\tcc%s_outer.%s = '%s';\n", $n, $var, $val ); 1869 | } 1870 | $output .= sprintf( "\tvar cc%s_inner = document.createElement('DIV');\n", $n ); 1871 | foreach( $custom_control->options['inner'] as $var => $val ) { 1872 | $output .= sprintf( "\tcc%s_inner.%s = '%s';\n", $n, $var, $val ); 1873 | } 1874 | $output .= sprintf( "\tcc%s_outer.appendChild(cc%s_inner);\n", $n, $n ); 1875 | $output .= sprintf( "\tvar cc%s_holder = document.createElement('DIV');\n", $n ); 1876 | $output .= sprintf( "\tcc%s_holder.appendChild(cc%s_outer);\n", $n, $n ); 1877 | $output .= sprintf( "\tthis.map.controls[google.maps.ControlPosition.%s].push(cc%s_holder);\n", $custom_control->position, $n ); 1878 | $output .= sprintf( "\tthis.custom_controls[%s] = cc%s_holder;\n\n", $n, $n ); 1879 | foreach( $custom_control->listeners as $listener ) { 1880 | $output .= sprintf( "\tgoogle.maps.event.addDomListener(cc%s_holder, '%s', %s);\n\n", $n, $listener['event'], $listener['function'] ); 1881 | } 1882 | } 1883 | } 1884 | 1885 | 1886 | if ( count( $this->shapes ) ) { 1887 | $output .= sprintf( "\n\tthis.shapes = [];\n", $this->map_id ); 1888 | foreach( $this->shapes as $n => $shape ) { 1889 | if ( $shape->decoratee instanceof \PHPGoogleMaps\Overlay\Circle ) { 1890 | $output .= sprintf( "\tthis.shapes[%s] = new google.maps.Circle( {\n", $n ); 1891 | $output .= sprintf( "\t\tcenter: new google.maps.LatLng(%s,%s),\n", $shape->center->getLat(), $shape->center->getLng() ); 1892 | $output .= sprintf( "\t\tradius: %s,\n", $shape->radius ); 1893 | } 1894 | elseif ( $shape->decoratee instanceof \PHPGoogleMaps\Overlay\Rectangle ) { 1895 | $output .= sprintf( "\tthis.shapes[%s] = new google.maps.Rectangle( {\n", $n ); 1896 | $output .= sprintf( "\t\tbounds: new google.maps.LatLngBounds(new google.maps.LatLng(%s,%s),new google.maps.LatLng(%s,%s)),\n", 1897 | $shape->southwest->getLat(), 1898 | $shape->southwest->getLng(), 1899 | $shape->northeast->getLat(), 1900 | $shape->northeast->getLng() 1901 | ); 1902 | } 1903 | foreach( $shape->getOptions() as $var => $val ) { 1904 | $output .= sprintf( "\t\t%s: %s,\n", $var, $this->phpToJs( $val ) ); 1905 | } 1906 | $output .= sprintf( "\t\tmap: this.map\n" ); 1907 | $output .= "\t} );\n"; 1908 | } 1909 | } 1910 | 1911 | if ( count( $this->polys ) ) { 1912 | $output .= sprintf( "\n\tthis.polys = [];\n", $this->map_id ); 1913 | foreach( $this->polys as $n => $poly ) { 1914 | if ( $poly->decoratee instanceof \PHPGoogleMaps\Overlay\Polygon ) { 1915 | $output .= sprintf( "\tthis.polys[%s] = new google.maps.Polygon( {\n", $n ); 1916 | foreach( $poly->getOptions() as $var => $val ) { 1917 | $output .= sprintf( "\t\t%s: %s,\n", $var, $this->phpToJs( $val ) ); 1918 | } 1919 | $output .= sprintf( "\t\tpaths: %s,\n", $this->parseLatLngs( $this->phpToJs( $poly->getPaths() ) ) ); 1920 | $output .= sprintf( "\t\tmap: this.map\n" ); 1921 | $output .= "\t} );\n"; 1922 | } 1923 | elseif ( $poly->decoratee instanceof \PHPGoogleMaps\Overlay\Polyline ) { 1924 | $output .= sprintf( "\tthis.polys[%s] = new google.maps.Polyline( {\n", $n ); 1925 | foreach( $poly->getOptions() as $var => $val ) { 1926 | $output .= sprintf( "\t\t%s: %s,\n", $var, $this->phpToJs( $val ) ); 1927 | } 1928 | $output .= sprintf( "\t\tpath: %s,\n", $this->parseLatLngs( $this->phpToJs( $poly->getPaths() ) ) ); 1929 | $output .= sprintf( "\t\tmap: this.map\n" ); 1930 | $output .= "\t} );\n"; 1931 | } 1932 | } 1933 | } 1934 | 1935 | if ( $this->directions ) { 1936 | $output .= "\tthis.directions = {};\n"; 1937 | $renderer_options = "\tthis.directions.renderer_options = {\n"; 1938 | foreach ( $this->directions->renderer_options as $renderer_option => $renderer_value ) { 1939 | switch ( $renderer_option ) { 1940 | case 'panel': 1941 | $renderer_options .= sprintf( "\t\tpanel: document.getElementById(\"%s\"),\n", $renderer_value ); 1942 | break; 1943 | default: 1944 | $renderer_options .= sprintf( "\t\t%s:%s,\n", $renderer_option, $this->phpToJs( $renderer_value ) ); 1945 | } 1946 | } 1947 | $renderer_options .= "\t};\n\n"; 1948 | $output .= $renderer_options; 1949 | 1950 | $output .= "\tthis.directions.renderer = new google.maps.DirectionsRenderer(this.directions.renderer_options);\n\tthis.directions.service = new google.maps.DirectionsService();\n"; 1951 | $output .= "\tthis.directions.renderer.setMap(this.map);\n\n"; 1952 | 1953 | $request_options = sprintf( "\tthis.directions.request_options = {\n", $this->map_id ); 1954 | if ( isset( $this->units ) && !isset( $this->directions->request_options['units'] ) ) { 1955 | $this->directions->request_options['units'] = $this->units; 1956 | } 1957 | foreach ( $this->directions->request_options as $request_option => $request_value ) { 1958 | switch ( $request_option ) { 1959 | case 'waypoints': 1960 | $request_options .= sprintf("\t\twaypoints: %s,\n", $this->parseLatLngs( $this->phptoJs( $request_value ) ) ); 1961 | break; 1962 | case 'origin': 1963 | $request_options .= sprintf( "\t\torigin: new google.maps.LatLng(%s,%s),\n", $this->directions->request_options['origin']->getLat(), $this->directions->request_options['origin']->getLng() ); 1964 | break; 1965 | case 'destination': 1966 | $request_options .= sprintf( "\t\tdestination: new google.maps.LatLng(%s,%s),\n", $this->directions->request_options['destination']->getLat(), $this->directions->request_options['destination']->getLng() ); 1967 | break; 1968 | case 'travelMode': 1969 | $request_options .= sprintf( "\t\ttravelMode: google.maps.DirectionsTravelMode.%s,\n", strtoupper( $this->directions->request_options['travelMode'] ) ); 1970 | break; 1971 | case 'units': 1972 | $request_options .= sprintf( "\t\tunitSystem: google.maps.DirectionsUnitSystem.%s,\n", isset( $this->directions->request_options['units'] ) ? $this->directions->request_options['units'] : $this->units ); 1973 | break; 1974 | default: 1975 | $request_options .= sprintf( "\t\t%s:%s,\n", $request_option, $this->phpToJs( $request_value ) ); 1976 | } 1977 | } 1978 | $request_options .= "\t};\n"; 1979 | $output .= $request_options; 1980 | $output .= "\t\n\tthis.directions.service.route(this.directions.request_options, function(response,status) {\n\t\tif (status == google.maps.DirectionsStatus.OK) {\n\t\t\tself.directions.success = response;\n\t\t\tself.directions.renderer.setDirections(response);\n\t\t}\n\t\telse {\n\t\t\tself.directions.error = status;\n\t\t}\n\t});\n\n"; 1981 | } 1982 | 1983 | if ( count( $this->marker_shapes ) ) { 1984 | $output .= sprintf( "\n\tthis.marker_shapes = [];\n", $this->map_id ); 1985 | foreach ( $this->marker_shapes as $marker_shape ) { 1986 | $output .= sprintf( "\tthis.marker_shapes[%s] = {\n", $marker_shape->id ); 1987 | $output .= sprintf( "\t\ttype: \"%s\",\n", $marker_shape->type ); 1988 | $output .= sprintf( "\t\tcoord: [%s]\n", implode( ",", $marker_shape->coords ) ); 1989 | $output .= "\t};\n"; 1990 | } 1991 | } 1992 | 1993 | $this->extractMarkerData(); 1994 | 1995 | if ( count( $this->marker_icons ) ) { 1996 | $output .= sprintf( "\n\tthis.marker_icons = [];\n", $this->map_id ); 1997 | foreach ( $this->marker_icons as $marker_icon_id => $marker_icon ) { 1998 | $output .= sprintf( "\tthis.marker_icons[%s] = new google.maps.MarkerImage(\n\t\t\"%s\",\n", $marker_icon_id, $marker_icon->icon ); 1999 | $output .= sprintf( "\t\tnew google.maps.Size(%s, %s),\n", $marker_icon->width, $marker_icon->height ); 2000 | $output .= sprintf( "\t\tnew google.maps.Point(%s, %s),\n", (int)$marker_icon->origin_x, (int)$marker_icon->origin_y ); 2001 | $output .= sprintf( "\t\tnew google.maps.Point(%s, %s)\n", (int)$marker_icon->anchor_x, (int)$marker_icon->anchor_y ); 2002 | $output .= "\t);\n"; 2003 | } 2004 | } 2005 | 2006 | if ( count( $this->markers ) && $this->auto_encompass ) { 2007 | $output .= "\n\tthis.bounds = new google.maps.LatLngBounds();\n"; 2008 | } 2009 | 2010 | if ( $this->info_windows ) { 2011 | $output .= "\tthis.info_window = new google.maps.InfoWindow();\n"; 2012 | } 2013 | 2014 | if ( count( $this->marker_shapes ) ) { 2015 | $output .= sprintf( "\n\tthis.marker_shapes = [];\n", $this->map_id ); 2016 | foreach ( $this->marker_shapes as $shape_id => $marker_shape ) { 2017 | $output .= sprintf( "\tthis.marker_shapes[%s] = {\n", $shape_id ); 2018 | $output .= sprintf( "\t\ttype: \"%s\",\n", $marker_shape->type ); 2019 | $output .= sprintf( "\t\tcoord: [%s]\n", implode( ",", $marker_shape->coords ) ); 2020 | $output .= "\t};\n"; 2021 | } 2022 | } 2023 | 2024 | if ( count( $this->marker_groups ) ) { 2025 | $output .= "\n\tthis.marker_groups = [];\n"; 2026 | $output .= "\tthis.marker_group_function = function( group_name, f_all, f_group ) {\n\t\tfor (i in map.markers) {\n\t\t\tvar marker = map.markers[i];\n\t\t\tf_all(marker);\n\t\t}\n\t\tfor (i in map.marker_groups[group_name].markers) {\n\t\t\tvar marker = map.markers[map.marker_groups[group_name].markers[i]];\n\t\t\tf_group(marker);\n\t\t}\n\t};\n"; 2027 | foreach( $this->marker_groups as $marker_group_var => $marker_group ) { 2028 | $output .= sprintf( "\tthis.marker_groups[\"%s\"] = {name: \"%s\", markers:[%s]};\n", $marker_group_var, $marker_group['name'], implode( ',', $marker_group['markers'] ) ); 2029 | } 2030 | } 2031 | 2032 | if ( count( $this->markers ) ) { 2033 | $output .= "\n\tthis.markers = [];\n"; 2034 | } 2035 | foreach ( $this->getMarkers() as $marker_id => $marker ) { 2036 | if ( $marker->isGeolocated() ) { 2037 | if ( !$this->geolocation ) { 2038 | $this->enableGeolocation( $marker->geolocation_timeout, $marker->geolocation_high_accuracy ); 2039 | } 2040 | $output .= "\tif ( navigator.geolocation && typeof geolocation != 'undefined' ) {\n"; 2041 | } 2042 | if ( $this->stagger_markers ) { 2043 | $output .= sprintf( "\tthis.markers[%s] = {\n", $marker_id ); 2044 | } 2045 | else { 2046 | $output .= sprintf( "\tthis.markers[%s] = new google.maps.Marker({\n", $marker_id ); 2047 | } 2048 | if ( $marker->geolocation ) { 2049 | $output .= "\t\tposition: geolocation,\n"; 2050 | } 2051 | else { 2052 | $output .= sprintf( "\t\tposition: new google.maps.LatLng(%s,%s),\n", $marker->position->getLat(), $marker->position->getLng() ); 2053 | } 2054 | if ( !$this->clustering_js ) { 2055 | $output .= "\t\tmap: this.map,\n"; 2056 | } 2057 | if ( is_int( $marker->_icon_id ) ) { 2058 | $output .= sprintf( "\t\ticon:this.marker_icons[%s],\n", $marker->_icon_id ); 2059 | } 2060 | if ( is_int( $marker->_shadow_id ) ) { 2061 | $output .= sprintf( "\t\tshadow:this.marker_icons[%s],\n", $marker->_shadow_id ); 2062 | } 2063 | if ( is_int( $marker->_shape_id ) ) { 2064 | $output .= sprintf( "\t\tshape:this.marker_shapes[%s],\n", $marker->_shape_id ); 2065 | } 2066 | if ( count( $marker->groups ) ) { 2067 | $gs = $this->marker_groups; 2068 | $output .= sprintf( "\t\tgroups:[%s],\n", implode( ',', array_map( function( $g ) use ( $gs ) { return $gs[$g->var_name]['id']; }, $marker->groups ) ) ); 2069 | } 2070 | if ( $marker->animation ) { 2071 | $output .= sprintf( "\t\tanimation: google.maps.Animation.%s,\n", strtoupper( $marker->animation ) ); 2072 | $marker->removeOption( 'animation' ); 2073 | } 2074 | foreach( $marker->getOptions() as $marker_option => $marker_value ) { 2075 | $output .= sprintf( "\t\t%s:%s,\n", $marker_option, $this->phpToJs( $marker_value ) ); 2076 | } 2077 | 2078 | $output .= sprintf( "\t}%s;\n", $this->stagger_markers ? '' : ')'); 2079 | 2080 | if ( $this->info_windows ) { 2081 | if ( isset( $marker->content ) ) { 2082 | $output .= sprintf( "\tgoogle.maps.event.addListener(this.markers[%s], 'click', function() { if ( !self.markers[%s].getVisible() ) return; self.info_window.setContent(self.markers[%s].content);self.info_window.open(self.map,self.markers[%s]); });\n", $marker_id, $marker_id, $marker_id, $marker_id ); 2083 | } 2084 | } 2085 | 2086 | if ( $this->auto_encompass & !isset( $marker->location ) ) { 2087 | $output .= sprintf( "\tthis.bounds.extend(this.markers[%s].position);\n", $marker_id ); 2088 | $output .= "\tthis.map.fitBounds(this.bounds);\n"; 2089 | } 2090 | 2091 | if ( $marker->geolocation ) { 2092 | $output .= "\t}\n\n"; 2093 | } 2094 | 2095 | } 2096 | if ( $this->clustering_js ) { 2097 | $output .= sprintf( "\n\tvar markerCluster = new MarkerClusterer(this.map, this.markers, %s);\n", $this->phpToJs( $this->clustering_options ) ); 2098 | } 2099 | if ( count( $this->ground_overlays ) ) { 2100 | $output .= "\tthis.ground_overlays = [];\n"; 2101 | foreach( $this->ground_overlays as $n => $ground_overlay ) { 2102 | $output .= sprintf( "\tthis.ground_overlays[%s] = new google.maps.GroundOverlay('%s', new google.maps.LatLngBounds(new google.maps.LatLng(%s,%s),new google.maps.LatLng(%s,%s)), %s);\n\tthis.ground_overlays[%s].setMap(this.map);\n\n", 2103 | $n, 2104 | $ground_overlay->url, 2105 | $ground_overlay->southwest->getLat(), 2106 | $ground_overlay->southwest->getLng(), 2107 | $ground_overlay->northeast->getLat(), 2108 | $ground_overlay->northeast->getLng(), 2109 | $this->phpToJs( $ground_overlay->options ), 2110 | $n 2111 | ); 2112 | } 2113 | } 2114 | 2115 | if ( count( $this->kml_layers ) ) { 2116 | $output .= "\tthis.kml_layers = [];\n"; 2117 | foreach( $this->kml_layers as $n => $kml_layer ) { 2118 | $output .= sprintf( "\tthis.kml_layers[%s] = new google.maps.KmlLayer('%s', %s);\n\tthis.kml_layers[%s].setMap(this.map);\n\n", $n, $kml_layer->url, $this->phpToJs( $kml_layer->options ), $n ); 2119 | } 2120 | } 2121 | 2122 | if ( count( $this->panoramio_layers ) ) { 2123 | $output .= "\tthis.panoramio_layers = [];\n"; 2124 | foreach( $this->panoramio_layers as $n => $panoramio_layer ) { 2125 | $output .= sprintf( "\tthis.panoramio_layers[%s] = new google.maps.panoramio.PanoramioLayer();\n\tthis.panoramio_layers[%s].setMap(this.map);\n", $n, $n ); 2126 | if ( $p_tag = $panoramio_layer->getOption('tag') ) { 2127 | $output .= sprintf( "\tthis.panoramio_layers[%s].setTag('%s');\n", $n, $p_tag ); 2128 | } 2129 | if ( $p_user_id = $panoramio_layer->getOption('user_id') ) { 2130 | $output .= sprintf( "\tthis.panoramio_layers[%s].setUserId('%s');\n", $n, $p_user_id ); 2131 | } 2132 | $output .= "\n"; 2133 | } 2134 | } 2135 | 2136 | if ( count( $this->fusion_tables ) ) { 2137 | $output .= "\tthis.fusion_tables = [];\n"; 2138 | foreach ( $this->fusion_tables as $n => $fusion_table ) { 2139 | $ft_options = ''; 2140 | foreach( $fusion_table->getOptions() as $var => $val ) { 2141 | if ( $var == 'query' ) { 2142 | $val = $this->switchQuotes( $val ); 2143 | } 2144 | $ft_options .= sprintf( "\t\t%s: %s,\n", $this->phpToJs( $var ), $this->phpToJs( $val ) ); 2145 | } 2146 | $output .= sprintf( "\tthis.fusion_tables[%s] = new google.maps.FusionTablesLayer(%s, {\n%s\t});\n\tthis.fusion_tables[%s].setMap(this.map);\n\n", $n, $fusion_table->table_id, $ft_options, $n ); 2147 | } 2148 | } 2149 | 2150 | if ( count( $this->binds ) ) { 2151 | foreach( $this->binds as $bind ) { 2152 | $output .= sprintf( "\t%s.bindTo('%s', %s, '%s');\n", $bind['bindee']->getJsVar(), $bind['bindee_property'], $bind['binder']->getJsVar(), $bind['binder_property'] ); 2153 | } 2154 | } 2155 | 2156 | if ( $this->adsense ) { 2157 | $output .= sprintf( 2158 | "\tadsense_options = {\n\t\tformat: google.maps.adsense.AdFormat.%s,\n\t\tposition: google.maps.ControlPosition.%s,\n\t\tmap: this.map,\n\t\tvisible: %s,\n\t\tpublisherId: '%s'\n\t}\n\tad_unit = new google.maps.adsense.AdUnit(document.createElement('div'), adsense_options);\n\n", 2159 | strtoupper( $this->adsense_format ), 2160 | strtoupper( $this->adsense_position ), 2161 | $this->phpToJs( $this->adsense_visible ), 2162 | $this->adsense_publisher_id 2163 | ); 2164 | } 2165 | 2166 | if ( $this->autocomplete_input_id ) 2167 | { 2168 | $output .= sprintf( 2169 | "\tthis.autocomplete_input = document.getElementById('%s');\n\tthis.autocomplete_options = %s;\n\tthis.autocomplete = new google.maps.places.Autocomplete(this.autocomplete_input, this.autocomplete_options);\n", 2170 | $this->autocomplete_input_id, 2171 | $this->phpToJs( $this->autocomplete_options ) 2172 | ); 2173 | 2174 | } 2175 | 2176 | if ( $this->traffic_layer ) { 2177 | $output .= "\tthis.traffic_layer = new google.maps.TrafficLayer();\n\tthis.traffic_layer.setMap(this.map);\n\n"; 2178 | } 2179 | 2180 | if ( $this->bicycle_layer ) { 2181 | $output .= "\tthis.bicycle_layer = new google.maps.BicyclingLayer();\n\tthis.bicycle_layer.setMap(this.map);\n\n"; 2182 | } 2183 | 2184 | if ( $this->center_on_user ) { 2185 | if ( $this->geolocation_backup ) { 2186 | $output .= "\tif ( typeof geolocation != 'undefined' ) {\n"; 2187 | } 2188 | $output .= "\t\tthis.map.setCenter( geolocation );\n"; 2189 | if ( $this->geolocation_backup ) { 2190 | $output .= sprintf( "\t}\n\telse {\n\t\tthis.map.setCenter( new google.maps.LatLng(%s,%s) );\n\t}\n\n", $this->geolocation_backup->getLat(), $this->geolocation_backup->getLng() ); 2191 | } 2192 | } 2193 | if ( $this->center ) { 2194 | $output .= sprintf( "\tthis.map.setCenter( new google.maps.LatLng(%s,%s) );\n", $this->center->getLat(), $this->center->getLng() ); 2195 | } 2196 | 2197 | if ( count ($this->event_listeners ) ) { 2198 | $output .= "\tthis.event_listeners = [];\n"; 2199 | foreach( $this->event_listeners as $n => $event_listener ) { 2200 | $event_class = get_class( $event_listener->decoratee ); 2201 | $output .= sprintf( "\tthis.event_listeners[%s] = google.maps.event.add%sListener%s(%s, '%s', %s);\n", 2202 | $n, 2203 | $event_class == 'PHPGoogleMaps\Event\DomEventListener' ? 'Dom' : '', 2204 | $event_listener->once ? 'Once' : '', 2205 | $event_listener->object instanceof \PHPGoogleMaps\Core\MapObjectDecorator || $event_listener->object instanceof \PHPGoogleMaps\Map ? $event_listener->object : sprintf( 'document.getElementById("%s")', $event_listener->object ), 2206 | $event_listener->event, 2207 | $event_listener->function 2208 | ); 2209 | } 2210 | } 2211 | 2212 | if ( $this->streetview ) { 2213 | $streetview_options = ''; 2214 | if ( isset ( $this->streetview->options ) ) { 2215 | foreach( $this->streetview->options as $streetview_option => $streetview_value ) { 2216 | switch( $streetview_option ) { 2217 | case 'container': 2218 | break; 2219 | default: 2220 | $streetview_options .= sprintf( "\t\t%s:%s,\n", $streetview_option, $this->parseLatLngs( $this->phpToJs( $streetview_value ) ) ); 2221 | } 2222 | } 2223 | } 2224 | $output .= sprintf( "\tthis.streetview = new google.maps.StreetViewPanorama(document.getElementById(\"%s\"), {\n%s\t});\n\tthis.map.setStreetView(this.streetview);\n", $this->streetview->container, $streetview_options ); 2225 | 2226 | } 2227 | 2228 | 2229 | $output .= sprintf( "\n};\n\n}\nfunction initialize_%s() {\n\t%s = new phpgooglemap_%s();\n\t%s.initialize();\n\n%s\n\n}\n\n", $this->map_id, $this->map_id, $this->map_id, $this->map_id, $this->mobile_iphone_fullscreen ? 'setTimeout(function() { window.scrollTo(0, 1) }, 100);' : '' ); 2230 | 2231 | if ( $this->geolocation ) { 2232 | $output .= "function get_geolocation() {\n"; 2233 | $output .= sprintf( "\tnavigator.geolocation.getCurrentPosition( geolocation_success_init, geolocation_error_init, {enableHighAccuracy: %s, timeout: %s} );\n", ( $this->geolocation_high_accuracy ? 'true' : 'false' ), $this->geolocation_timeout); 2234 | $output .= "}\n"; 2235 | $output .= "function geolocation_success_init( position ) {\n"; 2236 | $output .= sprintf( "\tgeolocation_status=1;\n\tgeolocation_lat = position.coords.latitude;\n\tgeolocation_lng = position.coords.longitude;\n\tgeolocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);%s\n\tinitialize_%s();\n}\n", ( $this->geolocation_success_callback ? "\n\t" . $this->geolocation_success_callback . "();" : '' ),$this->map_id ); 2237 | $output .= sprintf( "function geolocation_error_init( error ){\n\tgeolocation_status=0;\n\tgeolocation_error = error.code;%s\n\tinitialize_%s();\n}\n", ( $this->geolocation_fail_callback ? "\n\t" . $this->geolocation_fail_callback . "();" : '' ), $this->map_id ); 2238 | $output .= "if ( navigator.geolocation ) {\n"; 2239 | $output .= "\tgoogle.maps.event.addDomListener(window, \"load\", get_geolocation );\n"; 2240 | $output .= "}\nelse {\n"; 2241 | $output .= sprintf( "\tgeolocation_status = 0;\n\tgeolocation_error = -1;\n\tgoogle.maps.event.addDomListener(window, \"load\", initialize_%s );\n}\n\n", $this->map_id, $this->map_id ); 2242 | } 2243 | else { 2244 | $output .= sprintf( "google.maps.event.addDomListener(window, \"load\", initialize_%s );\n\n", $this->map_id, $this->map_id ); 2245 | } 2246 | 2247 | if ( $this->compress_output ) { 2248 | $output = preg_replace( '~\n|\t~', '', $output ); 2249 | $output = preg_replace( '~\s*([:=\(\)\{\},])\s*~', "$1", $output ); 2250 | } 2251 | 2252 | $output = preg_replace( '~,(\s*[\}|\)])~', '$1', $output ); 2253 | 2254 | return sprintf("\n", $output ); 2255 | 2256 | } 2257 | 2258 | /************************************** 2259 | * 2260 | * Code output functions 2261 | * 2262 | ****************************************/ 2263 | 2264 | /** 2265 | * Convert PHP to JSON 2266 | * 2267 | * @param string $php PHP code 2268 | * @return string Returns JSON code 2269 | */ 2270 | private function phpToJs( $php ) { 2271 | if ( is_null( $php ) ) { 2272 | return '{}'; 2273 | } 2274 | return json_encode( $php ); 2275 | } 2276 | 2277 | /** 2278 | * Switch quotes from " to ' for output into javascript code 2279 | * 2280 | * @param $str String to switch quotes 2281 | * @return string Returns the string with the quotes switched 2282 | */ 2283 | private function switchquotes( $str ) { 2284 | return str_replace( '"', "'", $str ); 2285 | } 2286 | 2287 | /** 2288 | * Parse LatLngs in a string 2289 | * 2290 | * @param string $str String to replace 2291 | * @return string Returns the string with LatLngs replaced with 2292 | * google LatLng objects 2293 | * @access private 2294 | */ 2295 | private function parseLatLngs( $str ) { 2296 | return preg_replace( '~{"lat":(.*?),"lng":(.*?),.*?}~i', 'new google.maps.LatLng($1,$2)', $str ); 2297 | } 2298 | 2299 | /** 2300 | * Normalize a variable name 2301 | * Removes all non-word characters from a variable name 2302 | * 2303 | * @param string $var Variable to normalize 2304 | * @return string Returns the normalized variable 2305 | * @access private 2306 | */ 2307 | private function normalizeVariable( $var ) { 2308 | return preg_replace( '~\W~', '', $var ); 2309 | } 2310 | 2311 | /** 2312 | * Get the map's javascript variable 2313 | * 2314 | * @return string 2315 | */ 2316 | public function getJsVar() { 2317 | return sprintf( '%s.map', $this->map_id ); 2318 | } 2319 | 2320 | /* 2321 | * toString magic method 2322 | * 2323 | * @return string 2324 | */ 2325 | public function __toString() { 2326 | return $this->getJsVar(); 2327 | } 2328 | 2329 | /** 2330 | * Get the map's javascript variable 2331 | * 2332 | * @return string 2333 | */ 2334 | public function getMarkersJsVar() { 2335 | return sprintf( '%s.markers', $this->map_id ); 2336 | } 2337 | 2338 | /** 2339 | * Get info window's javascript variable 2340 | * 2341 | * @return string 2342 | */ 2343 | public function getInfoWindowJsVar() { 2344 | return $this->info_windows ? sprintf( '%s.info_window', $this->map_id ) : 'null'; 2345 | } 2346 | 2347 | /** 2348 | * Extract marker data 2349 | * This gets called to extract the icons and groups from the markers 2350 | * and organize them 2351 | * 2352 | * @return void 2353 | * @access private 2354 | */ 2355 | private function extractMarkerData() { 2356 | 2357 | $hash = md5( serialize( $this->getMarkers() ) ); 2358 | 2359 | if ( $hash == $this->marker_data_hash ) { 2360 | return true; 2361 | } 2362 | 2363 | foreach( $this->markers as $marker_id => $marker ) { 2364 | if ( $marker->icon instanceof \PHPGoogleMaps\Overlay\MarkerIcon ) { 2365 | if ( ( $icon_id = array_search( $marker->icon, $this->marker_icons ) ) !== false ) { 2366 | $marker->_icon_id = $icon_id; 2367 | } 2368 | else { 2369 | $this->marker_icons[] = $marker->icon; 2370 | $marker->_icon_id = count( $this->marker_icons ) - 1; 2371 | } 2372 | if ( $marker->shadow instanceof \PHPGoogleMaps\Overlay\MarkerIcon ) { 2373 | if ( ( $shadow_id = array_search( $marker->shadow, $this->marker_icons ) ) !== false ) { 2374 | $marker->_shadow_id = count( $this->marker_icons ) - 1; 2375 | } 2376 | else { 2377 | $this->marker_icons[] = $marker->shadow; 2378 | $marker->_shadow_id = count( $this->marker_icons ) - 1; 2379 | } 2380 | } 2381 | } 2382 | if ( $marker->shape instanceof \PHPGoogleMaps\Overlay\MarkerShape ) { 2383 | if ( ( $shape_id = array_search( $marker->shape, $this->marker_shapes ) ) !== false ) { 2384 | $marker->_shape_id = $shape_id; 2385 | } 2386 | else { 2387 | $this->marker_shapes[] = $marker->shape; 2388 | $marker->_shape_id = count( $this->marker_shapes ) - 1; 2389 | } 2390 | } 2391 | foreach ( $marker->groups as $marker_group ) { 2392 | if ( isset( $this->marker_groups[ $marker_group->var_name ] ) ) { 2393 | $this->marker_groups[ $marker_group->var_name ]['markers'][] = $marker_id; 2394 | } 2395 | else { 2396 | $this->marker_groups[ $marker_group->var_name ] = array( 2397 | 'id' => count( $this->marker_groups ), 2398 | 'name' => $marker_group->name, 2399 | 'markers' => array( $marker_id ) 2400 | ); 2401 | } 2402 | } 2403 | } 2404 | $this->marker_data_hash = md5( serialize( $this->getMarkers() ) ); 2405 | 2406 | } 2407 | 2408 | /** 2409 | * Enable Adsense ads 2410 | * 2411 | * Format and position must be valid or the map will not display 2412 | * @link http://code.google.com/apis/maps/documentation/javascript/advertising.html 2413 | * 2414 | * @param string $publisher_id Your adsense publisher id 2415 | * @param string $format A valid adsense ad format {@link http://code.google.com/apis/maps/documentation/javascript/advertising.html#AdUnitFormats} 2416 | * @param string $position A valid control position {@link http://code.google.com/apis/maps/documentation/javascript/reference.html#ControlPosition} 2417 | * @param boolean $visible 2418 | * @return object Returns a decorated object 2419 | */ 2420 | function enableAdsense( $publisher_id, $format = null, $position = null, $visible = null ) { 2421 | $this->libraries[] = 'adsense'; 2422 | $this->adsense = true; 2423 | $this->adsense_publisher_id = $publisher_id; 2424 | if ( $format ) $this->adsense_format = $format; 2425 | if ( $position ) $this->adsense_position = $position; 2426 | $this->adsense_visible = $visible; 2427 | } 2428 | 2429 | /** 2430 | * Enable clusering 2431 | * 2432 | * Enables marker clustering 2433 | * 2434 | * @link https://developers.google.com/maps/articles/toomanymarkers 2435 | * 2436 | * Verified to work with marker clusterer 2437 | * @link http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/ 2438 | * 2439 | * @param string $clustering_js_file Location to the clustering file 2440 | * @param array $options Clustering options 2441 | */ 2442 | function enableClustering( $clustering_js_file, array $options = null ) { 2443 | $this->clustering_js = $clustering_js_file; 2444 | $this->clustering_options = $options; 2445 | } 2446 | 2447 | /** 2448 | * Enable Places 2449 | * 2450 | * Add an input on the page 2451 | * 2452 | * autocomplete_input_id is a required key if you want to use an autocomplete 2453 | * 2454 | * @param array $options array of places options 2455 | */ 2456 | function enablePlacesAutocomplete( array $options = array() ) { 2457 | $this->libraries[] = 'places'; 2458 | $this->places = true; 2459 | if ( isset( $options["autocomplete_input_id"] ) ) { 2460 | $this->autocomplete_input_id = $options["autocomplete_input_id"]; 2461 | unset( $options["autocomplete_input_id"] ); 2462 | $this->autocomplete_options = $options; 2463 | } 2464 | } 2465 | 2466 | } 2467 | -------------------------------------------------------------------------------- /Overlay/Circle.php: -------------------------------------------------------------------------------- 1 | center = $center->getLatLng(); 40 | } 41 | else { 42 | $geocode_result = \PHPGoogleMaps\Service\Geocoder::geocode( $center, true ); 43 | if ( $geocode_result instanceof \PHPGoogleMaps\Core\PositionAbstract ) { 44 | $this->center = $geocode_result->getLatLng(); 45 | } 46 | else { 47 | throw new \PHPGoogleMaps\Core\GeocodeException( $geocode_result ); 48 | } 49 | } 50 | $this->radius = (float) $radius; 51 | if ( $options ) { 52 | unset( $options['map'], $options['center'], $options['radius'] ); 53 | $this->options = $options; 54 | } 55 | } 56 | 57 | public static function createFromLatLng( $center, $radius, array $options=null ) { 58 | return new Circle( $center, $radius, $options ); 59 | } 60 | 61 | public static function createFromLocation( $location, $radius, array $options=null ) { 62 | $geocode_result = \PHPGoogleMaps\Service\Geocoder::geocode( $location ); 63 | if ( $geocode_result instanceof \PHPGoogleMaps\Core\PositionAbstract ) { 64 | return new Circle( $geocode_result->getLatLng(), $radius, $options ); 65 | } 66 | return false; 67 | } 68 | 69 | } -------------------------------------------------------------------------------- /Overlay/GroundOverlay.php: -------------------------------------------------------------------------------- 1 | url = $url; 45 | $this->southwest = $southwest; 46 | $this->northeast = $northeast; 47 | if ( $options ) { 48 | unset( $options['map'] ); 49 | $this->options = $options; 50 | } 51 | } 52 | 53 | } -------------------------------------------------------------------------------- /Overlay/GroundOverlayDecorator.php: -------------------------------------------------------------------------------- 1 | $id, '_map' => $map ) ); 35 | } 36 | 37 | /** 38 | * Returns the javascript variable of the ground overlay 39 | * 40 | * @return string 41 | */ 42 | public function getJsVar() { 43 | return sprintf( '%s.ground_overlays[%s]', $this->_map, $this->_id ); 44 | } 45 | 46 | } -------------------------------------------------------------------------------- /Overlay/MapStyle.php: -------------------------------------------------------------------------------- 1 | addObjects( array( $style ) ); 21 | * 22 | * 4. Explicity set the map types of the map and include your new MapStyle object 23 | * $map->setMapTypes( array( 'roadmap', 'terrain', $green_water_style ) ); 24 | * 25 | * @link http://code.google.com/apis/maps/documentation/javascript/maptypes.html#StyledMaps 26 | * @link http://gmaps-samples-v3.googlecode.com/svn/trunk/styledmaps/wizard/index.html 27 | */ 28 | 29 | class MapStyle extends \PHPGoogleMaps\Core\MapObject { 30 | 31 | /** 32 | * Map style name 33 | * This is the name that will show up on the map 34 | * 35 | * @var string 36 | */ 37 | protected $name; 38 | 39 | /** 40 | * Map style variable name 41 | * This is used internally 42 | * 43 | * @var string 44 | */ 45 | protected $var_name; 46 | 47 | /** 48 | * Map style 49 | * This is the JSON code 50 | * 51 | * @var string 52 | */ 53 | protected $style; 54 | 55 | /** 56 | * Constructor 57 | * 58 | * @param string $name The name of the map style 59 | * @param $style JSON code of the style 60 | * @return MapStyle 61 | */ 62 | public function __construct( $name, $style ) { 63 | $this->name = $name; 64 | $this->var_name = preg_replace( '~\W~', '', $name ); 65 | $this->style = $style; 66 | } 67 | 68 | } -------------------------------------------------------------------------------- /Overlay/Marker.php: -------------------------------------------------------------------------------- 1 | position = $position; 83 | if ( !$options ) { 84 | return; 85 | } 86 | foreach( $options as $option_name => $option ) { 87 | switch( $option_name ) { 88 | case 'group': 89 | $this->addToGroup( $option ); 90 | break; 91 | case 'groups': 92 | $this->addToGroups( $option ); 93 | break; 94 | case 'icon': 95 | $this->setIcon( $option ); 96 | break; 97 | case 'static': 98 | $this->static = (object)$option; 99 | break; 100 | case 'shadow': 101 | $this->setShadow( $option ); 102 | break; 103 | case 'shape': 104 | $this->setShape( $option ); 105 | case 'geolocation': 106 | if ( $position === null && $option ) { 107 | $timeout = isset( $options['geolocation_timeout'] ) ? $options['geolocation_timeout'] : null; 108 | $high_accuracy = isset( $options['geolocation_high_accuracy'] ) ? $options['geolocation_high_accuracy'] : null; 109 | $this->enableGeolocation( $timeout, $high_accuracy ); 110 | } 111 | break; 112 | default: 113 | $this->options[$option_name] = $option; 114 | } 115 | } 116 | } 117 | 118 | /** 119 | * Add marker to a group 120 | * 121 | * @param string|MarkerGroup $group Can be a Markergroup or a string 122 | * @return Marker 123 | */ 124 | function addToGroup( $group ) { 125 | if ( $group instanceof MarkerGroup ) { 126 | $this->groups[] = $group; 127 | } 128 | else { 129 | $this->groups[] = new MarkerGroup( $group ); 130 | } 131 | return $this; 132 | } 133 | 134 | /** 135 | * Add marker to an array of groups 136 | * 137 | * @param array $groups 138 | * @return Marker 139 | */ 140 | function addToGroups( array $groups ) { 141 | foreach( $groups as $group ) { 142 | $this->addToGroup( $group ); 143 | } 144 | return $this; 145 | } 146 | 147 | /** 148 | * Sets the marker's icon and optionally shadow 149 | * 150 | * @param MarkerIcon $icon The marker's icon. 151 | * @return Marker 152 | */ 153 | public function setIcon( $icon ) { 154 | if ( !$icon instanceof MarkerIcon ) { 155 | $icon = new MarkerIcon( $icon ); 156 | } 157 | $this->icon = $icon; 158 | return $this; 159 | } 160 | 161 | /** 162 | * Get the url of the marker's icon 163 | * 164 | * @return string 165 | */ 166 | public function getIcon() { 167 | if ( isset( $this->icon->icon ) ) { 168 | return $this->icon->icon; 169 | } 170 | return null; 171 | } 172 | 173 | /** 174 | * Get the url of the marker's shadow 175 | * 176 | * @return string 177 | */ 178 | public function getShadow() { 179 | if ( isset( $this->shadow->icon ) ) { 180 | return $this->shadow->icon; 181 | } 182 | return null; 183 | } 184 | 185 | /** 186 | * Sets the marker's shadow 187 | * 188 | * @param MarkerIcon $shadow The marker's shadow. 189 | * @return Marker 190 | */ 191 | public function setShadow( $shadow ) { 192 | if ( !$shadow instanceof MarkerIcon ) { 193 | $shadow = new MarkerIcon( $shadow ); 194 | } 195 | $this->shadow = $shadow; 196 | return $this; 197 | } 198 | 199 | /** 200 | * Sets the marker's shape 201 | * 202 | * @param MarkerShape $shape The marker's shape. 203 | * @return MarkerShape 204 | */ 205 | public function setShape( MarkerShape $shape ) { 206 | $this->shape = $shape; 207 | return $this; 208 | } 209 | 210 | /** 211 | * Enable marker geolocation 212 | * 213 | * Use `createMarkerFromUserLocation()` 214 | * 215 | * @access private 216 | * @return MarkerIcon 217 | */ 218 | private function enableGeolocation() { 219 | $this->geolocation = true; 220 | return $this; 221 | } 222 | 223 | /** 224 | * Return the marker's geolocation flag 225 | * 226 | * @return boolean 227 | */ 228 | public function isGeolocated() { 229 | return $this->geolocation; 230 | } 231 | 232 | /** 233 | * Factory method to create a marker from a position (LatLng or GeocodeResult) 234 | * 235 | * @param AbstractPosition $position Position of the marker 236 | * @param array $options Array of marker options 237 | * @return Marker 238 | */ 239 | public static function createFromPosition( \PHPGoogleMaps\Core\PositionAbstract $position, array $options=null ) { 240 | return new Marker( $position->getLatLng(), $options ); 241 | } 242 | 243 | /** 244 | * Factory method to create a marker from a location ( e.g. New York, NY ) 245 | * 246 | * @param string $location Location of the marker. This will be geocoded for you. 247 | * @param array $options Array of marker options. 248 | * @return Marker|False Will return false if geocoding fails. 249 | * @throws GeocodeException 250 | */ 251 | public static function createFromLocation( $location, array $options=null ) { 252 | $geocode_result = \PHPGoogleMaps\Service\Geocoder::geocode( $location ); 253 | if ( $geocode_result instanceof \PHPGoogleMaps\Service\GeocodeResult ) { 254 | return self::createFromPosition( $geocode_result, $options ); 255 | } 256 | else { 257 | throw new \PHPGoogleMaps\Service\GeocodeException( $geocode_result ); 258 | } 259 | } 260 | 261 | /** 262 | * Factory method to create a marker from the user's location 263 | * This uses HTML5's geolocation API 264 | * 265 | * @param array options Array of marker options 266 | * @return Marker 267 | */ 268 | public static function createFromUserLocation( array $options=null ){ 269 | $marker = new Marker( null, $options ); 270 | $marker->enableGeolocation(); 271 | return $marker; 272 | } 273 | 274 | /** 275 | * Return the markers position 276 | * 277 | * @return LatLng|null 278 | */ 279 | function getPosition() { 280 | return $this->position; 281 | } 282 | 283 | } 284 | 285 | -------------------------------------------------------------------------------- /Overlay/MarkerDecorator.php: -------------------------------------------------------------------------------- 1 | $id, '_map' => $map ) ); 59 | } 60 | 61 | /** 62 | * Returns the javascript variable of the marker 63 | * 64 | * @return string 65 | */ 66 | public function getJsVar() { 67 | return sprintf( '%s.markers[%s]', $this->_map, $this->_id ); 68 | } 69 | 70 | /** 71 | * Get marker opener 72 | * Returns the code to open the marker 73 | * 74 | * @returns string 75 | */ 76 | public function getOpener() { 77 | return sprintf( 'google.maps.event.trigger(map.markers[%s], \'click\');', $this->_id ); 78 | } 79 | } -------------------------------------------------------------------------------- /Overlay/MarkerGroup.php: -------------------------------------------------------------------------------- 1 | addToGroup( $group ); 15 | * 16 | * 2. Pass a group name to Marker::addToGroup() 17 | * $marker->addToGroup( 'group' ); 18 | * 19 | * 3. Pass a marker to MarkerGroup::addMarker() 20 | * $group->addMarker( $marker ); 21 | * 22 | * 4. Pass an array of markers to MarkerGroup::addMarkers() 23 | * $group->addMarkers( $markers ); 24 | * 25 | * Note: Be careful when naming groups. The group name you pass is turned into a 26 | * variable name by removing all non-word characters. This means there will be 27 | * issues if you have a group named 'group 1' and 'group1' since both of their 28 | * variables will be group1. 29 | */ 30 | 31 | class MarkerGroup extends \PHPGoogleMaps\Core\MapObject { 32 | 33 | /** 34 | * Group name 35 | * 36 | * @var string 37 | */ 38 | protected $name; 39 | 40 | /** 41 | * Group variable name 42 | * This is the group name with all non-word characters removed 43 | * 44 | * @var string 45 | */ 46 | protected $var_name; 47 | 48 | /** 49 | * Constructor 50 | * 51 | * @var string $group_name Name of the group 52 | * @return MarkerGroup 53 | */ 54 | public function __construct( $group_name ) { 55 | $this->name = $group_name; 56 | $this->var_name = preg_replace( '~\W~', '', $group_name ); 57 | } 58 | 59 | /** 60 | * Static create function 61 | * Used for method chaining 62 | * 63 | * @param string $group_name Name of the group to create 64 | * @return MarkerGroup 65 | */ 66 | public static function create( $group_name ) { 67 | return new MarkerGroup( $group_name ); 68 | } 69 | 70 | /** 71 | * Add a marker to the group 72 | * 73 | * @param Marker $marker Marker to add to the group 74 | * @return MarkerGroup 75 | */ 76 | public function addMarker( \PHPGoogleMaps\Overlay\Marker $marker ) { 77 | $marker->addToGroup( $this ); 78 | return $this; 79 | } 80 | 81 | /** 82 | * Add an array of markers to the group 83 | * 84 | * @param array $markers Array of markers to add to the group 85 | * @return MarkerGroup 86 | */ 87 | public function addMarkers( array $markers ) { 88 | foreach( $markers as $marker ) { 89 | $this->addMarker( $marker ); 90 | } 91 | return $this; 92 | } 93 | 94 | } -------------------------------------------------------------------------------- /Overlay/MarkerGroupDecorator.php: -------------------------------------------------------------------------------- 1 | $id, '_map' => $map ) ); 47 | } 48 | 49 | /** 50 | * Returns the javascript variable of the marker group 51 | * 52 | * @return string 53 | */ 54 | public function getJsVar() { 55 | return sprintf( '%s.marker_groups[%s]', $this->_map, $this->_id ); 56 | } 57 | 58 | /** 59 | * Call a function on all of a group's markers 60 | * 61 | * @param string $function_all_markers Javascript function to call on all map markers 62 | * This can be used to reset all markers to a default setting 63 | * 64 | * @param string $function_group_markers Javascript function to call on all group markers 65 | * This is called on the group markers after all markers have been "reset" 66 | * 67 | * @return string 68 | */ 69 | public function callFunction( $function_all_markers, $function_group_markers ) { 70 | return sprintf( "%s.marker_group_function('%s', %s, %s)", $this->_map, $this->var_name, $function_all_markers, $function_group_markers ); 71 | } 72 | 73 | } -------------------------------------------------------------------------------- /Overlay/MarkerIcon.php: -------------------------------------------------------------------------------- 1 | setSize( 30, 30 ); 12 | * $shadow = \PHPGoogleMaps\Marker\MarkerIcon::create( $shadow_url )->setAnchor( 0, 30 ); 13 | * $marker->setIcon( $icon ); 14 | * $marker->setShadow( $shadow ); 15 | */ 16 | 17 | 18 | class MarkerIcon extends \PHPGoogleMaps\Core\MapObject { 19 | 20 | /** 21 | * Url of the icon image 22 | * 23 | * @var string 24 | */ 25 | protected $icon; 26 | 27 | /** 28 | * Width of the icon 29 | * 30 | * @var integer 31 | */ 32 | protected $width; 33 | 34 | /** 35 | * Height of the icon 36 | * 37 | * @var integer 38 | */ 39 | protected $height; 40 | 41 | /** 42 | * X coord of the anchor 43 | * 44 | * @var integer 45 | */ 46 | protected $anchor_x; 47 | 48 | /** 49 | * Y coord of the anchor 50 | * 51 | * @var integer 52 | */ 53 | protected $anchor_y; 54 | 55 | /** 56 | * X coord of the origin 57 | * 58 | * @var integer 59 | */ 60 | protected $origin_x; 61 | 62 | /** 63 | * Y coord of the origin 64 | * 65 | * @var integer 66 | */ 67 | protected $origin_y; 68 | 69 | /** 70 | * Constructor 71 | * Sets the icon's image URL, width, height, origin and anchor 72 | * default origin ( 0, 0 ) 73 | * default anchor ( w/2, h ) 74 | * 75 | * Throws an exception if the icon is invalid 76 | * 77 | * If allow_url_fopen is not enabled on your server you will need to explicity specify the dimensions of the icon 78 | * 79 | * @param strin $icon Absolute URL of the icon image 80 | * @throws Exception 81 | * @return MarkerIcon 82 | */ 83 | public function __construct( $icon, array $options = null ) { 84 | 85 | $this->icon = $icon; 86 | 87 | if ( !isset( $options['width'] ) || !isset( $options['height'] ) ) { 88 | $size = @getimagesize( $icon ); 89 | list( $this->width, $this->height ) = $size; 90 | } 91 | 92 | if ( isset( $options['width'] ) ) { 93 | $this->width = (int) $options['width']; 94 | } 95 | if ( isset( $options['height'] ) ) { 96 | $this->height = (int) $options['height']; 97 | } 98 | 99 | $this->anchor_x = isset( $options['anchor_x'] ) ? $options['anchor_x'] : floor( $this->width / 2 ); 100 | $this->anchor_y = isset( $options['anchor_y'] ) ? $options['anchor_y'] : $this->height; 101 | $this->origin_x = isset( $options['origin_x'] ) ? $options['origin_x'] : 0; 102 | $this->origin_y = isset( $options['origin_y'] ) ? $options['origin_y'] : 0; 103 | 104 | return $this; 105 | 106 | } 107 | 108 | /** 109 | * Static create method useful for method chaining 110 | * 111 | * @param strin $icon URL of the icon image 112 | * @throws Exception 113 | * @return MarkerIcon 114 | */ 115 | public static function create( $icon, array $options=null ) { 116 | return new MarkerIcon( $icon, $options ); 117 | } 118 | 119 | /** 120 | * Set the icon's height 121 | * 122 | * @param int $height 123 | * @return MarkerIcon 124 | */ 125 | public function setHeight( $height ) { 126 | $this->height = (int) $height; 127 | return $this; 128 | } 129 | 130 | /** 131 | * Set the icon's width 132 | * 133 | * @param int $width 134 | * @return MarkerIcon 135 | */ 136 | public function setWidth( $width ) { 137 | $this->width = (int) $width; 138 | return $this; 139 | } 140 | 141 | /** 142 | * Set the icon's size (width/height) 143 | * 144 | * @param int $width 145 | * @param int $height 146 | * @return MarkerIcon 147 | */ 148 | public function setSize( $width, $height ) { 149 | $this->height = (int) $height; 150 | $this->width = (int) $width; 151 | return $this; 152 | } 153 | 154 | /** 155 | * Set the icon's anchor point 156 | * This is the point on the icon that will be placed on the map 157 | * 158 | * @param int $x X coord of the anchor 159 | * @param int $y Y coord of the anchor 160 | * @return MarkerIcon 161 | */ 162 | public function setAnchor( $x=null, $y=null ) { 163 | if ( $x !== null ) { 164 | $this->anchor_x = (int) $x; 165 | } 166 | if ( $y !== null ) { 167 | $this->anchor_y = (int) $y; 168 | } 169 | return $this; 170 | } 171 | 172 | 173 | /** 174 | * Set the icon's origin point 175 | * This is used when you are using a sprite for the marker 176 | * 177 | * @param int $x X coord of the origin 178 | * @param int $y Y coord of the origin 179 | * @return MarkerIcon 180 | */ 181 | public function setOrigin( $x=null, $y=null ) { 182 | if ( $x !== null ) { 183 | $this->origin_x = (int) $x; 184 | } 185 | if ( $y !== null ) { 186 | $this->origin_y = (int) $y; 187 | } 188 | return $this; 189 | } 190 | 191 | /** 192 | * Return string 193 | */ 194 | public function __toString() { 195 | return $this->icon; 196 | } 197 | 198 | } -------------------------------------------------------------------------------- /Overlay/MarkerShape.php: -------------------------------------------------------------------------------- 1 | setShape( $shape ); 11 | * 12 | * @link http://code.google.com/apis/maps/documentation/javascript/reference.html#MarkerShape 13 | */ 14 | 15 | class MarkerShape extends \PHPGoogleMaps\Core\MapObject { 16 | 17 | /** 18 | * Type of Shape 19 | * Rect, Poly or Circle 20 | * 21 | * @var string 22 | */ 23 | protected $type; 24 | 25 | /** 26 | * Coordinates 27 | * Vary depending on the shape 28 | * 29 | * @var array 30 | */ 31 | protected $coords = array(); 32 | 33 | /** 34 | * Array of variable names that can't be set without using a setter function 35 | * 36 | * @var array 37 | */ 38 | protected $protected_data = array( 'type', 'coords' ); 39 | 40 | /** 41 | * Private constructor 42 | * 43 | * @param string $type Type of shape ( circle, rect, poly ) 44 | * @param array $coords Array of coordinates associated with the shape 45 | * @access private 46 | * @return MarkerShape 47 | */ 48 | private function __construct( $type, array $coords ) { 49 | $this->type = $type; 50 | $this->coords = $coords; 51 | } 52 | 53 | /** 54 | * Create a circle shape 55 | * 56 | * @param $center_x X coord of the center of the circle 57 | * @param center_y Y coord of the center of the circle 58 | * @param $radius Radius of the circle 59 | * @return MarkerShape 60 | */ 61 | public static function createCircle( $center_x, $center_y, $radius ) { 62 | return new MarkerShape( 'circle', array( $center_x, $center_y, $radius ) ); 63 | } 64 | 65 | /** 66 | * Create a poly shape 67 | * 68 | * @param $coords Coordinates of the poly points 69 | * @return MarkerShape 70 | */ 71 | public static function createPoly( array $coords ) { 72 | return new MarkerShape( 'poly', $coords ); 73 | } 74 | 75 | /** 76 | * Create a circle shape 77 | * 78 | * @param $topleft_x X coord of the top left of the circle 79 | * @param $topleft_y X coord of the top left of the circle 80 | * @param $bottomright_x X coord of the bottom right of the circle 81 | * @param $bottomright_y Y coord of the bottom right of the circle 82 | * @return MarkerShape 83 | */ 84 | public static function createRect( $topleft_x, $topleft_y, $bottomright_x, $bottomright_y ) { 85 | return new MarkerShape( 'rect', array( $topleft_x, $topleft_y, $bottomright_x, $bottomright_y ) ); 86 | } 87 | 88 | } -------------------------------------------------------------------------------- /Overlay/Poly.php: -------------------------------------------------------------------------------- 1 | options = $options; 31 | } 32 | foreach( $paths as $path ) { 33 | $this->addPath( $path ); 34 | } 35 | } 36 | 37 | /** 38 | * Add a path 39 | * Adds a path to the end of the array of paths 40 | * 41 | * @throws GeocodeException 42 | * @param string|LatLng $location Location to add. This can be a location name 43 | * or a LatLng object. 44 | * @return void 45 | */ 46 | public function addPath( $location ) { 47 | if ( $location instanceof \PHPGoogleMaps\Core\PositionAbstract ) { 48 | $this->paths[] = $location->getLatLng(); 49 | } 50 | else { 51 | $geocode_result = \PHPGoogleMaps\Service\Geocoder::geocode( $location, true ); 52 | if ( $geocode_result instanceof \PHPGoogleMaps\Core\PositionAbstract ) { 53 | $this->paths[] = $geocode_result->getLatLng(); 54 | } 55 | else { 56 | throw new \PHPGoogleMaps\Service\GeocodeException( $geocode_result ); 57 | } 58 | } 59 | } 60 | 61 | /** 62 | * Get polygon paths 63 | * 64 | * @return array 65 | */ 66 | public function getPaths() { 67 | return $this->paths; 68 | } 69 | 70 | /** 71 | * Get a path 72 | * 73 | * @param integer $path Path to get 74 | * @return LatLng 75 | */ 76 | public function getPath( $path ) { 77 | $path = (int) $path; 78 | return $this->paths[$path]; 79 | } 80 | 81 | } -------------------------------------------------------------------------------- /Overlay/PolyDecorator.php: -------------------------------------------------------------------------------- 1 | $id, '_map' => $map ) ); 34 | } 35 | 36 | /** 37 | * Returns the javascript variable of the poly 38 | * 39 | * @return string 40 | */ 41 | public function getJsVar() { 42 | return sprintf( '%s.polys[%s]', $this->_map, $this->_id ); 43 | } 44 | 45 | } -------------------------------------------------------------------------------- /Overlay/Polygon.php: -------------------------------------------------------------------------------- 1 | paths as $path ) { 22 | $lat_sum += $path->lat; 23 | $lng_sum += $path->lng; 24 | } 25 | $lat_avg = $lat_sum / count( $this->paths ); 26 | $lng_avg = $lng_sum / count( $this->paths ); 27 | return new \PHPGoogleMaps\Core\LatLng( $lat_avg, $lng_avg ); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /Overlay/Polyline.php: -------------------------------------------------------------------------------- 1 | southwest = $southwest->getLatLng(); 39 | } 40 | else { 41 | $geocode_result = \PHPGoogleMaps\Service\Geocoder::geocode( $southwest, true ); 42 | if ( $geocode_result instanceof \PHPGoogleMaps\Core\PositionAbstract ) { 43 | $this->southwest = $geocode_result; 44 | } 45 | else { 46 | throw new \PHPGoogleMaps\Core\GeocodeException( $geocode_result ); 47 | } 48 | } 49 | if ( $northeast instanceof \PHPGoogleMaps\Core\PositionAbstract ) { 50 | $this->northeast = $northeast->getLatLng(); 51 | } 52 | else { 53 | $geocode_result = \PHPGoogleMaps\Service\Geocoder::geocode( $northeast, true ); 54 | if ( $geocode_result instanceof \PHPGoogleMaps\Core\PositionAbstract ) { 55 | $this->northeast = $geocode_result; 56 | } 57 | else { 58 | throw new \PHPGoogleMaps\Service\GeocodeException( $geocode_result ); 59 | } 60 | } 61 | 62 | if ( $options ) { 63 | unset( $options['map'], $options['bounds'] ); 64 | $this->options = $options; 65 | } 66 | } 67 | 68 | } -------------------------------------------------------------------------------- /Overlay/Shape.php: -------------------------------------------------------------------------------- 1 | $id, '_map' => $map ) ); 34 | } 35 | 36 | /** 37 | * Returns the javascript variable of the shape 38 | * 39 | * @return string 40 | */ 41 | public function getJsVar() { 42 | return sprintf( '%s.shapes[%s]', $this->_map, $this->_id ); 43 | } 44 | 45 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #PHP Google Maps API 2 | 3 | For PHP 5.3+ and Google Maps API v3 4 | 5 | ##Features 6 | - Adsense ads 7 | - Binding map objects 8 | - Custom map controls 9 | - Directions with waypoints ( walking, biking and driving ) 10 | - Event listeners and DOM event listeners 11 | - Fusion tables 12 | - Geocoding 13 | - Geolocation 14 | - Ground overlays 15 | - KML layers 16 | - Custom map styles 17 | - Markers 18 | - Marker clustering 19 | - Custom marker icons 20 | - Marker staggering 21 | - Marker animation (bounce, drop) 22 | - Mobile display 23 | - Panoramio layers 24 | - Polygons and polylines 25 | - Shapes (rectangles and circles) 26 | - Sidebar 27 | - Static Map 28 | - Streetview 29 | - Simple configuration of map objects 30 | 31 | ##Autoloading 32 | 33 | Use the included autoloader 34 | 35 | require( '../PHPGoogleMaps/Core/Autoloader.php' ); 36 | $map_loader = new SplClassLoader('PHPGoogleMaps', '../'); 37 | $map_loader->register(); 38 | 39 | This is the autoload from the [PSR-0 Final Proposal](http://groups.google.com/group/php-standards/web/psr-0-final-proposal). 40 | -------------------------------------------------------------------------------- /Service/BicyclingDirections.php: -------------------------------------------------------------------------------- 1 | was_in_cache = (bool)$was_in_cache; 52 | if ( isset( $was_put_in_cache ) ) { 53 | $this->was_put_in_cache = (bool)$was_put_in_cache; 54 | } 55 | $this->latLng = $latlng; 56 | $this->location = $latlng->location; 57 | } 58 | 59 | /** 60 | * Get cached flag 61 | * Returns whether the location's geocode was retrieved from the cache 62 | * 63 | * @return boolean 64 | */ 65 | public function wasInCache() { 66 | return $this->was_in_cache; 67 | } 68 | 69 | /** 70 | * Get cached flag 71 | * Returns whether the location's geocode was written to the cash 72 | * 73 | * @return boolean 74 | */ 75 | public function wasPutInCache() { 76 | return $this->was_put_in_cache; 77 | } 78 | 79 | /** 80 | * Get LatLng object 81 | * 82 | * @return LatLng 83 | */ 84 | public function getLatLng() { 85 | return $this->latLng; 86 | } 87 | 88 | /** 89 | * Get lat 90 | * 91 | * @return float 92 | */ 93 | public function getLat() { 94 | return $this->latLng->getLat(); 95 | } 96 | 97 | /** 98 | * Get lng 99 | * 100 | * @return float 101 | */ 102 | public function getLng() { 103 | return $this->latLng->getLng(); 104 | } 105 | } -------------------------------------------------------------------------------- /Service/CachingGeocoder.php: -------------------------------------------------------------------------------- 1 | geocode_cache = $gc; 29 | } 30 | 31 | /** 32 | * Write to cache 33 | * 34 | * @param string $location Location to cache 35 | * @param float $lat Latitude of location 36 | * @param float $lng Longitude of location 37 | * 38 | * @return boolean 39 | */ 40 | private function writeCache( $location, $lat, $lng ) { 41 | return $this->geocode_cache->writeCache( strtolower( $location ), $lat, $lng ); 42 | } 43 | 44 | /** 45 | * Get from cache 46 | * 47 | * @param string $location Location to get from cache 48 | * @return false|LatLng 49 | */ 50 | private function getCache( $location ) { 51 | return $this->geocode_cache->getCache( strtolower( $location ) ); 52 | } 53 | 54 | /** 55 | * Geocodes a location 56 | * 57 | * This will return a `LatLng` object if the location is successfully geocoded 58 | * The object will contain a `latitude`, a `longitude` 59 | * 60 | * If an error occurred a `GeocodeError` object will be returned with a `status` property 61 | * containing the error status returned from google 62 | * 63 | * @link http://code.google.com/apis/maps/documentation/geocoding/ 64 | * 65 | * @param string $location 66 | * @return LatLng|GeocodeError 67 | */ 68 | public function geocode( $location ) { 69 | if ( $get_cache = $this->getCache( $location ) ) { 70 | return $get_cache; 71 | } 72 | else { 73 | $geocode_result = \PHPGoogleMaps\Service\Geocoder::geocode( $location ); 74 | if ( $geocode_result instanceof \PHPGoogleMaps\Service\GeocodeResult ) { 75 | if ( $write_cache = $this->writeCache( $location, $geocode_result->getLat(), $geocode_result->getLng() ) ) { 76 | $geocode_result = new \PHPGoogleMaps\Service\CachedGeocodeResult( $geocode_result->getLatLng(), false, true ); 77 | } 78 | } 79 | return $geocode_result; 80 | } 81 | } 82 | 83 | } -------------------------------------------------------------------------------- /Service/Directions.php: -------------------------------------------------------------------------------- 1 | array( array( 'location' => \PHPGoogleMaps\Service\Geocoder::geocode( 'Phoenix, AZ' ) ) ) ); 11 | * $renderer = array( 'draggable' => true ); 12 | * $dir = new \PHPGoogleMaps\Service\DrivingDirections( 'New York, NY', 'San Jose, CA', $renderer, $request ); 13 | */ 14 | 15 | abstract class Directions extends \PHPGoogleMaps\Core\MapObject { 16 | 17 | /** 18 | * Directions request options 19 | * Array of directions request options 20 | * @link http://code.google.com/apis/maps/documentation/javascript/reference.html#DirectionsRequest 21 | * 22 | * @var array 23 | */ 24 | protected $request_options = array(); 25 | 26 | /** 27 | * Directions renderer options 28 | * Array of directions renderer options 29 | * @link http://code.google.com/apis/maps/documentation/javascript/reference.html#DirectionsRendererOptions 30 | * 31 | * @var array 32 | */ 33 | protected $renderer_options = array(); 34 | 35 | /** 36 | * Constructor 37 | * 38 | * @throws GeocodeException 39 | * @param string|LatLng $origin Origin. Can be a LatLng object or a string location e.g. San Jose, CA 40 | * @param string|LatLng $destination Destination. Can be a LatLng object or a string location e.g. San Jose, CA 41 | * @param array $renderer_options Array of renderer options corresponding to one of these: 42 | * {@link http://code.google.com/apis/maps/documentation/javascript/reference.html#DirectionsRendererOptions} 43 | * @param array $request_options Array of request options corresponding to one of these: 44 | * {@link http://code.google.com/apis/maps/documentation/javascript/reference.html#DirectionsRendererOptions} 45 | * @return Directions 46 | */ 47 | public function __construct( $origin, $destination, array $renderer_options=null, array $request_options=null ) { 48 | 49 | unset( $renderer_options['directions'], $renderer_options['map'] ); 50 | unset( $request_options['origin'], $request_options['destination'], $request_options['travelMode'] ); 51 | 52 | if ( $renderer_options ) { 53 | $this->renderer_options = $renderer_options ; 54 | } 55 | if ( $request_options ) { 56 | unset( $request_options['waypoints'] ); 57 | $this->request_options = $request_options; 58 | } 59 | 60 | $this->request_options['travelMode'] = $this->travel_mode; 61 | 62 | if ( $origin instanceof \PHPGoogleMaps\Core\PositionAbstract ) { 63 | $this->request_options['origin'] = $origin->getLatLng(); 64 | } 65 | else { 66 | if ( ( $geocode_result = \PHPGoogleMaps\Service\Geocoder::geocode( $origin ) ) instanceof \PHPGoogleMaps\Core\PositionAbstract ) { 67 | $this->request_options['origin'] = $geocode_result->getLatLng(); 68 | } 69 | else { 70 | throw new \PHPGoogleMaps\Service\GeocodeException( $geocode_result ); 71 | } 72 | } 73 | 74 | if ( $destination instanceof \PHPGoogleMaps\Core\PositionAbstract ) { 75 | $this->request_options['destination'] = $destination->getLatLng(); 76 | } 77 | else { 78 | if ( ( $geocode_result = \PHPGoogleMaps\Service\Geocoder::geocode( $destination ) ) instanceof \PHPGoogleMaps\Core\PositionAbstract ) { 79 | $this->request_options['destination'] = $geocode_result; 80 | } 81 | else { 82 | throw new \PHPGoogleMaps\Service\GeocodeException( $geocode_result ); 83 | } 84 | } 85 | 86 | } 87 | 88 | /** 89 | * Add a waypoint 90 | * 91 | * @param string|PositionAbstract $waypoint The waypoint 92 | * @param boolean $stopover 93 | */ 94 | public function addWaypoint( $waypoint, $stopover=true ) { 95 | if ( $waypoint instanceof \PHPGoogleMaps\Core\PositionAbstract ) { 96 | $this->request_options['waypoints'][] = array( 'location' => $waypoint->getLatLng() ); 97 | } 98 | else { 99 | if ( ( $geocode_result = \PHPGoogleMaps\Service\Geocoder::geocode( $waypoint, true ) ) instanceof \PHPGoogleMaps\Core\PositionAbstract ) { 100 | $this->request_options['waypoints'][] = array( 'location' => $geocode_result->getLatLng() ); 101 | } 102 | else { 103 | throw new \PHPGoogleMaps\Service\GeocodeException( $geocode_result ); 104 | } 105 | } 106 | } 107 | 108 | } -------------------------------------------------------------------------------- /Service/DirectionsDecorator.php: -------------------------------------------------------------------------------- 1 | addObjects( array( $dir ) ); 13 | * Set Panel 14 | */ 15 | 16 | class DirectionsDecorator extends \PHPGoogleMaps\Core\MapObjectDecorator { 17 | 18 | /** 19 | * Map id the fusion table is attached to 20 | * 21 | * @var string 22 | */ 23 | protected $_map; 24 | 25 | /** 26 | * Constructor 27 | * 28 | * @param Directions $dir Directions to decorate 29 | * @param string $map Map ID of the map the directions are attached to 30 | * @return DirectionsDecorator 31 | */ 32 | public function __construct( Directions $dir, $map ) { 33 | parent::__construct( $dir, array( '_map' => $map ) ); 34 | } 35 | 36 | /** 37 | * Returns the javascript variable of the directions renderer 38 | * @link http://code.google.com/apis/maps/documentation/javascript/reference.html#DirectionsRenderer 39 | * 40 | * @return string 41 | */ 42 | public function getRendererJsVar() { 43 | return sprintf( '%s.directions.renderer', $this->_map ); 44 | } 45 | 46 | /** 47 | * Returns the javascript variable of the directions service 48 | * @link http://code.google.com/apis/maps/documentation/javascript/reference.html#DirectionsService 49 | * 50 | * @return string 51 | */ 52 | public function getServiceJsVar() { 53 | return sprintf( '%s.directions.service', $this->_map ); 54 | } 55 | 56 | 57 | 58 | 59 | } -------------------------------------------------------------------------------- /Service/DrivingDirections.php: -------------------------------------------------------------------------------- 1 | db = new \PDO( "$db_type:host=$host;dbname=$db_name", $user, $pw ); 36 | $this->db_table = $db_table; 37 | } 38 | 39 | /** 40 | * Get cached location 41 | * 42 | * @param string $location Location to get from cache 43 | * @return false|GeocodeCachedResult 44 | */ 45 | public function getCache( $location ) { 46 | $get = $this->db->prepare( sprintf( 'select lat, lng from %s where location=:location limit 1', $this->db_table ) ); 47 | $get->bindValue( ':location', $location ); 48 | if ( !$get->execute() ) { 49 | return false; 50 | } 51 | $result = $get->fetchAll( \PDO::FETCH_ASSOC ); 52 | if ( count( $result ) ) { 53 | return new \PHPGoogleMaps\Service\CachedGeocodeResult( new \PHPGoogleMaps\Core\LatLng( $result[0]['lat'], $result[0]['lng'], $location ), true ); 54 | } 55 | return false; 56 | } 57 | 58 | /** 59 | * Write to cache 60 | * 61 | * @param string $location Location to write to cache 62 | * @param float $lat Latitude of location 63 | * @param float $lng Longitude of location 64 | * @return bool 65 | */ 66 | public function writeCache( $location, $lat, $lng ) { 67 | $put = $this->db->prepare( sprintf( 'insert into %s (lat,lng,location) values(:lat,:lng,:location)', $this->db_table, $lat, $lng, $location ) ); 68 | $put->bindValue( ':lat', $lat ); 69 | $put->bindValue( ':lng', $lng ); 70 | $put->bindValue( ':location', $location ); 71 | $put->execute(); 72 | return (bool) $put->rowCount(); 73 | } 74 | 75 | } -------------------------------------------------------------------------------- /Service/GeocodeError.php: -------------------------------------------------------------------------------- 1 | error = $error; 36 | $this->location = $location; 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /Service/GeocodeException.php: -------------------------------------------------------------------------------- 1 | error = $geocode_error->error; 33 | $this->location = $geocode_error->location; 34 | parent::__construct(); 35 | } 36 | 37 | } -------------------------------------------------------------------------------- /Service/GeocodeResult.php: -------------------------------------------------------------------------------- 1 | location = $location; 36 | $this->response = $geocode_response; 37 | } 38 | 39 | /** 40 | * Get the LatLng object 41 | * Returns a LatLng representation of the object 42 | * 43 | * @return LatLng 44 | */ 45 | public function getLatLng() { 46 | return new \PHPGoogleMaps\Core\LatLng( $this->response->results[0]->geometry->location->lat, $this->response->results[0]->geometry->location->lng, $this->location ); 47 | } 48 | 49 | /** 50 | * Get lat 51 | * 52 | * @return float 53 | */ 54 | public function getLat() { 55 | return $this->response->results[0]->geometry->location->lat; 56 | } 57 | 58 | /** 59 | * Get lng 60 | * 61 | * @return float 62 | */ 63 | public function getLng() { 64 | return $this->response->results[0]->geometry->location->lng; 65 | } 66 | 67 | /** 68 | * Get a variable from the first geocode result 69 | * 70 | * @param string $var Varibale to get 71 | * @return mixed 72 | */ 73 | public function __get( $var ) { 74 | if ( isset( $this->response->results[0]->$var ) ) { 75 | return $this->response->results[0]->$var; 76 | } 77 | return null; 78 | } 79 | 80 | } -------------------------------------------------------------------------------- /Service/Geocoder.php: -------------------------------------------------------------------------------- 1 | status != 'OK' ) { 40 | $error = new GeocodeError( $response->status, $location ); 41 | return $error; 42 | } 43 | return new GeocodeResult( $location, $response ); 44 | } 45 | 46 | /** 47 | * Scrape the API 48 | * 49 | * @param string $location Location to geocode 50 | * @return GeocodeError|LatLng Returns a GeocodeError on error, LatLng on success. 51 | */ 52 | private static function scrapeAPI( $location ) { 53 | $url = sprintf( "http://maps.google.com/maps/api/geocode/json?address=%s&sensor=false", urlencode( $location ) ); 54 | $response = json_decode( Scraper::scrape( $url ) ); 55 | return $response; 56 | } 57 | 58 | } -------------------------------------------------------------------------------- /Service/WalkingDirections.php: -------------------------------------------------------------------------------- 1 | =5.3.0" 18 | }, 19 | "autoload": { 20 | "psr-0": { 21 | "PHPGoogleMaps": "" 22 | } 23 | }, 24 | "target-dir": "PHPGoogleMaps" 25 | } 26 | --------------------------------------------------------------------------------