No, this isn’t one of those bet I can guess your weight or age games you find at places like carnivals and Six Flags. I bet you I can guess your approximate address with HTML and reverse geocoding.
See it in action!
Turning geographic data like a street address and zip code into geographic coordinates such as latitude and longitude is called geocoding
The process of turning geographic data like a street address and zip code into geographic coordinates such as latitude and longitude is called geocoding. When you do the opposite, turning coordinates into an address, it’s called reverse geocoding. See a live demo below:
I know, I know, it’s not perfect, but it’s a lot closer than we had which at most times was somewhere in the city, state and sometimes just country. And granted, if the user doesn’t allow us to grab their location we’re back to using the IP, but it’s still a pretty good guess. Now only if we could have got Osama Bin Laden to visit one of our webpages while logged on as himself… long shot, but wouldn’t that be awesome if that’s how it went down.
A Deeper Look
If you’ve already, I suggest you read my other post on HTML Geolocation to familiarize yourself with 90% of the code above. The rest is fairly simple to understand. You get the coordinates from
getCurrentPosition() , then pass them to a
printAddress() function, which uses the Google Maps API to perform the reverse geocoding.
printAddress() function begins by creating a new Google Geocoder object. The Geocoder object gives us access to the
geocode() method, which can take in a variety of options and return information based on them.
In this case, we’re using the
google.maps.LatLng() method to create a new Google LatLng object that is passed into geocode() in order to get the address. The
geocode() method is asynchronous, just like
The callback’s response contains two parameters, one for the results and the other for the status code. If the status is OK, then it’s safe to parse the array of
GeocoderResults objects stored in the results variable. The results variable is an array since Geocoder may return more than one entry.
Next, check for a
GeocoderResults object in the first position of the array and, if it exists, append the
formatted_address property to the defined results element.
For more information about reverse geocoding, check out Google’s documentation.
The possibilities are limitless with what you can do with this functionality. Internet marketers are biting at the bit to start using this technology to drive even better user-specific ads to the eyes of millions. Online game developers like Parallel Kingdom are building ground-breaking web-based massive multiplayer online (MMO) games. With a little extra code, we can turn our code into a mobile app that gives user’s the ability to send their location to a friend or family member with a click of a button. Our imagination has now become the limit with what we can build. The next fives years is sure to be an exciting time in the web development arena bringing the whole world a better internet. Are there any projects you’ve done you can share that use the HTML5 Geolocation API?