* * * * ----------------------------------------------- * Example * ------------------------------------------------ J. K. Rowling ------------------------------------------------ * * * * * * * ------------------------------------------------ * Example * ------------------------------------------------ #myIconStyleID and we have a link http://www.google.com. ]]> -90.86948943473118,48.25450093195546 * ------------------------------------------------ * ------------------------------------------------ ------------------------------------------------ * ------------------------------------------------ * * * * * * ------------------------------------------------ * Example * ------------------------------------------------ 48.25475939255556 48.25207367852141 -90.86591508839973 -90.8714285289695 ------------------------------------------------ * * * * * * ------------------------------------------------ * Example * ------------------------------------------------ NE US Radar http://www.example.com/geotiff/NE/MergedReflectivityQComposite.kml onInterval // headers HTTP only compatible with mode "onExpire" 30 onStop 7 * ------------------------------------------------ * * * * * * * ------------------------------------------------ * Example * ------------------------------------------------ // only supported here -122.366212,37.818977,30 -122.365424,37.819294,30 unextruded -122.364383,37.824664,0 -122.364152,37.824322,0 ------------------------------------------------ * Other supported tags by Google Maps v3 * * * * * * * * * * * * * */ class EGMapKMLFeed { /** * * Holds all tags on the feed * @var unknown_type */ protected $elements; const STYLE_ICON = 'Icon'; // when using addStyle Array it specifies is an IconStyle const STYLE_LINE = 'Line'; // when using addStyle Array it specifies is an LineStyle const STYLE_POLY = 'Poly'; // when using addStyle Array it specifies is an PolyStyle /** * * constructor */ function __construct(){ $this->elements = new CMap(); } /** * * ATOM style author * @param string $name */ public function setAuthor( $name ){ if( null === $this->elements->itemAt('head') ) $this->elements->add('head', new CMap() ); $item = ''.$name.''; $this->elements->itemAt('head')->add('author', $item); } /** * * ATOM style link * @param string $url */ public function setLink( $url ){ if( null === $this->elements->itemAt('head') ) $this->elements->add('head', new CMap() ); $validator = new CUrlValidator(); if(!$validator->validateValue($url)) throw new CException( Yii::t('EGMap', 'EGMapKMLFeed.setLink Url does not seem to valid') ); $item = ''; $this->elements->itemAt('head')->add('link', $item); } /** * * Adding an externally created Tag at the end of its 'body' section * Note: this method does not validates the tag * @param EGMapKMLNode $tag * @return string id of the inserted Tag */ public function addTag( EGMapKMLNode $tag ){ if( null === $this->elements->itemAt('body') ) $this->elements->add('body', new CMap() ); $name = uniqid(); $this->elements->itemAt('body')->add(uniqid(), $tag->toXML()); return $name; } /** * * Removes inserted tag name from specific section * sections can be head, body, styles, and placemark * @param string $tagName to be removed * @param string $section where to remove the tag */ public function removeTag( $tagName, $section ){ foreach($this->elements as $map) { if($map->itemAt($section)){ $map->itemAt($section)->remove($tagName); return true; } } return false; } /** * * This method is to add style tags as arrays * the style nodes are represented as the following: *
	 * // Icon
	 * $nodes = array( 'href'=>'http://url');
	 * // Line
	 * $nodes = array( 'color'=>'#FFAA00','width'=>2);
	 * // Polyline 
	 * $nodes = array( 'color'=>'#FFAA00','colorMode'=>'random' );
	 * 
* @param string $styleId id of the style * @param string $styleType the type of the style * @param array $nodes the tags to insert */ public function addStyleArray( $styleId, $styleType = self::STYLE_ICON, $nodes = array() ){ $item = ''; if( null === $this->elements->itemAt('styles') ) $this->elements->add( 'styles', new CMap() ); $this->elements->itemAt('styles')->add( $styleId, $item ); } /** * Adds a placemark on array structure * Example: *
	 * $nodes = array(
	 * 	'name'=>array('content'=>'testing'),
	 *	'description'=>array('content'=>'This marker has HTML'),
	 *	'styleUrl'=>array('content'=>'#style2'),
	 *	'Point'=>array('children'=>array(
	 *		'coordinates'=>array('content'=>'2.9087440013885635,39.719588117933185,0'))));
	 *
	 * 	 $kml->addPlacemark($nodes);
	 * 
* @param array $nodes the tags to insert */ public function addPlacemarkArray( $nodes = array() ){ $item = ''; $item .= $this->batchNodeCreate($nodes); $item .= ''; if( null === $this->elements->itemAt('body') ) $this->elements->add( 'body', new CMap() ); $name = uniqid(); $this->elements->itemAt('body')->add( $name, $item ); return $name; } /** * * Converts array given tags to its XML representation * Note: It does not check for correct array structure * * @param array $tags */ public function batchNodeCreate( $tags ){ $result = ''; if(is_array( $tags) ){ foreach($tags as $tag=>$el){ $result .= CHtml::openTag($tag, (isset($el['attributes']) && is_array($el['attributes'])? $el['attributes']:array())); $result .= isset($el['content'])? ($tag=='description'? '': $el['content']) : ''; $result .= isset($el['children']) && is_array($el['children'])? $this->batchNodeCreate($el['children']) : ''; $result .= CHtml::closeTag($tag); } } return $result; } /** * * Generates the feed */ public function generateFeed( ){ // you can choose between the both, as both of them work correctly // Google Earth MIME/TYPE // header('Content-type: application/vnd.google-earth.kml+xml'); header("Content-type: text/xml"); echo ''; echo ''; $this->renderItems(array('head', 'body','styles','placemarks')); echo ''; echo ''; } /** * * Render tags by sections * @param array | string sections to render $sections */ public function renderItems( $sections ){ if(!is_array($sections)) $sections = array( $sections ); foreach( $sections as $section ){ if(null === $this->elements->itemAt($section)) continue; foreach($this->elements->itemAt($section) as $tag) echo $tag; } } }