Hello Everyone,

I have this great parser thanks to all the help from this forum :)  My
boss, however, just informed me that we are not going to install PHP
support on our server here at the office and that we need to see how
we can do it in ASP.net or java.  Basically we run IIS on a Microsoft
server with no PHP support.

We use the parser to specify custom icons for each GeoRSS feed coming
in.

Here's the code and thanks for your time in looking at this with me.

Jack

<?php

  function sendKmzFile($kmlString, $icon, $img) {

    $file = tempnam(sys_get_temp_dir(), "zip");

    $zip = new ZipArchive();

    $zip->open($file, ZipArchive::OVERWRITE);
    $zip->addFromString("doc.kml", $kmlString);

    if ($icon) {
      $imageFile = tempnam(sys_get_temp_dir(), "img");
      file_put_contents($imageFile, file_get_contents($icon));
      $zip->addFile($imageFile, $img);
    }
    $zip->close();

    // Stream the file to the client
    header("Content-Type: application/vnd.google-earth.kmz");
    header("Content-Length: " . filesize($file));
    header("Content-Disposition: attachment; filename=\"feed.kmz\"");
    readfile($file);

    unlink($file);
    if ($icon) {
      unlink($imageFile);
    }
  }

  function startElement($parser, $name, $attribs) {
    global $latitude, $longitude, $currentTag, $withinItem, $icon,
$kml;
    $currentTag = strtolower($name);
    switch ($currentTag) {

        case "item":
             $withinItem = true;
             $kml[] = "<Placemark>";
             if ($icon) {
               $kml[] = "<styleUrl>#icon</styleUrl>";
             }
             $latitude = 0;
             $longitude = 0;
             break;

        case "title":
             if ($withinItem) {
               $kml[] = "<name>";
             }
             break;

        case "description":
             if ($withinItem) {
               $kml[] = "<description>";
             }
             break;
    }
  }

  function endElement($parser, $name) {
    global $latitude, $longitude, $currentTag, $withinItem, $kml;
    switch (strtolower($name)) {

        case "item":
              $withinItem = false;
              $kml[] =   "<Point><coordinates>$longitude,$latitude,0</
coordinates></Point>";
              $kml[] = "</Placemark>";
              break;

        case "title":
              if ($withinItem) {
                $kml[] = "</name>";
              }
              break;

        case "description":
              if ($withinItem) {
                $kml[] = "</description>";
              }
              break;
    }
    $currentTag = "";
  }

  function characterData($parser, $data) {
    global $latitude, $longitude, $currentTag, $withinItem, $kml;
    switch ($currentTag) {

        case "item":
        case "title":
        case "description":
              if ($withinItem) {
                $kml[] = htmlspecialchars(trim($data));
              }
              break;

        case "geo:lat":
              $latitude = $data;
              break;

        case "geo:long":
              $longitude = $data;
              break;
    }
  }


$file = $_GET["url"];
$icon = $_GET["icon"];
if ($icon) {
  $img = "image" . strrchr($icon, '.');
}

$currentTag = "";
$withinItem = false;
$latitude = 0;
$longitude = 0;

$kml = array();
$kml[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$kml[] = "<kml xmlns=\"http://www.opengis.net/kml/2.2\";>";
$kml[] = "<Document>";

if ($icon) {
  $kml[] = "<Style id=\"icon\">";
  $kml[] =   "<IconStyle>";
  $kml[] =     "<Icon><href>$img</href></Icon>";
  $kml[] =   "</IconStyle>";
  $kml[] = "</Style>";
}

$xmlParser = xml_parser_create();
xml_set_element_handler($xmlParser, "startElement", "endElement");
xml_set_character_data_handler($xmlParser, "characterData");
$xml_data = file_get_contents($file);
xml_parse($xmlParser, $xml_data, true);
xml_parser_free($xmlParser);

$kml[] = "</Document>";
$kml[] = "</kml>";

sendKmzFile(implode($kml), $icon, $img);
?>

-- 
You received this message because you are subscribed to the Google Groups 
"Google Maps JavaScript API v3" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-maps-js-api-v3?hl=en.

Reply via email to