views:

413

answers:

1

I am retrieving a Google Earth .kml (xml) file and using the content to place markers in Google Maps. The particular XML tags I am interested in look like:

<Placemark>
       <name>Bahamas-LSI</name>
       <description><![CDATA[
       <img src="http://coralreefwatch.noaa.gov/satellite/ge/data/current/vs_Bahamas_LSI.bmp"&gt;
       <p>
       - <a href="http://coralreefwatch.noaa.gov/satellite/virtual_stations/greater_caribbean.html#Bahamas_LSI"&gt;
         SST/DHW time series</a>.
       <p>
       - E-mail [email protected] to subscribe free<br>automatic e-mail bleaching alert for this site.
       ]]></description>
       <Snippet></Snippet>
       <LookAt>
       <longitude>-76.5000</longitude>
       <latitude>23.5000</latitude>
       ..
</Placemark>

The following code extracts the name and lat & long, however I don't know how to extract the CDATA from the description tag using jQuery. I would like to be able to extract the actual html so I can then use it in an infowindow for the Google Maps marker.

// 
jQuery.get("CRWGE_current_products.kml", {}, function(data) {
  // for each placemark tag, extract the name & latlong
  // and then plot
  jQuery(data).find("placemark").each(function() {
    var station = jQuery(this);
    var name = station.find('name').text();
    var latlng = new google.maps.LatLng(parseFloat(station.find('latitude').text()),
                                      parseFloat(station.find('longitude').text()));

    // get html for station-specific data
    var content = station.find('description').text();
    console.log(content); <--- empty string

    setMarker(map, latlng, name, stressIcon, content)
  });
});
+2  A: 

Retrieve it as xml and you should be able to call text properly:

jQuery.get("CRWGE_current_products.kml", {}, function(data) { }, 'xml');

I've done this with xml feeds from USGS.gov, but I haven't tried it with .kml.

Jim Schubert
Thanks! I was stumped for a while by a Firebug 1.4 bug that eliminated all console output when I added the xml parameter (weird!). I switched to Safari, saw the code was OK, then upgraded to 1.5 and now all works...and yes indeed, I can now retrieve the html text.
timbo