Nice, I plan on trying this out....
Suggestion/Request: Add flag to return distance in either kilometers or miles.
I don't know if this is right but highlighted is what i changed. (Notes after snippet)
Code:
/**
* Calculate the distance between geographical coordinates, latitude and longitude.
*
* @param lat1 The first IP latitude.
* @param lon1 The first IP longitude.
* @param lat2 The second IP latitude.
* @param lon2 The second IP longitude.
* @param system The system of measurement, 0 = Meteric(kilometers) or 1 = English(miles).
* @return The distance as result in specified system of measurement.
*/
native Float:geoip_distance( Float:lat1, Float:lon1, Float:lat2, Float:lon2, system = 0 );
Native defaults to kilometers if not specified.
Code:
static cell AMX_NATIVE_CALL amx_geoip_distance
( AMX *amx, cell *params
)
{
REAL RAD_CONVERT = (REAL)0.017453292519943;
REAL EARTH_RADIUS = (REAL)params[5] ? 3959.0 : 6371.0;
REAL lat1 = amx_ctof
( params
[1] ) * RAD_CONVERT;
REAL lon1 = amx_ctof
( params
[2] ) * RAD_CONVERT;
REAL lat2 = amx_ctof
( params
[3] ) * RAD_CONVERT;
REAL lon2 = amx_ctof
( params
[4] ) * RAD_CONVERT;
return amx_ftoc
( EARTH_RADIUS * acos
( sin
( lat1
) * sin
( lat2
) + cos
( lat1
) * cos
( lat2
) * cos
( lon2 - lon1
) ) );
}
I used
0.017453292519943 instead of
M_PI / 180 mainly because that is what xs uses for xs_deg2rad.
6371.0km is a bit more precise as it is the earth's mean radius not the equatorial radius of 6378.2km. (3959.0 is just the miles conversion of 6371.0)