A spatial database does not store just plain records but is optimized to store and query data related to objects in space, including points, lines and polygons. Spatial databases have been around for a while now but are currently gaining popularity rapidly. Where the market was dominated by Oracle Spatial for a large number of years, there are promising alternatives these days including PostgreSQL in combination with PostGIS spatial extension, as well as MySQL with spatial extensions. Microsoft’s SQL Server can also store spatial information.
So what is the advantage of having a spatial database? For one, having all your data in a central database including well defined privileges, is a great thing to have. Besides this it is possible to extend the more standard SQL queries with spatial queries. E.g. which towns lie within a zone of 100km around the epicenter of the earthquake location. But a spatial database can also be used to populate a Google Map (or other online mapping applications) as I have shown in the image here:

In the example below I connect to my PostgreSQL database by means of PHP.
$db_handle = pg_connect(”host=localhost port=5432 dbname=outbreak_locations user=mark password=whatever”);
$query = “SELECT * FROM locations”;
$result = pg_exec($db_handle, $query);
Once we have our connection it is time to generate the XML format which will be used to populate our Google Map.
header(”Content-type: text/xml”);
// Start XML file, echo parent node
echo ‘
for ($row = 0; $row < pg_numrows($result); $row++) {
// ADD TO XML DOCUMENT NODE
echo ‘
echo ‘town=”‘ . parseToXML(pg_result($result, $row, ‘town’)) . ‘” ‘;
echo ‘country=”‘ . parseToXML(pg_result($result, $row, ‘country’)) . ‘” ‘;
echo ‘lat=”‘ . parseToXML(pg_result($result, $row, ‘latitude’)) . ‘” ‘;
echo ‘lon=”‘ . parseToXML(pg_result($result, $row, ‘longitude’)) . ‘” ‘;
echo ‘/>’;
}
// End XML file
echo ”;
?>
Make sure you include the parseToXML function included at the top of your php file:
function parseToXML($htmlStr)
{
$xmlStr=str_replace(’<’,'<’,$htmlStr);
$xmlStr=str_replace(’>’,'>’,$xmlStr);
$xmlStr=str_replace(’”‘,’”‘,$xmlStr);
$xmlStr=str_replace(”‘”,”’,$xmlStr);
$xmlStr=str_replace(”&”,’&’,$xmlStr);
return $xmlStr;
}
The above code is the php used to generate the XML. This php file is referred to from the index.html file which loads the actual Google Map. Besides calling the Google Map API using your own key file and declaring your marker symbols, add two functions:
function load() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById(”map”));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(0, 0), 1);
GDownloadUrl(”generate_gmap_xml.php”, function(data) {
var xml = GXml.parse(data);
var markers = xml.documentElement.getElementsByTagName(”marker”);
for (var i = 0; i < markers.length; i++) {
var town = markers[i].getAttribute(”town”);
var country = markers[i].getAttribute(”country”);
var type = markers[i].getAttribute(”type”);
var point = new GLatLng(parseFloat(markers[i].getAttribute(”lat”)),
parseFloat(markers[i].getAttribute(”lon”)));
var marker = createMarker(point, country, town, type);
map.addOverlay(marker);
}
});
}
}
and
function createMarker(point, name, country, town, type) {
var marker = new GMarker(point, customIcons[type]);
var html = “” + name + “
” + country + ”
” + town + ”
” + type;
GEvent.addListener(marker, ‘click’, function() {
marker.openInfoWindowHtml(html);
});
return marker;
}

Hello
Excuse me for my english
I just want to congratulate u about this topic.
I have to make an application based on the GMaps API and the database select for the project is the SGBD postgreSQL/PostGIS.
With this message it’s easier for me to use directly the postgreSQL spatial database.
But i have a request, have you ever try to make in relation the GMaps API with the PostGIS spatial extension.
My application have to show some “vectors”, the spatial extension of my prroject (PostGIS)
associated with the database (postgresql)
I hope that u will understand my message
Thancks for ur attention
Bye
Arnaud
Just a question about the connection between GMaps and Postgresql
is it possible to make the same connection but employing the geometry of a polygon that stocked on a postgis database.
I have a problem when i want to run this PHP code
How much PHP files do i have to make??
Connect.php
Generate_gmap_xml.php
…
???
Thanks for you help.
Arnaud
Hi Arnaud,
Sorry for the late reply, had to clear too much spam before I got to your threads. Anyways, it is possible to also serve up line and polygon data stored in a spatial database. The easiest way is to have it output kml which can directly be read by both Google Earth and Google Maps. This thread on Mark McLaren’s weblog talks about this and might help you out. Alternatively you could consider using GeoServer which can output shapefile data directly to kml and/or wms.
Hope this helps.
cheers,
Mark
Hi,
Thanks for the article. I need to develop a web application where the users can mark the locations on the Google Map, which would be saved in a PostGIS database for later use. I supposed I’d have to add elements to the XML file and export it to PostGIS when the session ends. Is it possible to do that?
regards,
Dharshana
Hi Dharshana,
For that you could do a simple INSERT through PHP for example. For inspiration you could look at Google’s MySQL tutorial http://code.google.com/support/bin/answer.py?answer=80201&topic=11364
cheers,
Mark