繁体   English   中英

如何找到两个地理点之间的距离?

[英]how to find the distance between two geopoints?

double distance;  

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

locationA.setLatitude(latA);  
locationA.setLongitude(lngA);  

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

locationB.setLatitude(latB);  
LocationB.setLongitude(lngB);  

distance = locationA.distanceTo(locationB);  

上面的代码不起作用,我距离0.0公里? 同样在location类的构造函数中,字符串提供程序的含义是什么。 在上面的代码中,我使用PointA和PointB作为提供者。

为什么上面的代码不起作用?

先感谢您。

LOGCAT 05-09 17:48:56.144:INFO / current loc(1573):lat 0.0 lng 0.0 05-09 17:48:56.155:INFO / checklocation loc(1573):lat 54.4288665 lng 10.169366

只是一个快速的片段,因为我没有看到上面有GeoPoints的完整而简单的解决方案:

public float getDistanceInMiles(GeoPoint p1, GeoPoint p2) {
    double lat1 = ((double)p1.getLatitudeE6()) / 1e6;
    double lng1 = ((double)p1.getLongitudeE6()) / 1e6;
    double lat2 = ((double)p2.getLatitudeE6()) / 1e6;
    double lng2 = ((double)p2.getLongitudeE6()) / 1e6;
    float [] dist = new float[1];
    Location.distanceBetween(lat1, lng1, lat2, lng2, dist);
    return dist[0] * 0.000621371192f;
}

如果你想要米,只需直接返回dist [0]。

尝试Location.distanceBetween(..)

更新:

如果你从GeoPoint获得lat / lon,那么他们是微观的。 你必须乘以1e6。

如上所述,位置等级是要走的路。 这是我用过的代码:

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

locationA.setLatitude(pointA.getLatitudeE6() / 1E6);
locationA.setLongitude(pointA.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类的实例。

另一种完成上述方法的方法,

    public class Distance {

    public static 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);
        dist = dist * 60 * 1.1515;
        //if (unit == "K") {
        //  dist = dist * 1.609344;
        // else if (unit == "N") {
        //dist = dist * 0.8684;
        //}
        return (dist);
    }


    public static final double PI = 3.14159265;
    public static final double deg2radians = PI/180.0;


    public static double getDistance(double latitude1, double longitude1, double latitude2,double longitude2) {

        double lat1 = latitude1 * deg2radians;
        double lat2 = latitude2 * deg2radians;
        double lon1 = longitude1 * deg2radians;
        double lon2 = longitude2 * deg2radians;
        // Williams gives two formulae;
        // this is the more accurate for close distances.
        // In practice, the two differed only in the 8th or 9th place, for
        // separations as small as 1 degree.
        double radd = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin((lat1 - lat2) / 2),
                2.0)
                + Math.cos(lat1)
                * Math.cos(lat2)
                * Math.pow(Math.sin((lon1 - lon2) / 2), 2.0)));

        return radd;
    }





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

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

}

这不是答案,但请注意Location构造函数的签名是Location(String provider)

即传递给构造函数的String应该是LocationManager.GPS_PROVIDERLocationManager.NETWORK_PROVIDERLocationManager.PASSIVE_PROVIDER而不是 "point A""point B"

double CalculateDistance( double nLat1, double nLon1, double nLat2, double nLon2 )
{
    double nRadius = 6371; // Earth's radius in Kilometers
    // Get the difference between our two points
    // then convert the difference into radians

    double nDLat = ToRad(nLat2 - nLat1);
    double nDLon = ToRad(nLon2 - nLon1);

    // Here is the new line
    nLat1 =  ToRad(nLat1);
    nLat2 =  ToRad(nLat2);

    double nA = pow ( sin(nDLat/2), 2 ) + cos(nLat1) * cos(nLat2) * pow ( sin(nDLon/2), 2 );

    double nC = 2 * atan2( sqrt(nA), sqrt( 1 - nA ));
    double nD = nRadius * nC;

    return nD; // Return our calculated distance
}

http://www.jaimerios.com/?p=39

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM