0

このコードを使用して、2点間の距離を計算しています。半正矢関数を使用しています

private double CalcDistance(Location Start, Location End) 
    {
        // TODO Auto-generated method stub
        double distance;
        double lat1 = Start.getLatitude();
        double lat2 = End.getLatitude();
        double lng1 = Start.getLongitude();
        double lng2 = End.getLongitude();
        double dLat = Math.abs(Math.toRadians(lat2-lat1));
        double dLng = Math.abs(Math.toRadians(lng2 - lng1));

        distance = Math.sin(dLat/2) * Math.sin(dLng/2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * 
                Math.sin(dLng/2) * Math.sin(dLng/2);

        double c = 2 * Math.asin(Math.sqrt(distance));

        //Return the answer in Kilometre (6371km the mean radius of the earth)
        return c * 6371;
    }

生成される値は、場所の経度が変更された場合にのみ変更され、緯度が変更された場合でも同じままです。私のコードがこれだけを行う理由はありますか?

4

2 に答える 2

1

変更する必要があります

Math.sin(dLat/2) * Math.sin(dLng/2)

Math.sin(dLat/2) * Math.sin(dLat/2)
于 2012-02-10T00:09:16.900 に答える
1

ちょっとよくわかりませんが、コードを見るだけでエラーが発生した可能性があります。理解できませんが、変更できる可能性があります

distance = Math.sin(dLat/2) * Math.sin(dLng/2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * 
                Math.sin(dLng/2) * Math.sin(dLng/2);

distance = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * 
                Math.sin(dLng/2) * Math.sin(dLng/2);
于 2012-02-10T00:10:33.013 に答える