JoeTech.com



### Contact | About Joe Tech | Advertise | Get Reviewed | Disclosure | Tools
Search:

How To Get A Zip Code From Latitude And Longitude On A Mobile Phone

Posted in Code,Software,web by Joe Colburn on the June 11th, 2012

If you develop web applications for mobile devices, you may find yourself needing to get the visitor’s location. On a desktop browser, this can be determined by the IP address of the computer connecting to your application, but this method is far less useful on a mobile device where the location attached to the IP address is often far from the location of the device. In this post, I will describe how to first get the device’s GPS latitude and longitude and then how to use that to determine the device’s zip code for use in forms or elsewhere.

Google maps

Get Latitude And Longitude From The Phone’s GPS

Newer versions of Safari (IOS) and Chrome (Android) allow you to request the device’s GPS coordinates. As long as the user agrees, position information is supplied, including the device’s latitude and longitude which you can access with a little bit of JavaScript as follows:

When I used this in a recent app, I placed the code inside a function and called the function when the page loaded. The two console.log() calls are only for viewing the values in the console, so when you use this code in your application, you will probably want to pass that data on to another function, instead. In my case, I used an AJAX call to a PHP file that performs the query shown in the next step, below.

Translate Latitude And Longitude Into A Zip Code

So you’ve got the JavaScript from the previous step all working, but your application needs a zip code rather than latitude and longitude? Not a problem. From here, you just need to query a gis database that matches zip codes to latitude/longitude pairs. If you don’t have a gis database, you can download them from GeoNames.org. The key here is to just grab the closest match as your user’s latitude and longitude will most certainly not be an exact match to what’s in the database. To accomplish this, you just need to perform a little math within your MySQL query like I’ve done below:

Here’s how the query works… You want the difference between the database latitude and your user’s latitude and the difference between the database longitude and your user’s longitude by subtracting the user’s latitude from the database latitude and doing the same for longitude values. Then you want to combine these by adding the differences together to get your over-all difference in proximity between the zip code’s location and the user’s location. The shortest over-all distance is the user’s zip code. In the above query, the CASE statements turn any negative into a positive so that your difference will always end up as a positive number. The ORDER BY sorts the results to have the closest zip codes first, and the LIMIT restricts the result set to just one zip code.

Putting It All Together

The concept above is fine for understanding how it works, but it’s really missing some things it needs for it to all work together. Seasoned developers should be able to use the concepts above in their own code, but for beginners (and the impatient), I’ve included all the code below (minus a third-party database class from the Inexo framework I wrote this in).

zip.php:

zip.php (or index.html or whatever else you want to name this file) interfaces with the browser to request the latitude and longitude, which it passes to ajax.php. You will likely need to just pull the JavaScript from this file and use it in your existing file.

ajax.php

(assumes the use of external $db class, but connect however you normally do)

ajax.php is assumed to sit in the root of the website. In this file, I use a switch statement and “action” variable which will allow me to use this file for other AJAX needs as well, but it’s completely optional.

That’s all there is to it. Try it out yourself and comment on your results below.

About the author

Joe Colburn Joe Colburn is a software engineer specializing in PHP and a technology enthusiast. Always eager to dive into new and exciting things, Joe writes about anything technology related news and products that he thinks you will also be excited about. Find Joe Colburn on Google+ or by any of the links below.

If you liked this Joe Tech article, subscribe to the RSS feed for daily updates.

Submit this story to:

Sponsor




Events

Find me at any of the following events.

No upcoming events

Sponsors

Dedicated server hosting by Codero

led lighting

  • DocWhovian
  • Advertise Here

Need Help


Friends and Sponsors

 
Popular Posts
 • How to Crack the Account Password on Any Operating System
 • How To Send An Anonymous Text Or MMS Message
 • How To Replace A Dell Inspiron Laptop Heat Sink And Fan Assembly
 
More Popular Posts
 • 5 Geeky Ways to Say “I Love You” on Valentine’s Day
 • My Sony Vaio VGN-SZ430N Hard Drive Crashed and How I Fixed It
 • Swiss Mini Gun is Tiny, Lethal, and Expensive
 
© 2006 - 2013

Joe Colburn