找到两个地理点之间距离的源代码

时间:2011-11-02 09:06:04

标签: android gps haversine

我正在开发Android应用程序,我想添加商店定位器详细信息和地图视图。我搜索了很多。但我没有得到任何有目的的东西。我紧张,非常需要你的帮助。

问题是,我想找到2个坐标之间的距离,我有一个Haversine公式。但是不知道如何成功运行程序。,因为我是Android的初学者,请帮助按步骤进行编码,即xml代码..,请。,

4 个答案:

答案 0 :(得分:4)

如上所述,位置等级是要走的路。

这是我用过的代码:

Location locationA = new Location("point A");  

locationA.setLatitude(pointA.getLatitudeE6() / 1E6);  
locationA.setLongitude(pointB.getLongitudeE6() / 1E6);  

Location locationB = new Location("point B");  

locationB.setLatitude(pointB.getLatitudeE6() / 1E6);  
locationB.setLongitude(pointB.getLongitudeE6() / 1E6);  

double distance = locationA.distanceTo(locationB);

在此示例中, pointA pointB 都是GeoPoint类的实例。

答案 1 :(得分:2)

来自MotoDev片段(GPS坐标之间的距离):

Location originLocation = new Location("gps");
Location destinationLocation = new Location("gps");
originLocation.setLatitude(originLatitude);
originLocation.setLongitude(originLongitude);
destinationLocation.setLatitude(originLatitude);
destinationLocation.setLongitude(originLongitude);
float distance = originLocation.distanceTo(destinationLocation);

答案 2 :(得分:0)

http://developer.android.com/reference/android/location/Location.html

查看distanceTo或distanceBetween。您可以从纬度和经度创建位置对象:

Location location = new Location("");
location.setLatitude(lat);
location.setLongitude(lon);

答案 3 :(得分:0)

使用此代码。你会得到两个地理点之间的距离。

 private String distance(double lati, double longi, double curlati1,
        double curlongi1) {

    double theta = longi - curlongi1;
    double dist = Math.sin(deg2rad(lati)) * Math.sin(deg2rad(curlati1))
            + Math.cos(deg2rad(lati)) * Math.cos(deg2rad(curlati1))
            * Math.cos(deg2rad(theta));
    dist = Math.acos(dist);
    dist = rad2deg(dist);
    dist = dist * 60 * 1.1515;

    dist = dist * 1.609344;

    result = Double.toString(dist);
    System.out.println("dist_result :" + result);
    return (result);
}

/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
/* :: This function converts decimal degrees to radians : */
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
private double deg2rad(double deg) {
    return (deg * Math.PI / 180.0);
}

/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
/* :: This function converts radians to decimal degrees : */
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
private double rad2deg(double rad) {
    return (rad * 180.0 / Math.PI);
}