Funciones MySQL para calcular distancia entre coordenadas.
drop function if exists fxToRad; drop function if exists fxDistanceBetween; DELIMITER $$ create function fxToRad(value float(30, 16)) RETURNS float(30, 16) begin return value * pi() / 180; END$$ DELIMITER ; DELIMITER $$ create function fxDistanceBetween(lat1 float(30, 16), lng1 float(30, 16), lat2 float(30, 16), lng2 float(30, 16)) returns float(30, 16) begin declare a float(30, 16); declare b float(30, 16); declare r float(30, 16); declare dLat float(30, 16); declare dLng float(30, 16); set r = 6371 * 1000; -- Radio de la tierra para obtener metros set dLat = fxToRad(lat2 - lat1); set dLng = fxToRad(lng2 - lng1); set a = power(sin(dLat / 2), 2) + ( power(cos(fxToRad(lat2)), 2) * power(sin(dLng / 2), 2) ); set b = 2 * atan2(sqrt(a), sqrt(1 - a)); return r * b; end$$ DELIMITER ;