I am trying to use Google Map api v3 to render a map which will contain a groupe of locations. The data Lat and Lng are both stored in one field in my db called “Map” (screen)
Project table code
CREATE TABLE IF NOT EXISTS `project` (
`Id` varchar(255) NOT NULL,
`Type` varchar(20) NOT NULL default '',
`Location` varchar(50) NOT NULL default '',
`Map` text,
........
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
I followed the Google Developper’s tutorial on how to Use PHP/MySQL with Google Maps
The XML file is generated and works fine, but when I try to use it, no marker is displayed in the map. The map is empty.
No error is reported by fierbug or chrome console
The phpsqlajax_genxml code
<?php
require("phpsqlajax_dbinfo.php");
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("project");
$parnode = $dom->appendChild($node);
// Opens a connection to a MySQL server
$connection=mysql_connect ('localhost', $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 project table
$query = "SELECT * FROM project WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("Id",$row['Id']);
$newnode->setAttribute("Location", $row['Location']);
$newnode->setAttribute("Map", $row['Map']);
$newnode->setAttribute("Map", $row['Map']);
$newnode->setAttribute("Type", $row['Type']);
}
echo $dom->saveXML();
?>
The Javascript code
<!DOCTYPE html >
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
//<![CDATA[
var customIcons = {
apartment: {icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png'},
service_apartment: {icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png'},
condo: {icon: 'http://labs.google.com/ridefinder/images/mm_20_orange.png'}
};
function load() {
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(13.732304,100.567122),
zoom: 13,
mapTypeId: 'roadmap'
});
var infoWindow = new google.maps.InfoWindow;
// Change this depending on the name of your PHP file
downloadUrl("phpsqlajax_genxml.php", function(data) {
var xml = data.responseXML;
var project = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < project.length; i++) {
var Id = project[i].getAttribute("Id");
var Location = project[i].getAttribute("Location");
var Type = project[i].getAttribute("Type");
var point = new google.maps.LatLng(
parseFloat(project[i].getAttribute("Map")),
parseFloat(project[i].getAttribute("Map")));
var html = "<b>" + Id + "</b> <br/>" + Location;
var icon = customIcons[Type] || {};
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() {}
//]]>
</script>
</head>
<body onload="load()">
<div id="map" style="width: 1138px; height: 800px"></div>
</body>
</html>
The XML generated code
<project>
<marker Id="******" Location="*****" Map="a:5:{s:9:"CenterLat";s:18:"13.723127876409816";s:9:"CenterLng";s:18:"100.52852128985683";s:4:"Zoom";s:2:"16";s:9:"MarkerLat";s:18:"13.722750766481905";s:9:"MarkerLng";s:18:"100.52785811367796";}" Type="service_apartment"/>
......