AJAX Web Mapping Template
OK, template may be a bit of a strong word. Let’s call it “example” instead.
I’ve decided I’m not a bit fan of templates. I decided this after I downloaded another mapping site template to steal some code.
I appreciate all of the hard work that goes in to some of these template sites, but I never want to deploy one of those suckers. I just want to steal some code here and there. I really need examples more than templates. But I digress.
You might remember the much ballyhooed A Mapping Site in 19 Lines of Code. I republished that zip file (found here) with an AJAX addition.
Basically, we have three new files we didn’t have before. Our client-facing file is default2.php (default.php is our old example). We also have two additional files that will only be seen by the server - xajax.common.php and xajax.server.php.
XAJAX is one such toolkit for PHP, and having used it for a production site, I have to say I like it a lot. Let’s take a look at what we do to make it work.
First, take a look at xajax.server.php. It is basically the code we used in the non-AJAX example put in to a function (createMap). Just before that, we have two other lines of code:
require(“xajax.common.php”);We’re including xajax.common.php (we’ll get to that in a minute). Then we’re telling the XAJAX object we instantiated in xajax.common.php to process requests.
There are a couple of key things to note in the function that are different than what we had before. First, the funciton is getting the argument $aFormValues. This holds all of the form values from our client-side page. For example, we can read the value of the hidden form field extent by looking at $aFormValues[“extent”].
Now let’s look at this:
$objResponse->addAssign(“mapa”, “src”, $image_url);Next, check out xajax.common.php. There’s not much to this one. We insert the xajax class include and reference the xajax.server.php file. Then we register the functions we want to be available to AJAX requests. There might be 20 functions in your xajax.server.php file, but only the ones registered with XAJAX will be availble for asynchronous queries.
Finally, take a look at our client page, default2.php. We include the xajax.common.php page, and then we get one of these:
The rest is fairly similar to what we had before, with one exception. As this is an asychronous page, we don’t use form submits (which would refresh the page). Instead, you’ll see things like this:
xajax_createMap(xajax.getFormValues(‘formMap’))This is the createMap function we registered with XAJAX. It’s calling the function and passing it the values of our formMap form.
I hope that gives you some ideas about what you can do with AJAX and mapping sites, and how easy it can be with a good toolkit. If you’re in to PHP and you’re looking for a good AJAX toolkit, I highly recommend XAJAX. This example didn’t scratch the surface of what XAJAX can do.