# Extracting coordinates from Apple Maps

A couple of years ago, I wrote a script called coordinate that inserted latitude and longitude into the metadata of photographs I took with my regular, non-GPS-equipped, camera. I set it up to work two ways:

1. If I had a photo that already included GPS coordinates (one taken with an iPhone, for example), the script would extract that information and apply it to the non-GPS photos.
2. If I didn’t have such a photo, I could provide the latitude and longitude on the command line.

At the time, the easiest way for me to get latitude and longitude (if I didn’t have an iPhone photo with it) was to go to Google Maps, find the spot where the photos were taken, and right click on it. Both the Drop LatLng Marker (enabled through Maps Labs) and the What’s Here context menu items will show the coordinates.

The thing is, I’ve come to hate Google Maps. I know it has more and better information than Apple Maps, but its user interface has gotten worse over the years as Google has tried to squeeze more functionality into what is, when all is said and done, still just a web page. And I’m sick of having Google+ pushed at me.

So I’d prefer to get the coordinates from Apple Maps. Apple, of course, likes to hide such complicated technical information from its users, but there’s a way around the obfuscation. Here are the steps to get the latitude and longitude:

1. Right click to drop a pin at the spot you’re interested in.
2. Click the little info button on the pin’s flag.
3. Click the share button on the info window that pops up and choose from the context menu.
4. Right click on the Map link in the message window and choose from the context menu.

You now have a URL on your clipboard that looks something like this:

http://maps.apple.com/?q=41.772999,-88.150383&sspn=0.002885,0.006427&sll=41.772891,-88.149888


The coordinates of the pin in lat,long form is the value of the q field in the query string.

Even if I could remember this series of steps flawlessly, it still doesn’t give me the raw coordinates of the point. I have to paste the URL somewhere and select the coordinates.

I’d like to write an AppleScript to extract the coordinates of the pin, but the AppleScript library for Maps is, for all practical purposes, empty, and UI scripting is never going to work with all the clicking that’s necessary.

Keyboard Maestro to the rescue. I put together a macro that does all of the above steps and pulls out just the pin coordinates from the URL. It solves the problem of where to click by allowing you to specify an image to aim at. Here, for example, is the step for clicking on the pin to show its flag:

By restricting the target image to just the inside portion of the pin, this works in Standard, Satellite, and Hybrid view—the clutter around the pin doesn’t screw up Keyboard Maestro’s “aim.”

The extraction of the coordinates from the URL is done through a simple shell pipeline at the end of the macro:

The pipeline is

pbpaste | sed -E 's/^.+\?q=([0-9.,-]+).+/\1/'


which grabs the two comma-separated numbers after the q= and puts them on the clipboard. Passing the -E option to sed makes its regular expression syntax more like the Perl/Python syntax I’m used to. If it weren’t for that option, I’d find sed completely unusable.

If you’re interested in this macro, I put a zip file of it on my server. A few of the click actions are on popups that don’t appear instantly, so there are pauses at certain steps to insure the popups’ presence. You may need to adjust the lengths of these pauses to make the macro run smoothly on your system.