# Posted By

rnickel on 10/14/10

# Statistics

Viewed 701 times
Favorited by 0 user(s)

# How to calculate the distance between two lat/lon points on the blackberry

/ Published in: Java  Yes, I realize that this won't work with j2me, but RIM has implemented some good helper classes to make this calculation much easier.

This calculation uses the Haversine formula.

Copy this code and paste it in your HTML
`public double calculateDistance(double lat1, double lon1, double lat2, double lon2)    {               try{            double R = 6371; // km             double dlat = Math.toRadians(lat2 - lat1);            double dlong = Math.toRadians(lon2 - lon1);             double a = Math.sin(dlat / 2.0) * Math.sin(dlat / 2.0) +                        Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))  *                        Math.sin(dlong / 2.0) * Math.sin(dlong / 2.0);            double num2 = Math.sqrt(a);            double num1 = Math.sqrt(1-a);            double c = 2 * MathUtilities.atan2(Math.sqrt(a), Math.sqrt(1-a));            double d = R * c;             return d;         } catch(Exception e){            e.printStackTrace();        }        return 0;    }` Subscribe to comments