Solved

PHP - Get US State from Latitude & Longitude

Posted on 2011-09-08
2
364 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

863 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

23 Experts available now in Live!

Get 1:1 Help Now