>>Some things come to mind
>>
>>(1) They may also store the lattitude in radians to speed up the calculation
>>(2) They may have a table that stores all the distances (and update it within a trigger if the coordinates of a city are changed/added)
>>eg 10,000 cities would be a table of 10,000 * 10,000 / 2 records = 50,000,000 records
>
>I am looking at this right now:
>
>
http://msdn.microsoft.com/en-us/library/ms980211.aspx>
>As for point 2, in order to know a distance, we need two set of coordinates. So, this can only be known when the user do the search. So, basically, calculation applies at search time only. In the page I have added here, there is a SQL Server code to do this so I am trying it now. But, the more records there is, the more time it will take.
So they precalculate Xaxis, Yaxis and Zaxis for each location
As to point (2) it's possible if you have a table
FK_city1
FK_City2
Distance
The query is then simply:
select *
from DistanceTable
where (FK_Where_I_Am in (FK_city1, FK_city2))
and (Distance < = 50.0)
The result needs to be joined
- with the city table on FK_City2 if (FK_Where_I_Am == FK_city1), and
- with the city table on FK_City1 if (FK_Where_I_Am == FK_city2)
Can be done with a union
select *
from DistanceTable
join CityTable on (pk_City = FK_City2)
where ( FK_city1 = FK_Where_I_Am )
and (Distance < = 50.0)
union all
select *
from DistanceTable
join CityTable on (pk_City = FK_City1)
where ( FK_city2 = FK_Where_I_Am )
and (Distance < = 50.0)
Of course, you never store the distance from a city to itself
You need to find a way to store the distance between two cities only once
Gregory