Geoip: Add geoip_distance() native.

This commit is contained in:
Arkshine 2014-07-31 11:33:58 +02:00
parent 6941e406f4
commit 81d56dfdc0
2 changed files with 31 additions and 1 deletions

View File

@ -279,6 +279,19 @@ static cell AMX_NATIVE_CALL amx_geoip_longitude(AMX *amx, cell *params)
return amx_ftoc(longitude);
}
// native Float:geoip_distance(Float:lat1, Float:lon1, Float:lat2, Float:lon2, system = SYSTEM_METRIC);
static cell AMX_NATIVE_CALL amx_geoip_distance(AMX *amx, cell *params)
{
float earthRadius = params[5] ? 3958.0 : 6370.997; // miles / km
float lat1 = amx_ctof(params[1]) * (M_PI / 180);
float lon1 = amx_ctof(params[2]) * (M_PI / 180);
float lat2 = amx_ctof(params[3]) * (M_PI / 180);
float lon2 = amx_ctof(params[4]) * (M_PI / 180);
return amx_ftoc(earthRadius * acos(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1)));
}
void OnAmxxAttach()
{
@ -352,6 +365,7 @@ AMX_NATIVE_INFO geoip_natives[] =
{ "geoip_timezone" , amx_geoip_timezone },
{ "geoip_latitude" , amx_geoip_latitude },
{ "geoip_longitude", amx_geoip_longitude },
{ "geoip_distance" , amx_geoip_distance },
{ NULL, NULL },
};

View File

@ -150,4 +150,20 @@ native Float:geoip_latitude(const ip[]);
*
* @return The result of the geoip look up, 0 if longitude is not found.
*/
native Float:geoip_longitude(const ip[]);
native Float:geoip_longitude(const ip[]);
/**
* 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 = Metric(kilometers) or 1 = English(miles).
*
* @return The distance as result in specified system of measurement.
*/
#define SYSTEM_METRIC 0 // kilometers
#define SYSTEM_IMPERIAL 1 // statute miles
native Float:geoip_distance(Float:lat1, Float:lon1, Float:lat2, Float:lon2, system = SYSTEM_METRIC);