I’m trying to write a shell script that will accept a street address and output the latitude and longitude of that address, for geocache purposes. How can I do that?
This is a pretty interesting problem because while there are a variety of ways you can ostensibly get your latitude and longitude for a specific address from somewhere like Google Maps, the data isn’t always accurate and it’s also often pretty darn hard to figure out.
Heck, one of the most common ways to get lat/long from a Google Map is to cut and paste a Javascript snippet into your browser address bar. Ugh.
Fortunately, after much digging around, I found that there’s not only a nice API for Yahoo Maps, but that there’s a lazy way you can access it via a shell script.
The key piece is the Yahoo Maps URL:
Let’s step through how it works, using an address you might have bumped into a few times in the past, and then I’ll show how it can be turned into a simple Linux or Unix shell script.
YGeoCode.getMap({“GeoID”:”m”,”GeoAddress”:”1600 pennsylvania ave
washington
dc”,”GeoPoint”:{“Lat”:38.89859,”Lon”:-77.035971},”GeoMID”:false,”succes
s”:1},1);
The address is 1600 Pennsylvania Ave, Washington DC and I would like to think that everyone knows who lives there. Note that I’m using “lynx” to dump the result of that URL query to standard output and that the output is split across three lines.
The key data item here is GeoPoint because immediately following are the Lat and Lon values (in this case 38.89859 and -77.035971).
We can experimentally verify this by entering these two points into Google Maps too:
Yup, it’s the lat/long for the White House in Washington DC.
Back to the script, though. Using “lynx” (or “get” or “curl”) we now have a quick and easy way to convert a street address into a latitude / longitude pair. So there’s just stuff to wrap around it. For example, convert a simple address into a URL-friendly address. Pretty easy:
It’s a lazy way to accomplish this goal (e.g., turn spaces into “+” symbols), but let’s just keep moving and let it ride.
Next up, call the Yahoo API and save just the line that contains the GeoPoint data:
Finally, we have the line that contains the lat/long we want. How do we extract just that data? With my favorite Linux command: “cut”:
long=”$(echo $val | cut -d: -f4 | cut -d\} -f1)”
Now we’ve got it! All that’s left is to output it:
Put all the lines together and you now have a very simple command that does exactly what you seek:
38.89859,-77.035971
Hope that helps you out. Oh, and assembling all the discrete lines into a script is a simple task “best left for the reader”. 🙂
this makes no sense to me. i just got a Linux and im completely lost. youve made no attempt to tell me where i put the codes and how i even operate the terminal.
please simplify this
thanks.
Sorry, Chase. You need to open up a “terminal” window (probably called “xterm” on your Linux system) and launch an editor like emacs or vi to type in the code, line by line. You can use a window-oriented editor, but it’s critical it’s saved in “plain text” or “ascii” or “text only” format to work. Then you can invoke it from the command line as you would any other command program. Hope that helps!
confusion:
your first example displays lat to be equal to 38.89859 in one place and then after the line this line says echo “$long,$lat” to be equal to -77.035971 ! So which is it?
Thanks for the script in any case. 😉
Wow…. what a great way to trick google maps… I really love you little sweet code.. that worked out amazingly…
thank you so much for sharing this with us.
Harsh
sweet! thanks dude!