PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/sfEasyGMapPlugin/lib/GMapClient.class.php

https://bitbucket.org/leandroaromero/sgp
PHP | 274 lines | 124 code | 36 blank | 114 comment | 14 complexity | 59e0cb1155666b6b5f5cab2cb9b26db6 MD5 | raw file
Possible License(s): ISC
  1. <?php
  2. /**
  3. * A class to communicate with Google Maps
  4. * @author Fabrice Bernhard
  5. */
  6. class GMapClient
  7. {
  8. /**
  9. * Cache instance
  10. *
  11. * @var sfCache
  12. */
  13. protected $cache = null;
  14. /**
  15. * API key
  16. *
  17. * @var string
  18. */
  19. protected $api_key = null;
  20. /**
  21. * API key array
  22. *
  23. * @var array
  24. */
  25. protected $api_keys = null;
  26. const API_URL = 'http://maps.google.com/maps/geo?';
  27. const JS_URL = 'http://maps.google.com/maps/api/js?sensor=false';
  28. /**
  29. *
  30. * @param string $api_key
  31. * @author Fabrice Bernhard
  32. * @since 2009-06-17
  33. */
  34. public function __construct($api_key = null, $api_keys = null)
  35. {
  36. if (!is_null($api_keys))
  37. {
  38. $this->api_keys = $api_keys;
  39. }
  40. if (!is_null($api_key))
  41. {
  42. $this->api_key = $api_key;
  43. }
  44. else
  45. {
  46. $this->api_key = self::guessAPIKey($this->getAPIKeys());
  47. }
  48. }
  49. /**
  50. * Sets the Google Maps API key
  51. * @param string $key
  52. */
  53. public function setAPIKey($key)
  54. {
  55. $this->api_key = $key;
  56. }
  57. /**
  58. * Gets the Google Maps API key
  59. * @return string $key
  60. */
  61. public function getAPIKey()
  62. {
  63. return $this->api_key;
  64. }
  65. /**
  66. * Guesses and sets the API Key
  67. * @author Fabrice
  68. *
  69. */
  70. protected function guessAndSetAPIKey()
  71. {
  72. $this->setAPIKey(self::guessAPIKey($this->getAPIKeys()));
  73. }
  74. /**
  75. * Sets the Google Map API Key using the array_google_keys defined in the app.yml of your application
  76. * @param string $domain The domaine name
  77. * @author Fabrice
  78. *
  79. */
  80. public function setAPIKeyByDomain($domain)
  81. {
  82. $this->setAPIKey(self::getAPIKeyByDomain($domain, $this->getAPIKeys()));
  83. }
  84. /**
  85. * Guesses the GoogleMap key for the current domain
  86. * @param string[] $api_keys
  87. * @return string $api_key
  88. * @author Fabrice
  89. *
  90. */
  91. public static function guessAPIKey($api_keys = null)
  92. {
  93. if (isset($_SERVER['SERVER_NAME']))
  94. {
  95. return self::getAPIKeyByDomain($_SERVER['SERVER_NAME'], $api_keys);
  96. }
  97. else if (isset($_SERVER['HTTP_HOST']))
  98. {
  99. return self::getAPIKeyByDomain($_SERVER['HTTP_HOST'], $api_keys);
  100. }
  101. return self::getAPIKeyByDomain('default', $api_keys);
  102. }
  103. /**
  104. * abstract the sfConfig layer to override it when outside of symfony
  105. * @return string[]
  106. * @author fabriceb
  107. * @since Jun 17, 2009 fabriceb
  108. */
  109. public function getAPIKeys()
  110. {
  111. return $this->api_keys;
  112. }
  113. /**
  114. * abstract the sfConfig layer to override it when outside of symfony
  115. * @param string
  116. * @author fabriceb
  117. * @since Jun 17, 2009 fabriceb
  118. */
  119. public static function setAPIKeys($api_keys)
  120. {
  121. $this->api_keys = $api_keys;
  122. }
  123. /**
  124. * Static method to retrieve API key
  125. *
  126. * @param unknown_type $domain
  127. * @return unknown
  128. */
  129. public static function getAPIKeyByDomain($domain, $api_keys = null)
  130. {
  131. if (is_null($api_keys) && class_exists('sfConfig'))
  132. {
  133. $api_keys = sfConfig::get('app_google_maps_api_keys');
  134. }
  135. if (is_array($api_keys) && array_key_exists($domain, $api_keys))
  136. {
  137. $api_key = $api_keys[$domain];
  138. }
  139. else
  140. {
  141. if (is_array($api_keys) && array_key_exists('default', $api_keys))
  142. {
  143. $api_key = $api_keys['default'];
  144. }
  145. else
  146. {
  147. throw new sfException('No Google Map API key defined in the app.yml file of your application');
  148. }
  149. }
  150. return $api_key;
  151. }
  152. /**
  153. * Connection to Google Maps' API web service
  154. *
  155. * @param string $address
  156. * @param string $format 'csv' or 'xml'
  157. * @return string
  158. * @author fabriceb
  159. * @since 2009-06-17
  160. */
  161. public function getGeocodingInfo($address, $format = 'csv')
  162. {
  163. if ($this->hasCache())
  164. {
  165. $cache = $this->getCache()->get($format.$address);
  166. if ($cache)
  167. {
  168. return $cache;
  169. }
  170. }
  171. $apiURL = self::API_URL.'&output='.$format.'&key='.$this->getAPIKey().'&q='.urlencode($address);
  172. $raw_data = file_get_contents($apiURL);
  173. if ($this->hasCache())
  174. {
  175. $this->getCache()->put($format.$address, $raw_data);
  176. }
  177. return $raw_data;
  178. }
  179. /**
  180. * Dependency injection for the cache instance
  181. *
  182. * @param sfCache $cache
  183. * @author fabriceb
  184. * @since 2009-06-17
  185. */
  186. public function setCache($cache)
  187. {
  188. $this->cache = $cache;
  189. }
  190. /**
  191. *
  192. * @return sfCache
  193. * @author fabriceb
  194. * @since 2009-06-17
  195. */
  196. public function getCache()
  197. {
  198. return $this->cache;
  199. }
  200. /**
  201. * Is Geocode-Caching to the database enabled?
  202. * WARNING: this depends on the geocodes caching schema addition
  203. *
  204. * @return boolean $hasCache wether the geocodes Table is use to store address lookups
  205. * @author lukas.schroeder
  206. * @since 2009-06-09
  207. * @since 2009-06-17 fabriceb is now using dependency injection and the sfCache bastract class
  208. */
  209. public function hasCache()
  210. {
  211. return $this->cache instanceof sfCache;
  212. }
  213. /**
  214. * returns the URLS for the google map Javascript file
  215. * @param boolean $auto_load if the js of GMap should be loaded by default
  216. * @return string $js_url
  217. * @author fabriceb
  218. * @since 2009-06-17
  219. */
  220. public function getGoogleJsUrl($auto_load = true)
  221. {
  222. $js_url = self::JS_URL;
  223. return $js_url;
  224. }
  225. /**
  226. * Reverse geocoding info
  227. *
  228. * @return string
  229. * @author Vincent Guillon <vincentg@theodo.fr>
  230. * @since 2010-03-04
  231. */
  232. public function getReverseGeocodingInfo($lat, $lng)
  233. {
  234. $apiURL = 'http://maps.google.com/maps/geo?ll='.$lat.','.$lng;
  235. $raw_data = file_get_contents($apiURL);
  236. return $raw_data;
  237. }
  238. }