Introduction
This Windows Desktop application takes a picture as an input parameter and if that picture has GPS meta data, it will open the location that picture was taken in Google Maps (configurable to other mapping sites or applications). Smart phones seems to do this well, but once you have copied the picture to your desktop/laptop, it is somewhat difficult to see where the photo was taken. If the image does not have GPS data encoded in its metadata, or the file type passed in is not an image, a pop-up error explains the problem. To make using this easier, leave it on your desktop and drag and drop photos on to it, or wire it up as an alternative opener for photo files (see sample image).
Background
Almost all of the code to make this happen is available elsewhere, either in MSDN or on CodeProject, this just ties all the pieces together into a usable package. Some notable resources:
ExIF Wiki[^],
Google Maps API[^],
GeoCode Conversion[^],
Image Properties API[^]
Using the Code
The code should compile and run without intervention. If you would like to extend the code for your own use, focus on the two classes that encapsulate most of the work, Coord.cs and GeoPoint.cs.
The key points to note:
- On startup (Program.cs), we parse the command line for the path to a valid picture file.
- In
GeoPoint.GetFromImageFile()
, we parse the image meta data for GPS details.
- Once we have the GPS details, we use the
Coord
functions to convert them to proper Longitude
and Latitude
.
- After we have the
Longitude
and Latitude
, we construct a URL to open Google Maps to that location. Note that you can alter the URL to use any other mapping site, or since you have the coordinates, you could pass that to any mapping API of your choice.
- We launch the URL using the default browser.
Extending The Code
Since I thought someone might ask about adding this to the Windows Explorer context menu for pictures, it is easy to do with
SharpShell[
^] (very cool project - thanks SharpShell team!). You can read the SharpShell documentation for details, but ultimately it will entail adding a ToolStripMenuItem to the shell context menu. For brevity, the error handling and debugging hooks have been removed.
private ToolStripMenuItem GetShowImageLocationMenu()
{
string selected_file = SelectedItemPaths.FirstOrDefault();
if (IsImage(selected_file))
{
var imageLocationRootMenu = new ToolStripMenuItem("Show Picture Location");
imageLocationRootMenu.ToolTipText = "Show location picture was taken on a map.";
imageLocationRootMenu.Click += new EventHandler((o, e) =>
{
string messages = ShowPictureLocationLib.ShowPictureLocationHelper.ShowLocation(selected_file);
if (!String.IsNullOrEmpty(messages))
{
Msg.Box(messages, "Alert", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
});
return imageLocationRootMenu;
}
return null;
}
public static bool IsImage(string path_to_file)
{
if (String.IsNullOrEmpty(path_to_file) || !File.Exists(path_to_file))
{
return false;
}
string ext = (Path.GetExtension(path_to_file) ?? "")
.TrimStart('.')
.ToLower();
return (new string[] { "jpg", "jpeg", "png", "tiff", "gif" })
.Contains(ext);
}
History
- 10 October 2013 - Initial draft (1.0).
- 19 October 2013 - Updated source breaking most of the code into a reusable library. Added support for storing the map URL in a config file. Improved error management.
- 20 October 2013 - Updated with details on adding to Windows Explorer context menu.
- 21 October 2013 - Fixed bug were I was using Longitude in place of Latitude, terribly sorry about that.
- 11 December 2013 - Updated some notes in the article. Code is unchanged.