Saturday, 19 July 2014

Only one Google maps marker showing from XML



I am using the Google Maps API and a XML file generated from a mySQL Database to populate the map with markers.


The markers were all displaying correctly, however I recently restructured the database table and now only one marker will show, if I revert back to the previous table the markers show as they should.


I initially thought this was an issue with the database table, however when I run the PHP file which generates the XML both the old and new PHP files (which are identical except from the SQL statement and node attributes) bring up an XML file with all the markers.


So the XML is generating correctly, its just not displaying all the markers on the map.


This is my map.js file:



var infoWindow = new google.maps.InfoWindow;

downloadUrl("mapfile.php", function(data) {

debugger;
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
debugger;
var propName = markers[i].getAttribute("propName");
var displayName = markers[i].getAttribute("displayName");
var propertytext = markers[i].getAttribute("propertytext");
var propertystatus = markers[i].getAttribute("propertystatus");
var amtavailable = markers[i].getAttribute("amtavailable");
var amtsold = markers[i].getAttribute("amtsold");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<div id='mapinfo'><a href='project.php?id=" + propName + "'><h2>" + displayName + "</h2></a> <b>" + amtavailable + " available, " + amtsold + " sold.</b><br>" + propertytext + "<br><img src='assets/image/projects/" + propName + ".jpg' width='40%' /></div>" ;

var icon = customIcons[propertystatus] || {
};
var marker = new google.maps.Marker({
map: map,
position: point,


icon: icon.icon
});
var anchor = new google.maps.Point(36, 0)
bindInfoWindow(marker, map, infoWindow, html);
debugger;
}
});
}

function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'mouseover', 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() {}


This is my mapfile.php file - this is the non working one:



$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);

$connection=mysql_connect ('host', 'username', 'password');
if (!$connection) { die('Not connected : ' . mysql_error());}



$db_selected = mysql_select_db('database', $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}



$query = "SELECT * FROM properties WHERE 1";
$result = mysql_query($query);
if (!$result) { die('Invalid query: ' . mysql_error());
}

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

while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("propName",$row['propName']);
$newnode->setAttribute("displayName",$row['propDisplayName']);
$newnode->setAttribute("amtavailable",$row['propAmtAvailable']);
$newnode->setAttribute("amtsold",$row['propAmtSold']);
$newnode->setAttribute("lat", $row['propLatitude']);
$newnode->setAttribute("lng", $row['propLongitude']);
$newnode->setAttribute("propertystatus", $row['propStatus']);
$newnode->setAttribute("propertytext", $row['googleMapsMarkerText']);

}

echo $dom->saveXML();

?>


This is mapfile2.php - this one displays the multiple markers, I know the attribute names don't match so they don't display all the correct information but the markers still all display on the map:



<?php

$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);

// Opens a connection to a MySQL server

$connection=mysql_connect ('host', 'username', 'password');
if (!$connection) { die('Not connected : ' . mysql_error());}

// Set the active MySQL database

$db_selected = mysql_select_db('database', $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}

// Select all the rows in the markers table

$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}

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



while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("propertyname",$row['propertyname']);
$newnode->setAttribute("lat", $row['lat']);
$newnode->setAttribute("lng", $row['lng']);
$newnode->setAttribute("propertystatus", $row['propertystatus']);
$newnode->setAttribute("propertytext", $row['propertytext']);
$newnode->setAttribute("imgUrl", $row['imgUrl']);
}

echo $dom->saveXML();

?>


I really can't understand what is stopping the markers from displaying when I use mapfile.php instead of mapfile2.php


Any help would be much appreciated.


Thanks


No comments:

Post a Comment