Map your images with Mapquest

As most digital camera users know, there's actually quite a bit of information that goes along with each image. Known as the exchangeable image file format (Exif), this specification stores various bit of information (or meta data) along with the image, such as date/time and camera settings (e.g. shutter speed, ISO speed, focal length, etc).

While some of that information is rather dry, an often overlooked and very useful tag is 'GPSPosition'. This tag that allows you to store geographic parameters in your image. Now you'll never forget where you took that once-in-a-lifetime picture. In the Web 2.0 world, it means you can show folks on a map where you took that picture.

I think it's pretty cool that the folks who came up with the Exif specification had the foresight to include a tag for location, but the reality is most digital camera don't have a GPS unit built-in. To add geographical information (or geotag/geocode), you'll have to use a 3rd party service. In this example I've already used the Geotagger at my site SundayMorningRides.com to add geographical coordinates to 'image.jpg'. Now I'll show how to use the Mapquest Open API to create a custom map that displays the location of my images. All I do is read the location information tag from the Exif header ('GPSPosition'), do a bit of conversion of the heading notation, and use the setLatitude and setLongitude elements from the Mapquest API to create a marker at the location.
#!/usr/bin/perl
use Image::ExifTool;
my $exifTool = new Image::ExifTool;

my $info = $exifTool->ImageInfo('image.jpg', 'GPSPosition');
@temp = split (/, /, $info->{'GPSPosition'});

# convert heading to decimal notation
if ($temp[0] =~ m/S/) {
    $lat = dms2dd($temp[0]) * -1;
} else {
    $lat = dms2dd($temp[0]);
}
if ($temp[1] =~ m/W/) {
    $long = dms2dd($temp[1]) * -1;
} else {
    $long = dms2dd($temp[1]);
}

sub dms2dd($) {
    my ($dms) = @_;
    $dms =~ /(\d+) deg (\d+)' ([\d.]+)"/;
    my ($degrees, $minutes, $seconds) = ($1, $2, $3);
    my $dd = $degrees + $minutes/60.0 + $seconds/3600.0;
    return $dd;
}

($webtext =  <<"WEBTEXT");
<html>
<head>
    <script src="http://web.openapi.mapquest.com/oapi/transaction?request=script&key=YOUR-API-KEY-HERE" type="text/javascript"></script>
</head>
<body>
<div id='myMap' class='myMap' style="height:600;width:650"></div>
<script type="text/javascript">
    var mq = new MQMap("myMap");
    var loc1 = new MQLocation();
    loc1.setName("Location Name");
    loc1.setLatitude("$lat");
    loc1.setLongitude("$long");
    mq.locations.add(loc1);
    mq.getMap();
</script>
</body>
</html>
WEBTEXT
print "Content-type: text/html\n\n";
print "$webtext";

You'll get a map with a marker at the coordinates of the image. You can learn more about how to further customize the map at the Mapquest Open API page.

Geography is quickly becoming a standard feature of Web 2.0. Did you know you can also geocode RSS feeds?

 


Enable the Subscriptions block here!