tags. // // Input: // 1) a UTF-8 file // // Output: // 1) a sparse KML is returned to the caller just to show things are operating. // 2) the file rapid.kml is where the parsed feed result is written. // // Other notes: // A working example can be found at: http://www.turnhole.com // My Google Maps API key $key = "ENTER_YOUR_GOOGLE_MAPS_API_KEY_HERE"; header('Content-type: application/vnd.google-earth.kml+xml'); echo "\n"; echo "\n"; echo "\n"; $myFile = "rapid.kml"; $fzz = fopen($myFile, 'w') or die("can't open file"); $stringData = "\n"; fwrite($fzz, $stringData); $stringData = "\n"; fwrite($fzz, $stringData); $stringData = "\n"; fwrite($fzz, $stringData); // this is the feed coming from the weblog --> turnhole.com $ourFileName = "atom.xml"; $fh = fopen($ourFileName, 'r') or die("Can't open file"); $content = fread( $fh, filesize( $ourFileName ) ); // parse based on the character string "..." (three dots) $pieces = explode("...", $content); $counter1 = 0; $counter2 = 0; $placename = "New York, NY"; // get the number of pieces // this will be used later for pre-processing error handling //while($pieces[$counter1]) { //$counter1 = $counter1 + 1; //} // this is the big while loop where most of the action is // this is the big while loop where most of the action is while ($pieces[$counter2] AND $pieces[$counter2 + 1]) { // Echoes the KML file to my network link engine. if ($counter2 == 0) { echo "\n"; echo "KML Rapid Engine\n"; echo "Engine Running\n"; echo "\n"; echo "0, 0, 0\n"; echo "\n"; echo "\n"; } // this piece is disregarded // echo $pieces[$counter2]; $counter2 = $counter2 + 1; // the number below is the location $currentlocation = $pieces[$counter2]; $webreadylocation = str_replace(" ", "+", $currentlocation); // Desired address $address = "http://maps.google.com/maps/geo?q=$webreadylocation&output=xml&key=$key"; // Retrieve the URL contents $page = file_get_contents($address); // Parse the returned XML file $xml = new SimpleXMLElement($page); // Parse the coordinate string list($longitude, $latitude, $altitude) = explode(",", $xml->Response->Placemark->Point->coordinates); // this will associate a name to the address in a few cases. probably could remove most of this. // the important part is the spotname=currentlocation if ($xml->Response->Placemark->AddressDetails->Country->Locality->LocalityName) { $spotname = $xml->Response->Placemark->AddressDetails->Country->Locality->LocalityName; } else { if ($xml->Response->Placemark->AddressDetails->Country->AdministrativeArea->Locality->LocalityName) { $spotname = ($xml->Response->Placemark->AddressDetails->Country->AdministrativeArea->Locality->LocalityName); } else { $spotname = $currentlocation; } } // echo "Longitude: $longitude, Latitude: $latitude, Address: $spotname
"; $counter2 = $counter2 + 1; // this value is the location's description // echo $pieces[$counter2]; // write a static KML file $stringData = "\n"; fwrite($fzz, $stringData); $stringData = "$spotname\n"; fwrite($fzz, $stringData); $stringData = "$pieces[$counter2]\n"; fwrite($fzz, $stringData); $stringData = "\n"; fwrite($fzz, $stringData); $stringData = "$longitude,$latitude,0\n"; fwrite($fzz, $stringData); $stringData = "\n"; fwrite($fzz, $stringData); $stringData = "\n"; fwrite($fzz, $stringData); $counter2 = $counter2 + 1; // this next bracket ends the while loop } // the big while loop where most of the action is just ended // the big while loop where most of the action is just ended // A few KML administrative tags echo "
"; echo "
"; $stringData = "
"; fwrite($fzz, $stringData); $stringData = "
"; fwrite($fzz, $stringData); fclose($fzz); // Close the file fclose($fh); ?>