Scott
Scott

Reputation: 4210

Sort List by latitude/longitude

So, I have a google map on my website. I need the user to be able to center the map somewhere, and then I want on my server side, to be able to sort a list of objects (that each have a lat and long property), based on the visible portion of the map on the front end. Does this make sense? Think the way Yelp.com sorts restaurants based on the map users can zoom in/out of.

So first the list would get rid of any objects that aren't in the region of the map based on their lat/long properties, and then it would sort the remaining based on which ones are closest to the center of the map.

To do this, what would I have to pass to the server from the front end, and once it's on the server, how would I do this sorting?

Upvotes: 4

Views: 2005

Answers (3)

Larry Watanabe
Larry Watanabe

Reputation: 10184

From the sound of it, you may want to store the tiles or whatever you are retrieving in something like a quadtree. Then you can pass the coordinates to the server, the server can retrieve the objects visible in that aperture for you, sort them, then send them back.

Upvotes: 1

winwaed
winwaed

Reputation: 7801

Use the Haversine Formula to calculate the great circle distance:

http://en.wikipedia.org/wiki/Haversine_formula

Then sort, with the shortest distance (=closest) first.

Upvotes: 1

Randy
Randy

Reputation: 16677

calculate the great_circle_distance, then sort by the smallest number.

Upvotes: 4

Related Questions