Skip to content

Instantly share code, notes, and snippets.

@louy
Created February 28, 2011 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louy/847221 to your computer and use it in GitHub Desktop.
Save louy/847221 to your computer and use it in GitHub Desktop.
KML Feed Template
<?php
/**
* KML Feed Template
*
* @author Louy Alakkad <me@l0uy.com>
* @version 1.0
*/
/**
* what you need to define here is:
* $encoding string feed encoding (utf-8)
* $name string feed name
* $url string feed url
* $language string feed language
* $description string feed description
* $last_update string feed last update time
* $styles array ID => Icon
* $items array Arrays that each contains:
* 'name' => name
* 'description' => description (html)
* 'longitude' => longitude
* 'latitude' => latitude
* 'altitude' => altitude
* 'style' => style ID (for icon)
*
* Please set all manually so you wouldn't have XSS exploit or something :)
*/
isset($encoding) or $encoding = 'UTF-8';
isset($name) or $name = '';
isset($url) or $url = '';
isset($language) or $language = 'en';
isset($description) or $description = '';
isset($last_update) or $last_update = time('D, d M Y H:i:s +0000');
isset($styles) or $styles = array();
isset($items) or $items = array();
header('Content-Type: application/vnd.google-earth.kml+xml; charset=' . $encoding, true);
echo '<?xml version="1.0" encoding="'.$encoding.'"?'.'>';
?><kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name><?php echo $name; ?></name>
<visibility>1</visibility>
<open>1</open>
<link><?php echo $url; ?></link>
<description><?php echo $description; ?></description>
<lastBuildDate><?php echo $last_update; ?></lastBuildDate>
<language><?php echo $language; ?></language>
<?php foreach($styles as $id => $icon ) : ?>
<Style id="<?php echo $id; ?>">
<IconStyle>
<Icon>
<href><?php echo $icon; ?></href>
</Icon>
</IconStyle>
<LabelStyle>
<scale>0</scale>
</LabelStyle>
<BalloonStyle>
<text>$[description]</text>
</BalloonStyle>
</Style>
<?php endforeach; ?>
<?php foreach ($items as $item) :
isset( $item['altitude'] ) or $item['altitude'] = '0';
?>
<Placemark>
<name><?php echo $item['name']; ?></name>
<visibility>1</visibility>
<Snippet maxLines="0"></Snippet>
<description><![CDATA[<?php echo $item['description']; ?>]]></description>
<LookAt>
<longitude><?php echo (string) $item['longitude']; ?></longitude>
<latitude><?php echo (string) $item['latitude']; ?></latitude>
<altitude><?php echo (string) $item['altitude']; ?></altitude>
</LookAt>
<Point>
<coordinates><?php
echo (string) $item['longitude']
. ',' . (string) $item['latitude']
. ',' . (string) $item['altitude'];
?></coordinates>
</Point>
<styleUrl>#<?php echo $item['style']; ?></styleUrl>
</Placemark>
<?php endforeach; ?>
</Document>
</kml>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment