PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/gmaps/libraries/Gmap.php

https://github.com/MHordecki/milionkostek
PHP | 202 lines | 89 code | 29 blank | 84 comment | 3 complexity | f733db1fe86836cf3b9a5b774b8c9e2c MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Google Maps API integration.
  4. *
  5. * $Id: Gmap.php 2207 2008-03-01 20:44:52Z armen $
  6. *
  7. * @package Gmaps
  8. * @author Kohana Team
  9. * @copyright (c) 2007-2008 Kohana Team
  10. * @license http://kohanaphp.com/license.html
  11. */
  12. class Gmap_Core {
  13. /**
  14. * Retrieves the latitude and longitude of an address.
  15. *
  16. * @param string address
  17. * @return array longitude, latitude
  18. */
  19. public static function address_to_ll($address)
  20. {
  21. $lat = NULL;
  22. $lon = NULL;
  23. if ($xml = Gmap::address_to_xml($address))
  24. {
  25. // Get the latitude and longitude from the Google Maps XML
  26. // NOTE: the order (lon, lat) is the correct order
  27. list ($lon, $lat) = explode(',', $xml->Response->Placemark->Point->coordinates);
  28. }
  29. return array($lat, $lon);
  30. }
  31. /**
  32. * Retrieves the XML geocode address lookup.
  33. * ! Results of this method are cached for 1 day.
  34. *
  35. * @param string adress
  36. * @return object SimpleXML
  37. */
  38. public static function address_to_xml($address)
  39. {
  40. static $cache;
  41. // Load Cache
  42. ($cache === NULL) and $cache = Cache::instance();
  43. // Address cache key
  44. $key = 'gmap-address-'.sha1($address);
  45. if ($xml = $cache->get($key))
  46. {
  47. // Return the cached XML
  48. return simplexml_load_string($xml);
  49. }
  50. else
  51. {
  52. // Get the API key
  53. $api_key = Config::item('gmaps.api_key');
  54. // Send the address URL encoded
  55. $addresss = rawurlencode($address);
  56. // Disable error reporting while fetching the feed
  57. $ER = error_reporting(0);
  58. // Load the XML
  59. $xml = simplexml_load_file
  60. (
  61. 'http://maps.google.com/maps/geo?'.
  62. '&output=xml'.
  63. '&key='.$api_key.
  64. '&q='.rawurlencode($address)
  65. );
  66. if (is_object($xml) AND ($xml instanceof SimpleXMLElement) AND (int) $xml->Response->Status->code === 200)
  67. {
  68. // Cache the XML
  69. $cache->set($key, $xml->asXML(), array('gmaps'), 86400);
  70. }
  71. else
  72. {
  73. // Invalid XML response
  74. $xml = FALSE;
  75. }
  76. // Turn error reporting back on
  77. error_reporting($ER);
  78. }
  79. return $xml;
  80. }
  81. // Map settings
  82. protected $id;
  83. protected $options;
  84. protected $center;
  85. protected $control;
  86. // Map markers
  87. protected $markers;
  88. /**
  89. * Set the GMap center point.
  90. *
  91. * @param string HTML map id attribute
  92. * @param array array of GMap constructor options
  93. * @return void
  94. */
  95. public function __construct($id = 'map', $options = NULL)
  96. {
  97. // Set map ID and options
  98. $this->id = $id;
  99. $this->options = new Gmap_Options((array) $options);
  100. }
  101. /**
  102. * Set the GMap center point.
  103. *
  104. * @chainable
  105. * @param float latitude
  106. * @param float longitude
  107. * @param integer zoom level (1-16)
  108. * @return object
  109. */
  110. public function center($lat, $lon, $zoom = 6)
  111. {
  112. // Set center location and zoom
  113. $this->center = array($lat, $lon, $zoom);
  114. return $this;
  115. }
  116. /**
  117. * Set the GMap controls size.
  118. *
  119. * @chainable
  120. * @param string small or large
  121. * @return object
  122. */
  123. public function controls($size = NULL)
  124. {
  125. // Set the control type
  126. $this->controls = (strtolower($size) === 'small') ? 'Small' : 'Large';
  127. return $this;
  128. }
  129. /**
  130. * Set the GMap marker point.
  131. *
  132. * @chainable
  133. * @param float latitude
  134. * @param float longitude
  135. * @param string HTML for info window
  136. * @return object
  137. */
  138. public function add_marker($lat, $lon, $html = '')
  139. {
  140. // Add a new marker
  141. $this->markers[] = new Gmap_Marker($lat, $lon, $html);
  142. return $this;
  143. }
  144. /**
  145. * Render the map into GMap Javascript.
  146. *
  147. * @return string
  148. */
  149. public function render()
  150. {
  151. // Latitude, longitude, and zoom
  152. list ($lat, $lon, $zoom) = $this->center;
  153. // Map
  154. $map = 'map = new GMap2(document.getElementById("'.$this->id.'"));';
  155. // Map controls
  156. $controls = empty($this->controls) ? '' : 'map.addControl(new G'.$this->controls.'MapControl());';
  157. // Map centering
  158. $center = 'map.setCenter(new GLatLng('.$lat.', '.$lon.'));';
  159. // Map zoom
  160. $zoom = 'map.setZoom('.$zoom.');';
  161. // Render the Javascript
  162. return View::factory('gmaps/javascript', array
  163. (
  164. 'map' => $map,
  165. 'options' => $this->options,
  166. 'controls' => $controls,
  167. 'center' => $center,
  168. 'zoom' => $zoom,
  169. 'markers' => $this->markers,
  170. ))
  171. ->render();
  172. }
  173. } // End Gmap