简体   繁体   中英

Parsing XML to a google map to create multiple markers

Todays issue is this:

    GDownloadUrl("http://www.mysite/map.php", 
    
    function(data) {
      var xml = GXml.parse(data);
      //var markers = xml.documentElement.getElementsByTagName("marker");
     
      //for (var i = 0; i < markers.length; i++) {
        //var address = new GLatLng(parseFloat(markers[i].getAttribute("location")));
        
        //var latlong = address.split(",");            
        
        //var latlongtomap = new GLatLng(parseFloat(latlong[0]), parseFloat(latlong[1]));
       
        var latlongtomap = new GLatLng(51.477711,-3.176555);

        var marker = createMarker(latlongtomap);
        
        map.addOverlay(marker);
     // }
    });
  }
}

If I run this it will get the Lat and Long coords and create a marker on a map for me. However, if I attempt to get the "location"tab from my php script (using all the commented out code):

map.php

<?php
mysql_connect("", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());

$query = "SELECT MAPADDRESS FROM report";
$result = mysql_query($query) or die(mysql_error());

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

while($row = mysql_fetch_array($result)){
   $node = $doc->createElement("marker");
   $newnode = $parnode->appendChild($node);
   $newnode->setAttribute("location", $row['MAPADDRESS']);
}
print $doc->saveXML();
?>

results of this are:

<markers>
   <marker location="51.477711,-3.176555"/>
   <marker location="51.472512,-3.187527"/>
   <marker location="51.471057,-3.186263"/>
   <marker location="51.473261,-3.192219"/>
</markers>

Why will the code not read this PHP properly?

try this instead:

var latlong = markers[i].getAttribute("location").split(",");            
var latlongtomap = new GLatLng(parseFloat(latlong[0]), parseFloat(latlong[1]));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM