Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // MYSQL INFO
- $un = "";
- $pw = "";
- $db = "";
- $svr = "";
- $tbl = "";
- // MYSQL CONNECT INFO
- $con = mysqli_connect($svr, $un, $pw, $db);
- ## If the credentials are wrong
- if(!$con) {
- die("Could not connect to server: " . mysqli_error($con) . "<br>");
- }
- #$database = mysqli_select_db($db, $svr);
- ## If the database can't be reached.
- ## Likely misspelled, doesn't exist or the MYSQL user doesn't have access
- #if(!$database) {
- # die("Could not connect to database: " . mysqli_error($con) . "<br>");
- #}
- // Get the rows for the KML creation
- $query = "SELECT * FROM $tbl";
- $result = mysqli_query($con, $query);
- ## If the query is invalid it'll error out
- if(!$result) {
- die("There was an error with the query: " . mysqli_error($con) . "<br>");
- }
- // CREATE THE DOM DOCUMENT
- $dom = new DOMDocument('1.0', 'UTF-8');
- ## I'll admit that most of the stuff below this I copy and pasted
- ## because I'm not familiar with object oriented programming.
- ## https://developers.google.com/kml/articles/phpmysqlkml#outputkml
- ## Creates the root KML element and appends it to the root document.
- $node = $dom->createElementNS('http://earth.google.com/kml/2.1', 'kml');
- $parNode = $dom->appendChild($node);
- ## Creates a KML Document element and append it to the KML element.
- $dnode = $dom->createElement('Document');
- $docNode = $parNode->appendChild($dnode);
- ## Create a Folder element and append it to the KML element
- $fnode = $dom->createElement('Folder');
- $folderNode = $parNode->appendChild($fnode);
- ## Iterate through the MySQL results
- $row = @mysqli_fetch_assoc($result);
- ## Create a Placemark and append it to the document
- $node = $dom->createElement('Placemark');
- $placeNode = $folderNode->appendChild($node);
- ## Create an id attribute and assign it the value of id column
- $placeNode->setAttribute('id','linestring1');
- ## Create name, description, and address elements and assign them the values of
- ## the name, type, and address columns from the results
- $nameNode = $dom->createElement('name','My path');
- $placeNode->appendChild($nameNode);
- $descNode= $dom->createElement('description', 'Where I hiked');
- $placeNode->appendChild($descNode);
- ## Create a LineString element
- $lineNode = $dom->createElement('LineString');
- $placeNode->appendChild($lineNode);
- $exnode = $dom->createElement('extrude', '1');
- $lineNode->appendChild($exnode);
- $almodenode =$dom->createElement(altitudeMode,'relativeToGround');
- $lineNode->appendChild($almodenode);
- ## Create a coordinates element and give it the value of the lng and lat columns from the results
- ## I added this next line;
- $mycoords = $row['latitude'] . " " . $row['longitude'];
- $coorNode = $dom->createElement('coordinates',$mycoords);
- $lineNode->appendChild($coorNode);
- $kmlOutput = $dom->saveXML();
- // Output the KML
- header('Content-type: application/vnd.google-earth.kml+xml');
- echo $kmlOutput;
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement