To start, this is something I am experimenting with to learn more about php, google maps api, and xml manipulation so excuse me if I am noobish.
I have been able to successfully retrieve and parse xml data from a publicly available url to practice with. This XML has multiple types of namespaces throughout and I am able to retrieve all different objects I need but I am stuck after that.
I want to be able to take the coordinate objects from the XML (blockxcoord, and blockycoord) and plot them on a map. Am I unsuccessful because I am using a URL as my data source? Why is this so difficult? What is the easiest way to do this? Convert to KML? My attempts with the google maps api V3 haven't been successful so I did not include that code here but could use all the recommendations I could get. If my attempts would help figure more out then I can post that here.
I just want to know if what I have done here can be easily converted to markers on a map. I want to load markers on a map live as the file updates. I have not tried this with a local data source yet. Thanks!
<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc(url)
{
var xmlhttp;
var txt,x,xx,i;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
txt="<table border='1'><tr><th>Start Date and Time</th><th>End Date and Time</th><th>Offense</th><th>Address</th><th>X Coordinates</th><th>Y Coordinates</th></tr>";
x=xmlhttp.responseXML.documentElement.getElementsByTagName("entry");
for (i=0;i<x.length;i++)
{
txt=txt + "<tr>";
xx=x[i].getElementsByTagName("start_date");
{
try
{
txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
}
catch (er)
{
txt=txt + "<td> </td>";
}
}
xx=x[i].getElementsByTagName("end_date");
{
try
{
txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
}
catch (er)
{
txt=txt + "<td> </td>";
}
}
xx=x[i].getElementsByTagName("offense");
{
try
{
txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
}
catch (er)
{
txt=txt + "<td> </td>";
}
}
xx=x[i].getElementsByTagName("blocksiteaddress");
{
try
{
txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
}
catch (er)
{
txt=txt + "<td> </td>";
}
}
xx=x[i].getElementsByTagName("blockxcoord");
{
try
{
txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
}
catch (er)
{
txt=txt + "<td> </td>";
}
}
xx=x[i].getElementsByTagName("blockycoord");
{
try
{
txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
}
catch (er)
{
txt=txt + "<td> </td>";
}
}
txt=txt + "</tr>";
}
txt=txt + "</table>";
document.getElementById('Info').innerHTML=txt;
}
}
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="Info">
<button onclick="loadXMLDoc('http://ift.tt/1xsT5y1')">Get up to date information on criminal activity in Washington DC!</button>
</div>
</body>
</html>
No comments:
Post a Comment