Reputation: 880
I have a page that loads a lot of markers in a map (Google Maps).
The markers when clicked open InfoWindows that have "previous" and "next" links, which the user can use to navigate the markers. Right now I'm not using any particular order and the map moves all over the place when the next/prev marker is too far.
What I'd like to do is to use the coordinates in some way that the query already returns the results ordered by proximity (some combination of lat and long) so that the map moves "less" when the user moves from one marker to the next.
I may or may not have a central point of reference to calculate the proximity/distance from. Do I need one?
EDIT(some more info): Imagine that I have a query such as:
select id, name, lat, long from whatever;
I'd like to have something in the lines of:
select id, name, somecalc(lat, long) as coord from whatever order by coord;
So when I process this query in my code and generate the markers, each "prev" and "next" link (which I build as a linked list) would have markers the closest to them as possible. What I need is the somecalc
above.
Upvotes: 0
Views: 1022
Reputation: 7732
Good old Pythagoras is your friend here: Let's say we start from coords (start_lat, start_long). This could be a random first result for example. If you want to order your results by distance to that coordinates you simply have to
SELECT
id, Name,
SQRT(POW(start_lat - lat, 2) + POW(start_long - Long, 2)) AS distance
FROM
whatever
ORDER BY
distance
Upvotes: 1