Location, location, location

After my two posts on extracting photo metadata using Shortcuts (the naive one and the improvement), Vinay Kashyap asked me on Twitter if I knew of a way to get the latitude and longitude of a photo. I didn’t, but with a little JSON exploration, I figured it out. And now I’ll inflict it on you.

First, you can’t use the Location property of the photo.1 Here’s a Share Sheet shortcut that tries that, where the input is restricted to Images and the contents of the Show Result step is the Location property of the Shortcut Input magic variable.

Photo Address shortcut

The Location property typically returns a street address, although in some of my tests, on photos taken way out in national or state parks, it returned just a city and state. But whatever it returns, it isn’t the latitude and longitude. For that we need to use the Metadata Dictionary property, which you’ll find if you scroll about two-thirds of the way down the property list.

Choosing the Metadata Dictionary item of photo

When you choose the Metadata Dictionary, you’ll get an opportunity to choose which item of this dictionary to return. But in contrast to selecting a photo property, you don’t get a list to select from—you have to know the name (or key) of the dictionary item to return. The key we want is {GPS} (yes, with the curly braces).

Choosing {GPS} item of Metadata Dictionary

How do you know which key name to use? You don’t until you understand the dictionary structure. If we leave the key field blank, this step will return the entire dictionary in JSON format. Here’s an example, where I’ve used indentation to make the dictionary structure easier to see:

    "DateTime":"2018:10:24 09:27:26",
    "Model":"iPhone XS",
    "DateTimeOriginal":"2018:10:24 09:27:26",
    "DateTimeDigitized":"2018:10:24 09:27:26",
    "LensModel":"iPhone XS back dual camera 6mm f\/2.4",
  "ProfileName":"sRGB IEC61966-2.1",

As you can see, this is a complex nested data structure. The latitude and longitude are in a subdictionary—a dictionary within a dictionary—whose key is {GPS}.2 The {GPS} dictionary also includes some cool stuff like the altitude and the direction the camera was pointing, but we’ll leave them aside for now.

OK, so now that we know which item of the Metadata Dictionary to select, how do we pull the latitude and longitude out of it? The best way I found was to save the Metadata Dictionary as a variable called GPS and then select the values of that dictionary using the key names I found in the JSON.

GPS dictionary

Putting it all together, we get this simple three-step shortcut

Photo Coordinates shortcut

that returns the latitude and longitude of the photo’s location.

Photo Coordinates result

The simplicity of the shortcut stands in contrast to the complexity of the Metadata Dictionary and glosses over the JSON spelunking needed to figure out where the data was hiding. But now that we have one example, we can use it to pull out all kinds of metadata. I’m especially curious about the {MakerApple} subdictionary, with its mysterious numerical key names.

  1. If it were that easy, Vinay wouldn’t have asked. 

  2. I assume the braces are used in the key name as a mnemonic to indicate that the value is a dictionary.