DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. A Simple Example of Reverse Geocoding

A Simple Example of Reverse Geocoding

Raymond Camden user avatar by
Raymond Camden
·
Mar. 06, 13 · Interview
Like (0)
Save
Tweet
Share
7.06K Views

Join the DZone community and get the full member experience.

Join For Free

have you ever seen a web site that seemed to know where you were located? i'm not talking about a map, but the actual name of the location? this is done with a process called "reverse geocoding." whereas geocoding refers to translating a human-readable address into a longitude/latitude pair, reverse geocoding is, well, the opposite of that. given a longitude/latitude pair, what would be the description of that location. in this blog post i'll show a simple example of this process. my example application will attempt to report the city and state you live in.

once again i'll be making use of google for my example. one of the services of the google maps api is geocoding as well as reverse geocoding . their reverse geocode api needs a longitude and latitude, we can get that easily enough using geolocation. here is a snippet of code that begins the process:

$(document).ready(function() {
 
  //i'm not doing anything else, so just leave
	if(!navigator.geolocation) return;
	
	navigator.geolocation.getcurrentposition(function(pos) {
		geocoder = new google.maps.geocoder();
		var latlng = new google.maps.latlng(pos.coords.latitude,pos.coords.longitude);

please note that in this demo, if the user doesn't support geolocation i'm not going to do anything else. they won't get an error though and won't know what they are missing.

once we have the longitude and latitude, we then fire off a request to the geocode service.

geocoder.geocode({'latlng': latlng}, function(results, status) {
	if (status == google.maps.geocoderstatus.ok) {
        	//we'll do cool crap here
	} 
});

as with our initial geolocation support, all i care about here is a success. if anything goes wrong, i don't care and i just ignore it.

so, here comes the difficult part. the result was from the geocode call is fairly complex. you get an array of results ordered by the quality of the match. if you check the docs , you can see an example of this. each individual result is also fairly complex. you get an array of address parts that represent, obviously, parts of an address. if you read the "address component types" section of the docs, you can see an explanation of the types of address parts. each part has one or more of these types applied as a tag.

based on my reading of the spec, i determined i could get the city when the tag was "locality" and the state when the tag was "administrative_area_level_1." this is us-centric and i've not done any testing yet with other countries.

given that i knew which tags to look for, i decided to work with the first result and see if i could match those tags:

//check result 0
var result = results[0];
//look for locality tag and administrative_area_level_1
var city = "";
var state = "";
for(var i=0, len=result.address_components.length; i<len; i++) {
var ac = result.address_components[i];
if(ac.types.indexof("locality") >= 0) city = ac.long_name;
if(ac.types.indexof("administrative_area_level_1") >= 0) state = ac.long_name;
}
//only report if we got good stuff
if(city != '' && state != '') {
$("#result").html("hello to you out there in "+city+", "+state+"!");
}

again, if there isn't a match i don't throw an error. since this is simply window dressing for the site it doesn't really matter if we don't get a match. want to see this in action? check out the demo below.

Google Maps Web Service Google (verb) Doc (computing) Data structure application Snippet (programming)

Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Microservices Discovery With Eureka
  • API Design Patterns Review
  • What Is a Kubernetes CI/CD Pipeline?
  • 7 Awesome Libraries for Java Unit and Integration Testing

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: