Declarative Google Maps | April 7th, 2007
We are pretty big fans of the declarative approach to Ajax. Our up and coming Complete UI Ajax component product line is firmly based around declarative programming. The idea behind this is that the developer can use HTML like markup to define the majority of the component functionality while still being able to write a little bit of JavaScript to get the extra 20% of custom functionality that they need.
As an example of this I have put together a declarative Google Map. Currently it only supports a few declarative tags but it at least serves to give you an idea of how a regular JavaScript component could be wrapped declaratively. It could also be simplified quite a bit by building a JavaScript component from the ground up with declarative programming in mind - as the Nitobi framework enables of course
If you went about putting a Google Map into a web page imperatively, it would look something like this:
var map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(49.290327, -123.11348), 12);
var polyline = new GPolyline([
new GLatLng(49.265788, -123.069877),
new GLatLng(49.276988, -123.069534),
new GLatLng(49.276988, -123.099746),
new GLatLng(49.278108, -123.112106),
new GLatLng(49.2949043, -123.136825),
new GLatLng(49.3029641, -123.145065)
], "#ff0000", 10);
map.addOverlay(polyline);
The result of this is a nice Google Map that shows part of my daily bike ride to work. If you view the source of the web page you will see just that same code.
On the other hand, we could make that same web page declaratively meaning that we don’t have any extra JavaScript code to write but instead define our Google Map through custom HTML markup. This has the advantage, on top of the many advantages (and disadvantages) of declarative programming in general, that most web developers are familiar with HTML markup though might not be as comfortable with writing JavaScript code. So the equivalent declarative Google Map might look like this:
<g:map id="map" width="370px" height="380px" smallmapcontrol="true" maptypecontrol="true">
<g:center zoom="12">
<g:point lat="49.2853" lng="-123.11348"></g:point>
</g:center>
<g:polyline color="#FF0000" size="10">
<g:point lat="49.265788" lng="-123.069877"></g:point>
<g:point lat="49.276988" lng="-123.069534"></g:point>
<g:point lat="49.276988" lng="-123.099746"></g:point>
<g:point lat="49.278108" lng="-123.112106"></g:point>
<g:point lat="49.294904" lng="-123.136825"></g:point>
</g:polyline>
</g:map>
All the same elements are there as the imperative JavaScript version but there is no code to worry about. And, of course, the result is also the same. Here is a direct link to the source code - the top 1/3 of the code is the only interesting stuff, the rest is just helper functions that poached from the Nitobi Ajax Toolkit.
Take a look at the source code if you are interested in the details. Btw it currently has only been tested in Firefox 1.5 and IE 6/7.
This will be some of what I talk about in my session at XTech.
April 8th, 2007 at 12:36 am
There’s no question that the declarative approach is more elegant, but does it hinder graceful degradation?
For example, if I put an ntb:grid on a page, is there a best practice to ensure that the grid can degrade to an old fashioned HTML table in a legacy browser?
April 8th, 2007 at 3:22 pm
That’s a good question Evan. It would probably be more work, although doable, to do graceful degredation using a declarative approach.
The main problem would be browsers that don’t have JavaScript enabled.
For that reason and the fact that it might not be as customizable as a pure script approach, maybe declarative is not a good choice for building high performance public facing applications that need to work on many more browsers than a locked-down intranet environment?
April 9th, 2007 at 10:23 pm
[...] Dave Johnson » Blog Archive » Declarative Google Maps Example of imperative vs declarative programming with a google maps component. (tags: ajax declarative google googlemaps javascript mashup) [...]