Forward Geocode Shell Script Lat-Lon from address

Simple Script to get Latitude and Longitude from an address

A Simple Script to get the Latitude and Longitude (forward Geocode values) from an address using the MapQuest API

I recently found myself needing a way to get Latitude and Longitude coordinates from a common street address in order to use as an input to another script I would like to use. This topic was covered some time ago by Dave Taylor in Linux Journal, but used the yahoo maps API which no longer exists. At ten years on since that version, I’ve updated the script for the MapQuest API with same results. You can find Dave’s original article here.

Like the deprecated Yahoo Maps API, the MapQuest API uses a URL which will accept a request with a location and a API key, then will return an XML object which includes the Geocoded values.

Before you get started, you will need to go to and creatre a developer account in order to get a free API key. Once you have your key, we can try some queries at the command prompt. If you’re on Windows, you can use the Cygwin terminal, on Linux use your favorite terminal. I’m using Bash on Windows, and we’re going to use curl to fetch the URL.

First we’ll set some variables up:

Type: url=''
Then:  address='Jupiter+FL'
Then: key='YourKeyString'
And finally a location argument: args='&location='

So let’s try it out: curl $url$key$args$address
Which returns:

{"info":{"statuscode":0,"copyright":{"text":"\u00A9 2018 MapQuest, Inc.","imageUrl":"","imageAltText":"\u00A9 2018 MapQuest, Inc."},"messages":[]},"options":{"maxResults":-1,"thumbMaps":true,"ignoreLatLngInput":false},"results":[{"providedLocation":{"location":"Jupiter FL"},"locations":[{"street":"","adminArea6":"","adminArea6Type":"Neighborhood","adminArea5":"Jupiter","adminArea5Type":"City","adminArea4":"Palm Beach County","adminArea4Type":"County","adminArea3":"FL","adminArea3Type":"State","adminArea1":"US","adminArea1Type":"Country",<br/>"postalCode":"","geocodeQualityCode":"A5XAX","geocodeQuality":"CITY","dragPoint":false,"sideOfStreet":"N","linkId":<br/>"282039075","unknownInput":"","type":"s","latLng":{"lat":26.934145,"lng":-80.099736},"displayLatLng":{"lat":26.934145,"lng":-80.099736},"mapUrl":",160&locations=26.934145,-80.099736|marker-sm-50318A-1&scalebar=true&zoom=12&rand=1190609421"}]}]}

Whoa- lot of info there, but you can see we do have a forward geocoded latitude and longitude in the XML data (26.934145,-80.099736 for Jupiter, Florida). From here, all we need to do is write a simple script and remove the extraneous data we don’t want or need.

I named my script and it is pretty much the same script Dan wrote except for the API call, field numbers cut, and key variable.
addr="$(echo $* | sed 's/ /+/g')" curl -s "$converter$addr" | \
cut -d\" -f117,119 | \
sed 's/[^0-9\.\,\-]//g;s/,$//' exit 0

If you’d like to know all the details of each line, have a look at the original article on the site. You can run it by simply typing: ./ and an address.

./ 8865 SE Bridge Road, Hobe Sound, FL

Which returns:

And this is exactly the information I need to have in order to add Geocoding to existing JPG files. I’ll cover that in a later topic (with a adjusted script for Windows)- or you can read the original article by Zsolt Hajdo on Linux Journal here.

If you'd like to read the article on how to Geotag your photos using this, and another script, read on here.

Comments (0)

Add a Comment

This thread has been closed from taking new comments.