Solved

PHP - Get US State from Latitude & Longitude

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Creating and Managing Databases with phpMyAdmin in cPanel.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

730 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