Easy map controls in Ubiquity XForms

The map controls in Ubiquity XForms are by far the simplest way to add Google Maps to your web pages. This article shows you how to use them and some of the extra benefits that you get from the XForms engine powering them. Screenshot of a map in Ubiquity XForms

A basic map

There are three easy steps involved in adding a map to your web page. The first one is to paste the XForms namespace declaration on to the html root element:

  1. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:xf="http://www.w3.org/2002/xforms">

Next, you need to paste a couple of script tags into your document's head:

  1. <script src="http://ubiquity-xforms.googlecode.com/svn/trunk/src/ubiquity-loader.js" type="text/javascript">/**/</script>
  2. <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=ENTER YOUR GOOGLE MAPS API KEY HERE" type="text/javascript">/**/</script>

These will load Ubiquity XForms and Google Maps. Google Maps requires you to get an API key from their site. When you have one, just paste it in place of the text: ENTER YOUR GOOGLE MAPS API KEY HERE

The third and final step is to add an XForms output control to the body of your document. The control must have the attribute class="geolocation" and a value attribute that evaluates to the location that you want to display. The location can be in the form of an address, or a semi-colon separated latitude and longitude pair of co-ordinates. For instance, the following markup displays a map showing the location of the webBackplane office in central London:

  1. <xf:output value="'4 Pear Tree Court, London, EC1R 0DS'" class="geolocation" />

If you just want to display a static map on your page, then that is all you need. However, the presence of Ubiquity XForms enables things to get a lot more interesting than that.

User input

You may notice that the map in the previous example does not permit the user to change the location. There are buttons to zoom in and out, but the centre point is locked in place. This is because it uses output, which does not allow user input by definition. In order for a user to move your map around, you have to swap the output control for a range control. And, since range doesn't support the value attribute, you'll also need to add an XForms model containing the instance data that the range can be bound to:

  1. <xf:model>
  2. <xf:instance>
  3. <location xmlns="">4 Pear Tree Court, London, EC1R 0DS</location>
  4. </xf:instance>
  5. </xf:model>
  6.  
  7. <xf:range ref="/location" class="geolocation" />
4 Pear Tree Court, London, EC1R 0DS

You will see that, although the map looks similar to the last example, it now has buttons for panning the map north, south, east and west. Additionally, if you click and hold the left-hand mouse button, you can drag the map to move it to a new location. Each time you move the map, the bound instance data will get updated with the centre point, indicated by the red map marker. You can further verify this binding by adding a second map, bound to the same instance data as the first one. Each time you move the position of the first map, the position of the second will get updated automatically by the XForms dependency engine and vice versa:

  1. <xf:model>
  2. <xf:instance>
  3. <location xmlns="">4 Pear Tree Court, London, EC1R 0DS</location>
  4. </xf:instance>
  5. </xf:model>
  6.  
  7. <xf:range ref="/location" class="geolocation" />
  8.  
  9. <xf:range ref="/location" class="geolocation" />
4 Pear Tree Court, London, EC1R 0DS

Adding a hint

In XForms, each of the UI controls can be complemented with a child hint element. Ubiquity XForms makes use of hint for map controls to add contextual information to the map marker. When the user clicks the left-hand mouse button on the central map marker, an information bubble will pop up. In an output map control, this information might be static data:

  1. <xf:output value="'4 Pear Tree Court, London, EC1R 0DS'" class="geolocation">
  2. <xf:hint>webBackplane office</xf:hint>
  3. </xf:output>
webBackplane office

For range controls though, you would most likely want the hint itself to be based dynamically on the current location of the map. Fortunately, this is straightforward to achieve with the ref attribute on the hint element:

  1. <xf:model>
  2. <xf:instance>
  3. <location xmlns="">4 Pear Tree Court, London, EC1R 0DS</location>
  4. </xf:instance>
  5. </xf:model>
  6.  
  7. <xf:range ref="/location" class="geolocation">
  8. <xf:hint ref="." />
  9. </xf:range>
4 Pear Tree Court, London, EC1R 0DS

Notice how, after the map is moved, clicking on the marker will display different information: the latitude and longitude of the newly selected location.

Styling your maps

Map controls can be further customised by setting the width and height via CSS. Depending on how much space you assign to display the map, Ubiquity XForms will make decisions about which adornments should be displayed. It also initialises maps to a zoomed out, progressively less detailed view as their size is reduced. In the following example, the output control will be displayed with no adornments and zoomed out, whereas the range control will be displayed zoomed in with a full set of adornments. But because they are bound to the same instance data, they are just different views of the same location. Panning the map in the range will also cause the location of the output map to shift:

  1. <xf:model>
  2. <xf:instance>
  3. <location xmlns="">4 Pear Tree Court, London, EC1R 0DS</location>
  4. </xf:instance>
  5. </xf:model>
  6.  
  7. <xf:output ref="/location" class="geolocation" style="width: 100px; height: 80px;" />
  8.  
  9. <xf:range ref="/location" class="geolocation" style="width: 650px; height: 400px;" />
AttachmentSize
ubiquity-xforms-map-detail.jpg22.85 KB