Return to Snippet

Revision: 33850
at October 14, 2010 07:25 by rnickel


Initial Code
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;
    }

Initial URL
http://www.ryannickel.com/2010/10/how-to-calculate-the-distance-between-two-latlon-points-on-the-blackberry/

Initial Description
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.

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

Initial Tags


Initial Language
Java