HTML5 Geo - Show results nearby - via API data - location difference


I am making a simple app to return snow conditions via an API that returns JSON. I cache this in a database. I am using HTML5 Geo to get the users location and so far that works, returning long/lat/city. What I have been trying to do now, is say if you're in Portland, ME. Show resorts around you, say within 40 miles or so.

What is the best way to do that? Some type of math for long/lat to find a radius? Then sort through JSON?

Query DB
Return JSON to an object
Sort through object (client side or server?)
Return results within the users location


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

In order to do that you are going to have to have the latitude and longitude of all the resorts in the database.  Then comes a real problem which is calculating the distance. Latitude is a no brainer 1.15 miles for each minute of latitude.  But longitude is variable from zero to 69 miles per degree as you move from the poles to the equator.  

The setup for the math is something like this

Then: (pi / 180) * R * cos(A)  where R is the radius of the earth in miles and A is
the degree latitude.  The radius of the earth is 3963.1676 miles.

Doing the math on the computer is not the real challenge.  The hard part is trying to figure out how to select appropriate resorts to do the calculation on.  You will need some kind of regional classification coding associated with each resort so you can filter the query otherwise you will have to do the calculations on every row in the DB for every request and performance could be really terrible.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
catonthecouchproductionsAuthor Commented:
Thats what I was thinking. The API does return both longitude and latitude. Which is a huge help.

catonthecouchproductionsAuthor Commented:
Could I break up the queries?

Grab location -> Get state -> Only return say Maine -> calculate distance?

Or is there an easier way? Also, the trick with the above is I have the JSON stored in a mysql table:


Would that make it harder to sort? I would have to manipulate the array and sort/remove say state X

Not sure if thats the best way.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

If the data that is needed is stored in the JSON data; then it will have to be parsed out so it can be referenced in the query. the PHP and query look a little complex and it will probably need someone better at PHP/mysql than me, and having to parse the JSON before it goes into the database also requires a fair amount of skill to get it right.


catonthecouchproductionsAuthor Commented:
Could I query the DB:

Grab the row with the highest id (newest)
Store object for use
Filter through based on state (deconstruct JSON, GET state via param)
Calculate distance
Send back

I don't know, I don't really work with JSON.  When I have had to process JSON packages I generally am using some snippet design for the particular task.  I think you are going to have to pull out the latitude  and longitude as well.  The calculations need to be done server side; I think.  They can be done client side with Javascript, but JS is not the world's best number cruncher especially with float arithmetic.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.