Cooking with OpenLayers Part I: Computed WKT to map multitple loc fields
This is the first part in a series of blog entries on how to solve Drupal mapping problems with the OpenLayers Module
I ran into a problem creating map views where I wanted to map multiple latitude and longitude fields that were created using the Locations module.

Openlayers only supports one source of latitude and longitude information, so how do we map multiple lat/lon fields?

Openlayers supports mapping multiple WKT fields at the same time. So what we need to do is use the Computed Field module to create some WKT fields from those latitude and longitude fields.
Computing WKT using Computed Fields
The first step is to add a computed field to the node type in question.

The next step is to see how to access latitude and longitude fields. To do that I have the following handy snippet that will dump all the data contained in $node to the screen. Place the following code inside the computed field php textarea. You should select "text" as the data storage option.
ob_start();
var_dump(&$node);
$result = ob_get_clean();
$node_field[0]['value'] = $result;
Now you can go to a node, and press 'edit' then 'save' to trigger the computed field. You should see the structure of the node outputted under "Computed WKT".
For this node type, I am using locations data that is picked by the user, and stored using the node location module (not cck). So by looking at the node data outputted from the code above, I can see that location data lives at &$node->locations[0]['locpick']['user_latitude'] etc. Referencing http://en.wikipedia.org/wiki/Well-known_text to see how to format WKT, I use the following code in my computed WKT field:
$lat = &$node->locations[0]['locpick']['user_latitude'];
$lon = &$node->locations[0]['locpick']['user_longitude'];
$wkt = "POINT($lon $lat)";
$node_field[0]['value'] = $wkt;
We are done with the first node type! Now I want to do the same thing with the second node type, except that this node type uses location cck fields, so our location data will be stored differently in the node. Again I dump the contents of the code using the code block above. This time the data is stored in &$node->field_cbr_org_ad_street[0]['latitude'] etc. Given that, we can computed WKT like so:
$lat = &$node->field_cbr_org_ad_street[0]['latitude'];
$lon = &$node->field_cbr_org_ad_street[0]['longitude'];
$wkt = "POINT($lon $lat)";
$node_field[0]['value'] = $wkt;
Now that we have our computed field in place, we need to calculate WKT for all the existing nodes in your database. We can use the following code do so (you will need to modify the content type specified by 'some_content' in the code). First back-up your database. Then place the following code in a page with 'input format' set to php and press 'preview' (no need to save it, we only want to run the php code once).
<?php
$res = db_query("SELECT n.nid FROM {node} n WHERE n.type = 'some_content'");
while ($n = db_fetch_object($res)){
$node = node_load($n->nid);
$n = node_save($node);
}
?>
We are done! We now have WKT for all our location fields!
Using OpenLayers in views to access both WKT fields
Now we can select both our WKT fields in the OpenLayers views options.

And now we have our map view, pulling data from multiple location fields!

