duan19780629 2016-05-22 23:45
浏览 97

谷歌地图问题上的xml标记显示

I'm trying to display multiple markers from a sql database table to google maps. I've converted the data into XML format ready to be iterated into multiple markers on the map, but this is where i'm receiving issues.

I have already managed to do this once, but when I have tried adding another table of markers on the map im having issues. It's not with having two sets of similar codes the same, just this piece of code is wrong somewhere and I cannot see it anywhere.

What you see below are 2 functions for iterating through and marking them on the map.

the following function works :

downloadUrl("php/setMarkers.php");

but the following doesn't :

downloadUrl("php/sethospMarkers.php");

Both XML documents are getting called in and the response are shown fine in the google console and are both exact the same, so there is no difference. Even when i remove the "setMarkers" function out of the application I cannot get this "hospmarkers" function working. What is wrong with this function? am i just to tired and missing the basics?

One of the errors relating to this issue that chrome is showing is :

Uncaught TypeError: Cannot read property 'documentElement' of null

for the following call

var markers = xml.documentElement.getElementsByTagName("marker");

in the hospital function

 function initialize() {
        var bournemouth = new google.maps.LatLng(50.86244, -1.24778);
        var myOptions = {
            center: bournemouth,
            zoom: 10,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        var infoWindow = new google.maps.InfoWindow; 
        downloadUrl("php/setMarkers.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 acronym = markers[i].getAttribute("acronym");
          var point = new google.maps.LatLng(
            parseFloat(markers[i].getAttribute("lat")),
            parseFloat(markers[i].getAttribute("lng")));
          var html = "<b>" + name + "</b> <br/>" + acronym;
          var marker = new google.maps.Marker({
          map: map,
          position: point,
          icon: "img/bus.png"
        });
        bindInfoWindow(marker, map, infoWindow, html);
      }
    });

        downloadUrl("php/sethospMarkers.php", function(data) {
          var xml = data.responseXML;
          var markers = xml.documentElement.getElementsByTagName("marker");
          for (var i = 0; i < markers.length; i++) {
          var point = new google.maps.LatLng(
            parseFloat(markers[i].getAttribute("lat")),
            parseFloat(markers[i].getAttribute("lng")));
          var name = markers[i].getAttribute("name");
          var phone = markers[i].getAttribute("phone");
          var html = "<b>" + name + "</b> <br/>" + phone;
          var marker = new google.maps.Marker({
          map: map,
          position: point,
          icon: "img/hospital-building.png"
        });
        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() {}

The below script is the xml converter that I use to prepare the data, as you can see it is very simple. this is for the hospital Markers.

   <?php
require("sql_dbinfo.php");

function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','&lt;',$htmlStr);
$xmlStr=str_replace('>','&gt;',$xmlStr);
$xmlStr=str_replace('"','&quot;',$xmlStr);
$xmlStr=str_replace("'",'&#39;',$xmlStr);
$xmlStr=str_replace("&",'&amp;',$xmlStr);
return $xmlStr;
}

// Opens a connection to a MySQL server
$connection=mysqli_connect ('localhost', $username, $password);
if (!$connection) {
  die('Not connected : ' . mysql_error($connection));
}

// Set the active MySQL database
$db_selected = mysqli_select_db($connection, $database);
if (!$db_selected) {
  die ('Can\'t use db : ' . mysqli_error($connection));
}

// Select all the rows in the markers table
$query = "SELECT * FROM hospmarkers WHERE 1";
$result = mysqli_query($connection, $query);
if (!$result) {
  die('Invalid query: ' . mysqli_error($connection));
}

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

// Start XML file, echo parent node
echo '<markers>';

// Iterate through the rows, printing XML nodes for each
while ($row = @mysqli_fetch_assoc($result)){
  // ADD TO XML DOCUMENT NODE
  echo '<marker ';
  echo 'lat="' . $row['lat'] . '" ';
  echo 'lng="' . $row['lng'] . '" ';
  echo 'name="' . $row['name'] . '" ';
  echo 'phone="' . $row['phone'] . '" ';
  echo '/>';
}

// End XML file
echo '</markers>';

?>`

The sethospmarkers response is as follows.

"<markers><marker lat="53.853" lng="-0.411" name="East Riding Community Hospital" phone="01482 886600" /><marker lat="50.807" lng="-0.500" name="Zachary Merton Community Hospital " phone="01903 858100" /><marker lat="51.140" lng="-0.486" name="Musculoskeletal physiotherapy service - Cranleigh Village Hospital" phone="01483 782400" /><marker lat="51.380" lng="-0.406" name="Walton Community Hospital" phone="01932 414205" /><marker lat="51.315" lng="-0.556" name="Woking Community Hospital (Virgin Care)" phone="01483 715911" />

` Am I doing a something silly? or am i having a syntax issue? Thanks in advanced

  • 写回答

1条回答 默认 最新

  • duanjizhan9353 2016-05-23 12:05
    关注

    As Dr Molle Suggested.

    The name and phone attributes had to use the function parseToXML

      echo 'name="' . parseToXML($row['name']) . '" ';
      echo 'phone="' . parseToXML($row['phone']) . '" ';
    

    as shown above.

    Thanks Molle.

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算