* @package PhoogleMap * @license GNU / GPL / MIT * @version 2.1 works on PHP 5.2.x and PHP 5.3 * @copyright 2009 Alexandre JULIEN */ class PhoogleMap { /** * validPoints : array * Holds addresses and HTML Messages for points that are valid (ie: have longitude and latitutde) */ var $validPoints = array(); /** * invalidPoints : array * Holds addresses and HTML Messages for points that are invalid (ie: don't have longitude and latitutde) */ var $invalidPoints = array(); /** * mapWidth * width of the Google Map, in pixels */ var $mapWidth = 300; /** * mapHeight * height of the Google Map, in pixels */ var $mapHeight = 300; /** * apiKey * Google API Key */ var $apiKey = ""; /** * showControl * True/False whether to show map controls or not */ var $showControl = true; /** * showType * True/False whether to show map type controls or not */ var $showType = true; /** * controlType * string: can be 'small' or 'large' * display's either the large or the small controls on the map, small by default */ var $controlType = array(); /** * zoomLevel * int: 0-17 * set's the initial zoom level of the map */ var $zoomLevel = 5; /** * sideBarEnabled * boolean true or false * enable the sidebar (false by default) */ var $sideBarEnabled = false; /** * defaultIcon * (String) Javascript definition of the default icon, standard Google Map marker icon * by default */ var $defaultIcon = " var gIcon = new GIcon(); gIcon.image = \"http://labs.google.com/ridefinder/images/mm_20_red.png\"; gIcon.shadow = \"http://labs.google.com/ridefinder/images/mm_20_shadow.png\"; gIcon.iconSize = new GSize(12, 20); gIcon.shadowSize = new GSize(22, 20); gIcon.iconAnchor = new GLatLng(20, 6); gIcon.infoWindowAnchor = new GLatLng(1, 5);"; /** * @var actionClick */ var $actionClick = null; /** * @var $actionOver */ var $actionOver = null; /** * $marker : Marker identifier * @var unknown_type */ var $marker = null; /** * $dragndrop : Enable or disable drag & drop * @var unknown_type */ var $dragndrop = false; /** * $openWindowInformation : Enable or disable WindowInformation on markers */ var $openWindowInformation = false; /** * Instanciation of a PhoogleMap object * @param $key Google Map Api Key * @return unknown_type */ function __contruct ($key) { $this->apiKey = $key; } /** * return sidebarstatus */ function isSideBarEnabled () { return $this->sideBarEnabled; } /** * @function setDefaultIcon * @description Definition of a default icon */ function setDefaultIcon ($definition) { $this->defaultIcon = $definition; } /** * @function setShowType * @description Enable or disable with a flag the control view * @param $flag * @return unknown_type */ function setShowType ($flag) { $this->showType = $flag; } /** * @function getShowType * @desc Return the flag for control view * @param $flag * @return unknown_type */ function getShowType ($flag) { return $this->showType; } /** * @function setZoomLevel * @param $level * @desc Change the zoom level * @return unknown_type */ public function setZoomLevel ($level) { $this->zoomLevel = $level; } /** * @function getZoomLevel * @desc Return the zoom level * @return unknown_type */ public function getZoomLevel () { return $this->zoomLevel; } /** * @function addControlType * @description add the control type "small" or "large" * @param $controlType * @return unknown_type */ public function addControlType ($controlType) { $this->controlType[] = $controlType; } /** * @function getControlType * @description get the control type "small" or "large" * @return $controlType */ public function getType () { return $this->controlType; } /** Intelligent Zoom and resizing * @desc Reposition algorithm for rezoom and resizing fully automatized with an intelligence * @return unknown_type */ public function enableAutoZoom () { if (count($this->validPoints) <= 1) { return false; } $latitude_array = array(); $latitude_array = array(); foreach ($this->validPoints as $key => $value) { $latitude_array[] = $value['lat']; $longitude_array[] = $value['long']; } $minimal_latitude = min ($latitude_array); $maximal_latitude = max ($latitude_array); $minimal_longitude = min ($longitude_array); $maximal_longitude = max ($longitude_array); $central_latitude = $minimal_latitude + ($maximal_latitude - $minimal_latitude) / 2; $central_longitude = $minimal_longitude + ($maximal_longitude - $minimal_longitude) / 2; $miles = (3958.75 * acos(sin($minimal_latitude / 57.2958) * sin($maximal_latitude / 57.2958) + cos($minimal_latitude / 57.2958) * cos($maximal_latitude / 57.2958) * cos($maximal_longitude / 57.2958 - $minimal_longitude / 57.2958))); switch ($miles) { case ($miles < 0.2): $zoom = 2; break; case ($miles < 0.5): $zoom = 3; break; case ($miles < 1): $zoom = 3; break; case ($miles < 2): $zoom = 4; break; case ($miles < 3): $zoom = 5; break; case ($miles < 7): $zoom = 6; break; case ($miles < 15): $zoom = 8; break; case ($miles < 50): $zoom = 9; break; case ($miles < 300): $zoom = 12; break; case ($miles < 600): $zoom = 14; break; default: $zoom = 2; break; } $this->centerMap($central_latitude, $central_longitude); $this->zoomLevel = $zoom; } /** * @function getDefaultIcon * @return String Default Icon Definition */ function getDefaultIcon () { return $this->defaultIcon; } /** * @function setClickFunction * @desc Define the click event on a marker or on sidebar * @param Array:parameters * @return void */ function setActionClick ($function) { $this->actionClick = $function; } /** * @function getActionClick * @desc return the click event * @return array: $this->actionClick */ function getActionClick () { return $this->actionClick; } /** * @function setActionOver * @desc Definition of an action when the mouse is over the marker * @return void */ function setActionOver ($actionHover) { $this->actionOver = $actionHover; } /** * @function getActionHover * @desc Return an action when the mouse is over the marker * @return String */ function getActionHover () { return $this->actionOver; } /** * @function enableDragnDrop * @desc Enable drag & drop on marker * @return void */ function enableDragnDrop () { $this->dragndrop = true; } /** * Enable OpenWindowInformation * @return unknown_type */ function enableOpenWindowInformation () { $this->openWindowInformation = true; } /** * @function defineDragnDropAction * @desc Define an action when somebody makes a drag & drog on a marker * @return String */ function defineDragnDropAction ($parameters) { $command = ''; if ($this->sideBarEnabled == true && $parameters['action'] == 'updateSideBar') { $command = ''; } } /** * @function addGeoPoint * @description Add's an address to be displayed on the Google Map using latitude/longitude * early version of this function, considered experimental */ function addGeoPoint($long, $lat, $address, $title, $infoHTML, $icon = null) { $pointer = count($this->validPoints); $this->validPoints[$pointer]['long'] = $long; $this->validPoints[$pointer]['lat'] = $lat; $this->validPoints[$pointer]['passedAddress'] = $address; $this->validPoints[$pointer]['title'] = $title; $this->validPoints[$pointer]['htmlMessage'] = $infoHTML; $this->validPoints[$pointer]['icon'] = $icon; } /** * @function centerMap * @description center's Google Map on a specific point * (thus eliminating the need for two different show methods from version 1.0) */ function centerMap($lat,$long) { $this->centerMap = "map.setCenter(new GLatLng(".$lat.",".$long."), ".$this->zoomLevel.");\n"; } /** * @function addAddress * @param $address:(string) Full and true address of the marker * @param $htmlMessage:(String) the HTML Code for Description of the Information Window * @param $iconURL:(String) The URL of the ICON * @returns Boolean Return false if the geocode is valid or true even * @description Add's an address to be displayed on the Google Map * (thus eliminating the need for two different show methods from version 1.0) */ function addAddress($address, $title = null, $htmlMessage=null, $icon=null) { if (!is_string($address)){ die("All Addresses must be passed as a string"); } $apiURL = "http://maps.google.com/maps/geo?&output=xml&oe=utf-8&key=".$this->apiKey."&q="; $address = $apiURL.urlencode($address); $addressData = file_get_contents($address); $results = $this->xml2array($addressData); $xml = simplexml_load_file($address); if($results['kml']['Response']['Status']['code'] === '602' || $results['kml']['Response']['Status']['code'] === '620') { return false; } if (empty($results['kml']['Response']['Placemark']['Point']['coordinates'])){ $pointer = count($this->invalidPoints); $this->invalidPoints[$pointer]['lat']= $results['kml']['Response']['Placemark']['Point']['coordinates'][0]; $this->invalidPoints[$pointer]['long']= $results['kml']['Response']['Placemark']['Point']['coordinates'][1]; $this->invalidPoints[$pointer]['passedAddress'] = $address; $this->invalidPoints[$pointer]['title'] = $title; $this->invalidPoints[$pointer]['htmlMessage'] = $htmlMessage; $this->invalidPoints[$pointer]['icon'] = $icon; }else{ $pointer = count($this->validPoints); $this->validPoints[$pointer]['lat']= $results['kml']['Response']['Placemark']['Point']['coordinates']; $this->validPoints[$pointer]['long']= $results['kml']['Response']['Placemark']['Point']['coordinates']; $this->validPoints[$pointer]['passedAddress'] = $address; $this->validPoints[$pointer]['title'] = $title; $this->validPoints[$pointer]['htmlMessage'] = $htmlMessage; $this->validPoints[$pointer]['icon'] = $icon; $this->validPoints[$pointer]['accuracy'] = null; $this->validPoints[$pointer]['address'] = trim($xml->Response->Placemark->address); $lat = explode(",",$this->validPoints[$pointer]['lat']); $long = explode(",",$this->validPoints[$pointer]['lat']); $this->validPoints[$pointer]['lat'] = $lat[1]; $this->validPoints[$pointer]['long'] = $long[0]; return array ('lat' => $lat[1], 'long' => $long[0], 'accuracy' => $this->validPoints[$pointer]['accuracy'], 'address' => $this->validPoints[$pointer]['address']); } } /** * @function showValidPoints * @param $displayType:string * @param $css_id:string * @returns nothing * @description Displays either a table or a list of the address points that are valid. * Mainly used for debugging but could be useful for showing a list of addresses * on the map */ function showValidPoints($displayType,$css_id) { $total = count($this->validPoints); if ($displayType == "table"){ echo "\n\n\t\n\n"; for ($t=0; $t<$total; $t++){ echo"\n\t\n\n"; } echo "
Address
".$this->validPoints[$t]['passedAddress']."
\n"; } if ($displayType == "list"){ echo "\n"; } } /** * @function generateSideBar * @return nothing * @description Display a slidebar which generate dynamic links with Google Map */ function generateSideBar () { $total = count ($this->validPoints); $record = array (); foreach ($this->validPoints as $key => $value) { $entry = array (); // Here we recover all important informations $entry[] = ''.$value['passedAddress'].''; $entry[] = $value['title']; $record[] = $entry; } $this->sideBarEnabled = true; return $record; } /** * @static function generateIcon * @param $url (String) URL of the icon * @param $shadow (String) URL of the icon's shadow * @param $iconSize (Array (height, width)) icon's dimensions * @param $shadowSize (Array (height, width)) shadow icon's dimensions * @param $iconAnchor (Array (height, width)) Icon Anchor * @param $infoWindowAnchor (Array (height, width)) infoWindowAnchor * @return nothing * @description Generate javascript to make a marker icon */ static function generateIcon ($url, $shadow = null, $iconSize, $shadowSize = array ('height' => 0, 'width' => 0), $iconAnchor = array ('height' => 0, 'width' => 0), $infoWindowAnchor = array('height' => 5, 'width' => 5)) { return " var gIcon = new GIcon(); gIcon.image = \"".$url."\"; gIcon.shadow = \"".$shadow.".\"; gIcon.iconSize = new GSize(".$iconSize['height'].", ".$iconSize['width']."); gIcon.shadowSize = new GSize(".$shadowSize['height'].",".$shadowSize['width']."); gIcon.iconAnchor = new GLatLng(".$iconAnchor['height'].", ".$iconAnchor['width']."); gIcon.infoWindowAnchor = new GLatLng(".$infoWindowAnchor['height'].", ".$infoWindowAnchor['width'].");"; } /** * @function showInvalidPoints * @param $displayType:string * @param $css_id:string * @returns nothing * @description Displays either a table or a list of the address points that are invalid. * Mainly used for debugging shows only the points that are NOT on the map */ function showInvalidPoints($displayType,$css_id) { $total = count($this->invalidPoints); if ($displayType == "table"){ echo "\n\n\t\n\n"; for ($t=0; $t<$total; $t++){ echo"\n\t\n\n"; } echo "
Address
".$this->invalidPoints[$t]['passedAddress']."
\n"; } if ($displayType == "list"){ echo "\n"; } } /** * @function setWidth * @param $width:int * @returns nothing * @description Sets the width of the map to be displayed */ function setWidth($width) { $this->mapWidth = $width; } /** * @function setHeight * @param $height:int * @returns nothing * @description Sets the height of the map to be displayed */ function setHeight($height) { $this->mapHeight = $height; } /** * @function setAPIkey * @param $key:string * @returns nothing * @description Stores the API Key acquired from Google */ function setAPIkey($key) { $this->apiKey = $key; } /** * @function printGoogleJS * @returns nothing * @description Adds the necessary Javascript for the Google Map to function * should be called in between the html tags */ function printGoogleJS() { echo "\n\n"; } /** * showMap * enhance the view of the map on a PHP Template * @return HTML Code */ function showMap() { if (empty($this->validPoints)) { return false; } echo "\n
mapWidth."px; height: ".$this->mapHeight."px\">\n
\n"; echo " \n"; } /** * XML Parsing Engine * @param string $xml * @return array $xmlarray */ function xml2array($xml) { $this->depth=-1; $this->xml_parser = xml_parser_create(); xml_set_object($this->xml_parser, $this); xml_parser_set_option ($this->xml_parser,XML_OPTION_CASE_FOLDING, 0);//Don't put tags uppercase xml_set_element_handler($this->xml_parser, "startElement", "endElement"); xml_set_character_data_handler($this->xml_parser,"characterData"); xml_parse($this->xml_parser,$xml,true); xml_parser_free($this->xml_parser); return $this->arrays[0]; } /** * XML Parsing Method StartElement * @param $parser * @param $name * @param $attrs * @return unknown_type */ function startElement($parser, $name, $attrs) { $this->keys[]=$name; $this->attrs[] = $attrs; $this->node_flag=1; $this->depth++; } /** * characterData * @param $parser * @param $data * @return unknown_type */ function characterData($parser,$data) { if(!trim($data)) return; $key=end($this->keys); $this->arrays[$this->depth][$key]=$data; $this->node_flag=0; } /** * endElement * @param $parser * @param $name * @return unknown_type */ function endElement($parser, $name) { $key=array_pop($this->keys); if($this->node_flag==1){ $this->arrays[$this->depth][$key]=$this->arrays[$this->depth+1]; $this->arrays[$this->depth+1] = null; // unset($this->arrays[$this->depth+1]); } $this->node_flag=1; $this->depth--; } } ?>