Gomdori

[Android] 안드로이드 스튜디오 GPS 위도/경도 거리 계산법 본문

코딩(Coding)

[Android] 안드로이드 스튜디오 GPS 위도/경도 거리 계산법

Ghomdori 2020. 10. 8. 18:48

안드로이드 GPS 위도/경도 지점 A와 지점 B와의 거리 계산법

    public String getDistance(double lat1, double lng1, double lat2, double lng2) {
        double distance;
        Location locationA = new Location( "point A" );
        locationA.setLatitude( lat1 );
        locationA.setLongitude( lng1 );
        Location locationB = new Location( "point B" );
        locationB.setLatitude( lat2 );
        locationB.setLongitude( lng2 );
        distance = locationA.distanceTo( locationB );
        if(distance>1000){
            distance=500;
        }else if(distance==0){
            Random randomGenerator = new Random();
            int start = 1;
            int end = 20;
            double range = end - start + 1;
            int randomInt5to10 = (int)(randomGenerator.nextDouble() * range + start);
            distance =randomInt5to10+randomGenerator.nextDouble();
        }
        if(distance==500){
            String num = String.format( "%.0f", distance )+"+";
            return num;
        }else{
            String num = String.format( "%.1f", distance );
            return num;
        }
    }
Comments