PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/library/phooglemap.php

https://github.com/josesanch/wimpy
PHP | 385 lines | 205 code | 47 blank | 133 comment | 35 complexity | afe1d2f16e55e56838dadf9ad237dd37 MD5 | raw file
  1. <?php
  2. /**
  3. * Phoogle Maps 2.0 | Uses Google Maps API to create customizable maps
  4. * that can be embedded on your website
  5. * Copyright (C) 2005 Justin Johnson
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. *
  22. * Phoogle Maps 2.0
  23. * Uses Google Maps Mapping API to create customizable
  24. * Google Maps that can be embedded on your website
  25. *
  26. * @class Phoogle Maps 2.0
  27. * @author Justin Johnson <justinjohnson@system7designs.com>
  28. * @copyright 2005 system7designs
  29. */
  30. class PhoogleMap{
  31. /**
  32. * validPoints : array
  33. * Holds addresses and HTML Messages for points that are valid (ie: have longitude and latitutde)
  34. */
  35. var $validPoints = array();
  36. /**
  37. * invalidPoints : array
  38. * Holds addresses and HTML Messages for points that are invalid (ie: don't have longitude and latitutde)
  39. */
  40. var $invalidPoints = array();
  41. /**
  42. * mapWidth
  43. * width of the Google Map, in pixels
  44. */
  45. var $mapWidth = 300;
  46. /**
  47. * mapHeight
  48. * height of the Google Map, in pixels
  49. */
  50. var $mapHeight = 300;
  51. /**
  52. * apiKey
  53. * Google API Key
  54. */
  55. var $apiKey = "";
  56. /**
  57. * showControl
  58. * True/False whether to show map controls or not
  59. */
  60. var $showControl = true;
  61. /**
  62. * showType
  63. * True/False whether to show map type controls or not
  64. */
  65. var $showType = true;
  66. /**
  67. * controlType
  68. * string: can be 'small' or 'large'
  69. * display's either the large or the small controls on the map, small by default
  70. */
  71. var $controlType = 'small';
  72. /**
  73. * zoomLevel
  74. * int: 0-17
  75. * set's the initial zoom level of the map
  76. */
  77. var $zoomLevel = 4;
  78. private $icons = array();
  79. /**
  80. * @function addGeoPoint
  81. * @description Add's an address to be displayed on the Google Map using latitude/longitude
  82. * early version of this function, considered experimental
  83. */
  84. function addGeoPoint($lat, $long, $title, $infoHTML, $icon = null, $callback = null, $id = null) {
  85. $pointer = count($this->validPoints);
  86. $this->validPoints[$pointer]['lat'] = $lat;
  87. $this->validPoints[$pointer]['long'] = $long;
  88. $this->validPoints[$pointer]['title'] = $title;
  89. $this->validPoints[$pointer]['htmlMessage'] = $infoHTML;
  90. if($icon) $this->validPoints[$pointer]['icon'] = $icon;
  91. if($id) $this->validPoints[$pointer]['id'] = $id;
  92. if($callback) $this->validPoints[$pointer]['callback'] = $callback;
  93. return $pointer;
  94. }
  95. /**
  96. * @function centerMap
  97. * @description center's Google Map on a specific point
  98. * (thus eliminating the need for two different show methods from version 1.0)
  99. */
  100. function centerMap($lat,$long){
  101. $this->centerMap = "map.setCenter(new GLatLng(".$lat.",".$long."), ".$this->zoomLevel.");\n";
  102. }
  103. /**
  104. * @function addAddress
  105. * @param $address:string
  106. * @returns Boolean True:False (True if address has long/lat, false if it doesn't)
  107. * @description Add's an address to be displayed on the Google Map
  108. * (thus eliminating the need for two different show methods from version 1.0)
  109. */
  110. function addAddress($address,$htmlMessage = null, $icon = null){
  111. if (!is_string($address)){
  112. die("All Addresses must be passed as a string");
  113. }
  114. $apiURL = "http://maps.google.com/maps/geo?&output=xml&key=".$this->apiKey."&q=";
  115. $addressData = file_get_contents($apiURL.urlencode($address));
  116. $results = $this->xml2array(utf8_encode($addressData));
  117. if (empty($results['kml'][Response]['Placemark']['Point']['coordinates'])){
  118. $pointer = count($this->invalidPoints);
  119. $this->invalidPoints[$pointer]['lat']= $results['kml'][Response]['Placemark']['Point']['coordinates'][0];
  120. $this->invalidPoints[$pointer]['long']= $results['kml'][Response]['Placemark']['Point']['coordinates'][1];
  121. $this->invalidPoints[$pointer]['passedAddress'] = $address;
  122. $this->invalidPoints[$pointer]['htmlMessage'] = $htmlMessage;
  123. } else {
  124. $pointer = count($this->validPoints);
  125. $this->validPoints[$pointer]['lat']= $results['kml'][Response]['Placemark']['Point']['coordinates'];
  126. $this->validPoints[$pointer]['long']= $results['kml'][Response]['Placemark']['Point']['coordinates'];
  127. $this->validPoints[$pointer]['passedAddress'] = $address;
  128. $this->validPoints[$pointer]['htmlMessage'] = $htmlMessage;
  129. if($icon) $this->validPoints[$pointer]['icon'] = $icon;
  130. }
  131. }
  132. /**
  133. * @function showValidPoints
  134. * @param $displayType:string
  135. * @param $css_id:string
  136. * @returns nothing
  137. * @description Displays either a table or a list of the address points that are valid.
  138. * Mainly used for debugging but could be useful for showing a list of addresses
  139. * on the map
  140. */
  141. function showValidPoints($displayType,$css_id){
  142. $total = count($this->validPoints);
  143. if ($displayType == "table"){
  144. echo "<table id=\"".$css_id."\">\n<tr>\n\t<td>Address</td>\n</tr>\n";
  145. for ($t=0; $t<$total; $t++){
  146. echo"<tr>\n\t<td>".$this->validPoints[$t]['passedAddress']."</td>\n</tr>\n";
  147. }
  148. echo "</table>\n";
  149. }
  150. if ($displayType == "list"){
  151. echo "<ul id=\"".$css_id."\">\n";
  152. for ($lst=0; $lst<$total; $lst++){
  153. echo "<li>".$this->validPoints[$lst]['passedAddress']."</li>\n";
  154. }
  155. echo "</ul>\n";
  156. }
  157. }
  158. /**
  159. * @function showInvalidPoints
  160. * @param $displayType:string
  161. * @param $css_id:string
  162. * @returns nothing
  163. * @description Displays either a table or a list of the address points that are invalid.
  164. * Mainly used for debugging shows only the points that are NOT on the map
  165. */
  166. function showInvalidPoints($displayType,$css_id){
  167. $total = count($this->invalidPoints);
  168. if ($displayType == "table"){
  169. echo "<table id=\"".$css_id."\">\n<tr>\n\t<td>Address</td>\n</tr>\n";
  170. for ($t=0; $t<$total; $t++){
  171. echo"<tr>\n\t<td>".$this->invalidPoints[$t]['passedAddress']."</td>\n</tr>\n";
  172. }
  173. echo "</table>\n";
  174. }
  175. if ($displayType == "list"){
  176. echo "<ul id=\"".$css_id."\">\n";
  177. for ($lst=0; $lst<$total; $lst++){
  178. echo "<li>".$this->invalidPoints[$lst]['passedAddress']."</li>\n";
  179. }
  180. echo "</ul>\n";
  181. }
  182. }
  183. /**
  184. * @function setWidth
  185. * @param $width:int
  186. * @returns nothing
  187. * @description Sets the width of the map to be displayed
  188. */
  189. function setWidth($width){
  190. $this->mapWidth = $width;
  191. }
  192. /**
  193. * @function setHeight
  194. * @param $height:int
  195. * @returns nothing
  196. * @description Sets the height of the map to be displayed
  197. */
  198. function setHeight($height){
  199. $this->mapHeight = $height;
  200. }
  201. /**
  202. * @function setAPIkey
  203. * @param $key:string
  204. * @returns nothing
  205. * @description Stores the API Key acquired from Google
  206. */
  207. function setAPIkey($key){
  208. $this->apiKey = $key;
  209. }
  210. /**
  211. * @function printGoogleJS
  212. * @returns nothing
  213. * @description Adds the necessary Javascript for the Google Map to function
  214. * should be called in between the html <head></head> tags
  215. */
  216. function printGoogleJS(){
  217. echo $this->googleJS();
  218. }
  219. function googleJS(){
  220. return "\n<script src=\"http://maps.google.com/maps?file=api&amp;v=2&amp;key=".$this->apiKey."\" type=\"text/javascript\"></script>\n";
  221. }
  222. public function addCustomIcon($name, $url) {
  223. $icon = new GIcon($name);
  224. if($url) $icon->image = "\"$url\"";
  225. $this->icons[$name]= $icon;
  226. return $icon;
  227. }
  228. /**
  229. * @function showMap
  230. * @description Displays the Google Map on the page
  231. */
  232. public function createMap() {
  233. $str = "\n<div id=\"map\" style=\"width: ".$this->mapWidth."px; height: ".$this->mapHeight."px\">\n</div>\n";
  234. $str .= "
  235. <script type=\"text/javascript\">\n
  236. //<![CDATA[\n";
  237. $numPoints = count($this->validPoints);
  238. for ($g = 0; $g < $numPoints; $g++) {
  239. $str .= "var point".$g.";var marker".$g.";";
  240. }
  241. $str .="
  242. var map;
  243. function initialize_GoogleMAPS() {
  244. map = new GMap2(document.getElementById(\"map\"));
  245. if (GBrowserIsCompatible()) {\n";
  246. if (empty($this->centerMap)){
  247. $str.= "map.setCenter(new GPoint(".$this->validPoints[0]['long'].",".$this->validPoints[0]['lat']."), ".$this->zoomLevel.");\n";
  248. }else{
  249. $str.= $this->centerMap;
  250. }
  251. $str.= "}\n
  252. map.enableScrollWheelZoom()
  253. var icon = new GIcon();
  254. icon.image = \"http://labs.google.com/ridefinder/images/mm_20_red.png\";
  255. icon.shadow = \"http://labs.google.com/ridefinder/images/mm_20_shadow.png\";
  256. icon.iconSize = new GSize(12, 20);
  257. icon.shadowSize = new GSize(22, 20);
  258. icon.iconAnchor = new GPoint(6, 20);
  259. icon.infoWindowAnchor = new GPoint(5, 1);
  260. ";
  261. if ($this->showControl) {
  262. if ($this->controlType == 'small') { $str.= "map.addControl(new GSmallMapControl());\n"; }
  263. if ($this->controlType == 'large') { $str.= "map.addControl(new GLargeMapControl());\n"; }
  264. }
  265. if ($this->showType) {
  266. $str.= "map.addControl(new GMapTypeControl());\n";
  267. }
  268. foreach($this->icons as $name => $icon) {
  269. $str .= "var icon_$name = new GIcon(G_DEFAULT_ICON);\n";
  270. foreach(get_object_vars($icon) as $item => $value) {
  271. if($value) $str .= "icon_$name.$item = $value;\n";
  272. }
  273. }
  274. $numPoints = count($this->validPoints);
  275. for ($g = 0; $g < $numPoints; $g++) {
  276. if($this->validPoints[$g]['icon']) {
  277. $str .= "var options".$g." = { icon: icon_".$this->validPoints[$g]['icon']." };\n";
  278. } else {
  279. $str .= "var options".$g." = {};\n";
  280. }
  281. $str.= "point".$g." = new GPoint(".$this->validPoints[$g]['long'].",".$this->validPoints[$g]['lat'].");\n
  282. marker".$g." = new GMarker(point".$g.", options".$g.");\n
  283. map.addOverlay(marker".$g.")\n
  284. GEvent.addListener(marker".$g.", \"click\", function() {\n";
  285. $htmlMessage = $this->validPoints[$g]['htmlMessage']!=null ? $this->validPoints[$g]['htmlMessage'] : $this->validPoints[$g]['passedAddress'];
  286. if($this->validPoints[$g]['callback']) {
  287. $str.= "if(".$this->validPoints[$g]['callback']."(marker".$g.")) ";
  288. }
  289. $str.= "marker".$g.".openInfoWindowHtml(\"".$htmlMessage."\");\n";
  290. $str.= "});\n";
  291. }
  292. $str.= "}
  293. //]]>\n
  294. </script>\n";
  295. return $str;
  296. }
  297. function showMap() {
  298. return $this->createMap()."<script type=\"text/javascript\">initialize_GoogleMAPS();</script>";
  299. }
  300. // window.onload = showmap;
  301. ///////////THIS BLOCK OF CODE IS FROM Roger Veciana's CLASS (assoc_array2xml) OBTAINED FROM PHPCLASSES.ORG//////////////
  302. function xml2array($xml){
  303. $this->depth=-1;
  304. $this->xml_parser = xml_parser_create();
  305. xml_set_object($this->xml_parser, $this);
  306. xml_parser_set_option ($this->xml_parser,XML_OPTION_CASE_FOLDING,0);//Don't put tags uppercase
  307. xml_set_element_handler($this->xml_parser, "startElement", "endElement");
  308. xml_set_character_data_handler($this->xml_parser,"characterData");
  309. xml_parse($this->xml_parser,$xml,true);
  310. xml_parser_free($this->xml_parser);
  311. return $this->arrays[0];
  312. }
  313. function startElement($parser, $name, $attrs){
  314. $this->keys[]=$name;
  315. $this->node_flag=1;
  316. $this->depth++;
  317. }
  318. function characterData($parser,$data){
  319. $key=end($this->keys);
  320. $this->arrays[$this->depth][$key]=$data;
  321. $this->node_flag=0;
  322. }
  323. function endElement($parser, $name)
  324. {
  325. $key=array_pop($this->keys);
  326. if($this->node_flag==1){
  327. $this->arrays[$this->depth][$key]=$this->arrays[$this->depth+1];
  328. unset($this->arrays[$this->depth+1]);
  329. }
  330. $this->node_flag=1;
  331. $this->depth--;
  332. }
  333. //////////////////END CODE FROM Roger Veciana's CLASS (assoc_array2xml) /////////////////////////////////
  334. }//End Of Class
  335. class GIcon {
  336. public $image, $shadow, $iconSize, $shadowSize, $iconArchor;
  337. }
  338. ?>