milionkostek /modules/gmaps/libraries/Gmap.php

Language PHP Lines 202
MD5 Hash f733db1fe86836cf3b9a5b774b8c9e2c Estimated Cost $2,105 (why?)
Repository https://github.com/MHordecki/milionkostek.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Google Maps API integration.
 *
 * $Id: Gmap.php 2207 2008-03-01 20:44:52Z armen $
 *
 * @package    Gmaps
 * @author     Kohana Team
 * @copyright  (c) 2007-2008 Kohana Team
 * @license    http://kohanaphp.com/license.html
 */
class Gmap_Core {

	/**
	 * Retrieves the latitude and longitude of an address.
	 *
	 * @param   string  address
	 * @return  array   longitude, latitude
	 */
	public static function address_to_ll($address)
	{
		$lat = NULL;
		$lon = NULL;

		if ($xml = Gmap::address_to_xml($address))
		{
			// Get the latitude and longitude from the Google Maps XML
			// NOTE: the order (lon, lat) is the correct order
			list ($lon, $lat) = explode(',', $xml->Response->Placemark->Point->coordinates);
		}

		return array($lat, $lon);
	}

	/**
	 * Retrieves the XML geocode address lookup.
	 * ! Results of this method are cached for 1 day.
	 *
	 * @param   string  adress
	 * @return  object  SimpleXML
	 */
	public static function address_to_xml($address)
	{
		static $cache;

		// Load Cache
		($cache === NULL) and $cache = Cache::instance();

		// Address cache key
		$key = 'gmap-address-'.sha1($address);

		if ($xml = $cache->get($key))
		{
			// Return the cached XML
			return simplexml_load_string($xml);
		}
		else
		{
			// Get the API key
			$api_key = Config::item('gmaps.api_key');

			// Send the address URL encoded
			$addresss = rawurlencode($address);

			// Disable error reporting while fetching the feed
			$ER = error_reporting(0);

			// Load the XML
			$xml = simplexml_load_file
			(
				'http://maps.google.com/maps/geo?'.
				'&output=xml'.
				'&key='.$api_key.
				'&q='.rawurlencode($address)
			);

			if (is_object($xml) AND ($xml instanceof SimpleXMLElement) AND (int) $xml->Response->Status->code === 200)
			{
				// Cache the XML
				$cache->set($key, $xml->asXML(), array('gmaps'), 86400);
			}
			else
			{
				// Invalid XML response
				$xml = FALSE;
			}

			// Turn error reporting back on
			error_reporting($ER);
		}

		return $xml;
	}

	// Map settings
	protected $id;
	protected $options;
	protected $center;
	protected $control;

	// Map markers
	protected $markers;

	/**
	 * Set the GMap center point.
	 *
	 * @param   string  HTML map id attribute
	 * @param   array   array of GMap constructor options
	 * @return  void
	 */
	public function __construct($id = 'map', $options = NULL)
	{
		// Set map ID and options
		$this->id = $id;
		$this->options = new Gmap_Options((array) $options);
	}

	/**
	 * Set the GMap center point.
	 *
	 * @chainable
	 * @param   float    latitude
	 * @param   float    longitude
	 * @param   integer  zoom level (1-16)
	 * @return  object
	 */
	public function center($lat, $lon, $zoom = 6)
	{
		// Set center location and zoom
		$this->center = array($lat, $lon, $zoom);

		return $this;
	}

	/**
	 * Set the GMap controls size.
	 *
	 * @chainable
	 * @param   string   small or large
	 * @return  object
	 */
	public function controls($size = NULL)
	{
		// Set the control type
		$this->controls = (strtolower($size) === 'small') ? 'Small' : 'Large';

		return $this;
	}

	/**
	 * Set the GMap marker point.
	 *
	 * @chainable
	 * @param   float   latitude
	 * @param   float   longitude
	 * @param   string  HTML for info window
	 * @return  object
	 */
	public function add_marker($lat, $lon, $html = '')
	{
		// Add a new marker
		$this->markers[] = new Gmap_Marker($lat, $lon, $html);

		return $this;
	}

	/**
	 * Render the map into GMap Javascript.
	 *
	 * @return  string
	 */
	public function render()
	{
		// Latitude, longitude, and zoom
		list ($lat, $lon, $zoom) = $this->center;

		// Map
		$map = 'map = new GMap2(document.getElementById("'.$this->id.'"));';

		// Map controls
		$controls = empty($this->controls) ? '' : 'map.addControl(new G'.$this->controls.'MapControl());';

		// Map centering
		$center = 'map.setCenter(new GLatLng('.$lat.', '.$lon.'));';

		// Map zoom
		$zoom = 'map.setZoom('.$zoom.');';

		// Render the Javascript
		return View::factory('gmaps/javascript', array
			(
				'map' => $map,
				'options' => $this->options,
				'controls' => $controls,
				'center' => $center,
				'zoom' => $zoom,
				'markers' => $this->markers,
			))
			->render();
	}

} // End Gmap
Back to Top