转载 Android计算地图上两点距离

在android中可采用如下代码获取距离:
Java代码 

    public double getDistance(double lat1, double lon1, double lat2, double lon2) {  
        float[] results=new float[1];  
        Location.distanceBetween(lat1, lon1, lat2, lon2, results);  
        return results[0];  
    }  

 

在其他设备若没有类似android的Location的distanceBetween方法开采用如下代码获取:
Java代码 

    double distance(double lat1, double lon1, double lat2, double lon2) {  
        double theta = lon1 - lon2;  
        double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2))  
                    + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2))  
                    * Math.cos(deg2rad(theta));  
        dist = Math.acos(dist);  
        dist = rad2deg(dist);  
        double miles = dist * 60 * 1.1515;  
        return miles;  
    }  
    //将角度转换为弧度  
    static double deg2rad(double degree) {  
        return degree / 180 * Math.PI;  
    }  
    //将弧度转换为角度  
    static double rad2deg(double radian) {  
        return radian * 180 / Math.PI;  
    }  

 这个计算得出的结果是英里,如果要转换成公里,需要乘以1.609344,若是海里需要乘以0.8684
posted @ 2011-09-14 23:49  小0  阅读(557)  评论(0编辑  收藏  举报