Using this function you can easily reverse geocode an address into a lat and lang position. This uses the jq executable and the Google Maps API.
Requirements
This little snippet requires jq to be installed. It is very easy to install.
From the site:
jq is a lightweight and flexible command-line JSON processor
It is multi-platform, so no worries for Windows users.
Here is the meat:
Function
function reverse-geocode() {
# replace spaces with + signs
STRING=$(echo $1 | tr ' ' '+')
# save results
CURLED=$(curl "http://maps.googleapis.com/maps/api/geocode/json?address=$STRING&sensor=true")
# save lat and lng
LANG=$(echo $CURLED | jq '.results[0].geometry.location.lng')
LAT=$(echo $CURLED | jq '.results[0].geometry.location.lat')
# echo them out
echo "Lat: $LAT, Lang: $LANG"
}
Usage:
reverse-geocode "998 Oxford Street E, London ON, N5Y 3K7"
This return the curl results as well as the Lat and Lang output for the location.
James Doyle
I'm a full-stack developer, co-organizer of PHP Vancouver meetup, and winner of a Canadian Developer 30 under 30 award. I'm a huge Open Source advocate and contributor to a lot of projects in my community. When I am not sitting at a computer, I'm trying to perfect some other skill.