0

I am having an error with my javascript for the Google Maps API. I have an almost identical script running on another page and it works flawlessly however when this one runs, it writes:

"Uncaught TypeError: Cannot read property 'documentElement' of null"

in the console. I only have one documentElement reference and it is EXACTLY the same as it is in my other page that works correctly. I can't figure out why this one is throwing the error.

My PHP to make the XML:

header("Content-type: text/xml");

// Start XML file, echo parent node
echo '<markers>';
$i = 1;
// Iterate through the rows, printing XML nodes for each
while ($row = @mysqli_fetch_assoc($result)){
  // ADD TO XML DOCUMENT NODE
  echo '<marker ';
  echo 'name="' . parseToXML($row['firstName']) . '" ';
  echo 'address="' . parseToXML($row['address1']) . '" ';
  echo 'city="' . parseToXML($row['city']) . '" ';
  echo 'zip="' . parseToXML($row['zip']) . '" ';
  echo 'comments="' . parseToXML($row['comments']) . '" ';
  echo 'location="' . parseToXML($row['location']) . '" ';
  echo 'lat="' . $row['lat'] . '" ';
  echo 'lng="' . $row['lng'] . '" ';
  echo 'distance="' . $i . '" ';
  echo '/>';
  $i++;
}
echo '<marker name="Test" address="123 Rose Road" city="Austin" zip="78745" comments="" location="" lat="" lng="" distance="home"/>';
// End XML file
echo '</markers>';

My Google Maps API Javascript that errors:

var customIcons = {
      1: {
        icon: 'mapicons/number_1.png',
        shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
      }
    };

    function load() {
      var map = new google.maps.Map(document.getElementById("map"), {
        center: new google.maps.LatLng(31.526342, -99.432643),
        zoom: 10,
        mapTypeId: 'roadmap'
      });
      var infoWindow = new google.maps.InfoWindow;


      // Change this depending on the name of your PHP file
      downloadUrl("phpsqlsearch_confirmed.php", function(data) {
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
          var name = markers[i].getAttribute("name");
          var address = markers[i].getAttribute("address");
          var city = markers[i].getAttribute("city");
          var zip = markers[i].getAttribute("zip");
          var distance = markers[i].getAttribute("distance");
          var comments = markers[i].getAttribute("comments");
          var location = markers[i].getAttribute("location");
          var point = new google.maps.LatLng(
              parseFloat(markers[i].getAttribute("lat")),
              parseFloat(markers[i].getAttribute("lng")));
          var html = "<b>" + name + " - " + location + "</b> <br/>" + address + "<br/><font size=-1 color='blue'>" + comments + "</font>";
          var icon = customIcons[distance] || {};
          var marker = new google.maps.Marker({
            map: map,
            position: point,
            icon: icon.icon,
            shadow: icon.shadow
          });
          bindInfoWindow(marker, map, infoWindow, html);
        }
      });
    }

    function bindInfoWindow(marker, map, infoWindow, html) {
      google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(html);
        infoWindow.open(map, marker);
      });
    }

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

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

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

    function doNothing() {}

    //]]>

Any help is greatly appreciated, if you need more information let me know because I am not too familiar with javascript.

1 Answer 1

1

The problem is that the response from the server is null, check that on the chrome debug console -> tab network. Check the response of the php request and see if it is an xml.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that helped me get on the right track. Apparently $_GET variables are frowned upon on that same xml output php page. I moved the $_GET's to another page and required it and that seemed to make it work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.