未捕获的TypeError:无法读取属性' documentElement' null Googlemap

时间:2015-08-05 10:24:10

标签: javascript php

我意识到这是一个之前提出的问题,但是,我确实有一个奇怪的错误。我正在尝试从pgsql输出数据到googlemap。当我使用来自表单的参数编写查询时,不输出数据。正在创建xml文件,查询在phpPgAdmin中工作。但是,如果我进行硬编码的相同查询,则输出数据。我想问为什么会这样 这是代码。      

 <script type="text/javascript">

 //<![CDATA[

var customIcons = {
  7634: {
    icon: 'images/markers/1.png'
  },
  7644: {
    icon: 'images/markers/7.png'
  },
  7632: {
    icon: 'images/markers/8.png'
  },
  7633: {
    icon: 'images/markers/9.png'
  },
  7639: {
    icon: 'images/markers/10.png'
  },
  7646: {
    icon: 'images/markers/11.png'
  },
  7657: {
    icon: 'images/markers/12.png'
  },
  7649: {
    icon: 'images/markers/3.png'
  },
  7636: {
    icon: 'images/markers/4.png'
  },
  7648: {
    icon: 'images/markers/5.png'
  },
  7640: {
    icon: 'images/markers/6.png'
  },
  7650: {
    icon: 'images/markers/2.png'
  }
};



function initMap() {
    var map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(39.295614,23.241548),
    zoom: 7,
     mapTypeId: google.maps.MapTypeId.ROADMAP,
    mapTypeControl: false,
     mapTypeControlOptions: {
    style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
    },
     navigationControl: true,
     navigationControlOptions: {
     style: google.maps.NavigationControlStyle.SMALL
     }
  });
  var infoWindow = new google.maps.InfoWindow();

  // Change this depending on the name of your PHP file
  downloadUrl("temr_1.php", function(data) {
    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("marker");
    for (var i = 0; i < markers.length; i++) {
      var veh = markers[i].getAttribute("veh");
      var speed = markers[i].getAttribute("speed");
      var cons = markers[i].getAttribute("cons");
      var co = markers[i].getAttribute("co");
      var point = new google.maps.LatLng(
          parseFloat(markers[i].getAttribute("lat")),
          parseFloat(markers[i].getAttribute("lng")));
      var html = "<b>" + veh + "</b> <br/>" + speed + "</b> <br/>" + cons + "</b> <br/>" + co;
      var icon = customIcons[veh] || {};
      var marker = new google.maps.Marker({
        map: map,
        position: point,
        icon: icon.icon
      });
      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() {}

//]]>

这是带有查询的php代码。变量$ acase是一个多选框,它是一个数组。

<?php

$dateFrom= $_POST[date_from];
$acase = $_POST[vehid];
$date_to = $_POST[date_to];

foreach ($acase as $a){
   $counter++;
}

header("Content-type: text/xml");
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);


// Opens a connection to a MySQL server
$con= pg_connect("host=localhost port=5432 dbname=temr user=postgres    password=evan");

if (!$con) {
    print("Connection Failed.");
    echo "Failed";
    exit;
}



$query = pg_query($con, "SELECT * FROM gps_data WHERE vehicleid IN    (".implode(',', $acase).")");
//$query = pg_query($con, "SELECT * FROM gps_data WHERE vehicleid IN (7644,7632)"); -- this works, which is the same as the parameterised query



if (!$query) {
  echo "Oh No.\n";
  exit;
}

while ($row = pg_fetch_assoc($query)){
    $node = $dom->createElement("marker");
    $newnode = $parnode->appendChild($node);
    $newnode->setAttribute("veh",$row['vehicleid']);
    $newnode->setAttribute("speed", $row['speed']);
    $newnode->setAttribute("lat", $row['latitude']);
    $newnode->setAttribute("lng", $row['longitude']);
    $newnode->setAttribute("cons", $row['consumption']);
    $newnode->setAttribute("dis", $row['distance']);
    $newnode->setAttribute("co", $row['co2']);

  }

  echo $dom->saveXML();


?>

0 个答案:

没有答案