0

I have 2 tables 1 is for facility and 1 is for customer. both contained latitude longitude we want query to fetch customer's available with in 1 miles of facility. We don't want to use postgres function like ST_Distance. Any alternate query to achieve it.

Praj Patil
  • 31
  • 2
  • just transpose in sql the formula given by [https://stackoverflow.com/a/11172685/8060017](https://stackoverflow.com/a/11172685/8060017) – Edouard Jan 02 '23 at 17:11
  • Thankyou Edouard, We used haversine formula to calculate distance in miles. – Praj Patil Feb 06 '23 at 07:23
  • private double distance(double LatOne, double LonOne,double LatTwo, double LonTwo) { LonOne = Math.toRadians(LonOne); LonTwo = Math.toRadians(LonTwo); LatOne = Math.toRadians(LatOne); LatTwo = Math.toRadians(LatTwo); double deltaLon = LonTwo - LonOne; double deltaLat = LatTwo - LatOne; double formula = Math.pow(Math.sin(deltaLat / 2), 2)+ Math.cos(LatOne) * Math.cos(LatTwo)* Math.pow(Math.sin(deltaLon / 2),2); double fOutput = 2 * Math.asin(Math.sqrt(formula)); return (fOutput * 3956) ; } – Praj Patil Feb 06 '23 at 07:28

1 Answers1

0

Iterate java list of location and used haversine formula to calculate distance in miles

    private double distance(double LatOne, double LonOne, double LatTwo, double LonTwo) {
        LonOne = Math.toRadians(LonOne);
        LonTwo = Math.toRadians(LonTwo);
        LatOne = Math.toRadians(LatOne);
        LatTwo = Math.toRadians(LatTwo);     
        double deltaLon = LonTwo - LonOne;
        double deltaLat = LatTwo - LatOne;
        double formula = Math.pow(Math.sin(deltaLat / 2), 2)+ Math.cos(LatOne) * Math.cos(LatTwo)* Math.pow(Math.sin(deltaLon / 2),2);
        double fOutput = 2 * Math.asin(Math.sqrt(formula));        
        return (fOutput * 3956) ;
    }
AlexK
  • 2,855
  • 9
  • 16
  • 27
Praj Patil
  • 31
  • 2