I have a database with ~3000 records and each one of them has a field called coordinates with the coordinates in a string, separated by a comma, for example:
coordinates: -32.23192381,51.32238190
And then I have some other coordinates:
coordinates2: -32.21093841,52.99193764
So the algorithm should:
1) Split the string into two pieces: latitude
and longitude
.
2) Get the distance using a function like this:
function getDistance(lat1, lng1, lat2, lng2){
radius = 6371
dLat = deg2rad(lat2-lat1)
dLng = deg2rad(lng2-lng1)
a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.sin(dLng/2) * Math.sin(dLng/2)
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
return radius * c
}
function deg2rad(deg){
return deg * (PI/180)
}
3) Return an array with the records where distance is less than 10km.
I'm using PHP for server and JS for client, so what would be faster:
1) Pass the records from the DB as JSON to javascript and then run the algorithm for every record.
or
2) Do that with PHP and only pass the final array with the records less than 10km.
Which will be faster?
Also, is this too slow? I'll never have more than 3000 records, but it seems to me like it would be slow. What do you think?