0

I'm using Google Maps to create a map that can pull (and toggle) various data layers from a database by calling a PHP script to output a KML layer in the form of XML. When I run the code below, nothing happens and I get no console errors. I know the XML returned is sound, as in my tests it is exactly the same as an XML data layer that does work.

html

function downloadUrl(url,callback) {
        var request = window.ActiveXObject ?
        new ActiveXObject('Microsoft.XMLHTTP') :
        new XMLHttpRequest;

        request.onreadystatechange = function() {
            if (request.readyState == 4) {
                callback(request, request.status);
            }
        };

        request.open('GET', url, true);
        request.send(null);
    }

    $(document).ready(function() {
        $('.kml_item').toggle(
                function() {
                    $(this).animate({backgroundColor: '#ffffff'}, 200);
                    downloadUrl("SQL_map.php", function(data) {
                        var xml = data.responseXML;
                        var kmlxml = new google.maps.KmlLayer(xml);
                        kmlxml.setMap(the_Map);
                    });

relevant part of SQL_map.php

header("Content-type: text/xml"); 
echo '<?xml version="1.0" encoding="UTF-8"?>
    <kml xmlns="http://earth.google.com/kml/2.1">
        <Document> 

        <Style id="a">
            <IconStyle>
                <Icon>
                           <href>http://maps.google.com/mapfiles/ms/icons/yellow-dot.png</href>
                </Icon>
            </IconStyle>
        </Style>';

// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
 echo '<Placemark><name>' . parseToXML($row['name']) . '</name><styleUrl>#a</styleUrl><point><coordinates>' . $row['lat'] . ',' . $row['lng'] . '</coordinates></point><description>' . parseToXML($row['name']) . '</description></Placemark>';
 }

  // End XML file
   echo '</Document>
   </kml>';

I'm not necessarily looking for someone to give me the whole code for what I'm trying to do, but the real question is whether or not this method of pulling the data and placing it on the map is even feasible. I've tried a couple other methods, each with their own problems.

Hat
  • 1,691
  • 6
  • 28
  • 44

1 Answers1

0

Not sure why you don't get or see an error, this is incorrect:

downloadUrl("SQL_map.php", function(data) {
                    var xml = data.responseXML;
                    var kmlxml = new google.maps.KmlLayer(xml);
                    kmlxml.setMap(the_Map);

KmlLayer requires a publicly accessible URL that can be accessed by Google's servers.

Depending on the complexity of your KML, you might be able to use geoxml3, and pass its parseKmlString method a string containing KML (but not responseXML).

geocodezip
  • 158,664
  • 13
  • 220
  • 245