10.2 Mapping data from a database: Writing the server side script

Now that we have the maproom ready to grab data from the database, we need to write the script that allows us to do so.  The previous tutorial states that a filed named “getdata.php” will server this function.  Much like the “adddata.php” script that we wrote in Tutorial 9, this file must reside in the same directly as your current maproom file, and must also be on the same server that hosts the database.

Step 1: Create the server side script in PHP

Create a new file called “getdata.php” and add the following to it:

Step 2: Get the data from the server

Now we are ready to map data returned from our database. First, add the following variables to the global area:

Next, trace what is returned by our server side script. Create the callback function, and just add a trace to the data:

You should see an array returned with an object for each data row in the database. You should also see a lat/lon pair for each object.

Step 3: Map the data!

Now that we are getting data from the server, we are ready to map it.  replace the trace with the following code:

Step 4: Redraw the layer on map pan and zoom

Since the data retrieved is only showing what is within the current map view, you need to redraw the layer every time the map changes.  Add the following event liseteners to the initialize function to ensure that the layer is drawn when the map changes:

Leave a Reply