Solved

PHP - Get US State from Latitude & Longitude

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
This article discusses how to create an extensible mechanism for linked drop downs.
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 count occurrences of each item in an array.

776 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