Solved

PHP - Get US State from Latitude & Longitude

Posted on 2011-09-08
2
363 Views
Last Modified: 2012-05-12
I need a function that conceptually is very simple in PHP but seems pretty complex the more I think about it.

I need a PHP function to return the US State that a particular Latitude and Longitude is in. I have access to a database of points that represent the boundaries of the states and there might be 50 points or so per State.

Is there a straightforward way of doing this or could someone get me started with the function? We have a MySQL database that I'll be quering and I can certainly put the data into a Memory table, but this function will be run many times and so needs to be efficient.

If it were a simple square it'd be straightforward to me, but the crazy sizes of the states give me pause.

I'm also open if there is an existing script and/or product out there to achieve this as well.

Any ideas?

Thanks!
0
Comment
Question by:khilles
2 Comments
 
LVL 20

Accepted Solution

by:
dsacker earned 500 total points
ID: 36504649
You may want to look into Google Geocoding API. For instance, there you can make a call for reverse address lookup (by longitude and latitude), as follows:
http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true_or_false

Open in new window

It will return quite an XML descriptor.

Check out http://code.google.com/apis/maps/documentation/geocoding
0
 

Author Closing Comment

by:khilles
ID: 36506380
Thanks. Its not an ideal solution, but it'll work.

For those trying the same thing, what I'm going to do is first create a table (one row per State) turning the State into a rectangle with absolute largest Lat/Long and Smallest absolute Lat/Long this will handle 90% of the scenarios when there is only one state within that rectangle. When there's overlap, I'll use Google Maps to get the actual State.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now