마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 이동거리 구하기 관련 질문 드립니다.

0 추천
double distance;
        double distanceA =0;
        String meter;

        double pointY[] = {127.011016, 127.011081, 127.011113, 127.011167, 127.011253, 127.010480};
        double pointX[] = {37.583560, 37.583449, 37.583143, 37.582795, 37.582395, 37.582301};

        for (int i = 0; i < pointX.length; i++) {
            Location locationA = new Location("point A");
            locationA.setLatitude(pointX[i]);
            locationA.setLongitude(pointY[i]);

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

            locationB.setLatitude(pointX[i + 1]);
            locationB.setLongitude(pointY[i + 1]);
            distance = locationA.distanceTo(locationB);
            distanceA =+distance;

        }

        meter = Double.toString(distanceA);
        TextView distanceoutput = (TextView) findViewById(R.id.distance);
        distanceoutput.setText("이동거리:" + meter + "m");

    }

 

위도 경도로 표시된 좌표 6개의 이동거리 합을 구하려고 합니다.

distanceTo() 를 이용하여 두점 사이의 거리를 구하고 for문을 이용하여 좌표 6개의 이동거리 합을 구하려고 하는데 문법상으로는 문제가 없다고 나오는데 앱으로 돌리면 오류나면서 팅기네요 ㅠㅠ

어떤 문제인지 전혀 모르겠습니다....

도움 부탁드려요 ㅠㅠ
해리케인 (210 포인트) 님이 2018년 5월 10일 질문

1개의 답변

+1 추천
메인스레드에서 ui 변경할려고해서 에러나는거같은데..

 

로그..가...필..요..해여

 

깁미 로그
sadeva (21,550 포인트) 님이 2018년 5월 10일 답변
제가 진짜 초보라서 무슨 말인지 잘 모르겠어요..... 조금만 더 자세히 설명 가능하신가요? ㅠㅠ
전체소스를 본게아니라 이게맞다 아니다 판별하기는 그렇지만 제가 보이는것만 말씀드리면 우선 for문안에         TextView distanceoutput = (TextView) findViewById(R.id.distance); 선언하신거 for문 밖으로 빼내시고

 distanceoutput.setText("이동거리:" + meter + "m");이부분을  

runOnUiThread(new Runnable() {
public void run() {
   distanceoutput.setText("이동거리:" + meter + "m");
 }
 });

이런식으로 변경하셔야되여 그리고.. 만약 for문안에 textview선언한의도가 textview여러개를 노출시키고싶으신거라면.. 동적으로 생성해야합니다.
제가 코드를 확실하게 안 올렸군요...
TextView distanceoutput = (TextView) findViewById(R.id.distance);는 원래 for문 밖에 있어요ㅠㅠ
final TextView distanceoutput = (TextView) findViewById(R.id.distance);
        runOnUiThread(new Runnable() {
            public void run() {
                distanceoutput.setText("이동거리:" + meter + "m");
            }
        });
로 변경해서 해봤는데 여전히 팅기네요 ㅠㅠ
public class MainActivity extends AppCompatActivity {
    String meter;
   float distance;
   float distanceA=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Thread(new Runnable() {
            public void run() {

                double pointY[] = {127.011016, 127.011081, 127.011113, 127.011167, 127.011253, 127.010480};
                double pointX[] = {37.583560, 37.583449, 37.583143, 37.582795, 37.582395, 37.582301};
                for (int i = 0; i < pointX.length; i++) {
                    Location locationA = new Location("point A");
                    locationA.setLatitude(pointX[i]);
                    locationA.setLongitude(pointY[i]);
                    Location locationB = new Location("point B");
                    locationB.setLatitude(pointX[i + 1]);
                    locationB.setLongitude(pointY[i + 1]);
                    float distance = locationA.distanceTo(locationB);
                    distanceA =+distance;
               }
            }
        });
        meter = Double.toString(distanceA);
        TextView distanceoutput = (TextView) findViewById(R.id.distance);
        distanceoutput.setText("이동거리:" + meter + "m");
    }
}

조언 해주신 대로 수정했습니다. 이제 app 실행하면 팅기지는 않아요!
근데 이동거리 합이 0m로 나옵니다..... 간단히 distanceTo()를 써서 두점 사이 거리를 구하면 잘 나와요.... for 문대신 while 문으로도 바꿔서 해봣는데 여전히 이동거리 합은 0m가 나옵니다....
distanceTo로 나온 값들은 서로 더 할 수가 없는 걸까요?? ㅠㅠ
무엇이 문제인지 모르겠습니다.. ㅠㅠㅠ
distance 로그값찍으면 거리 잘나오고있나요?
그리고      distanceA =+distance; 요게아니라 distanceA +=distance; 변경해보심이?
로그 값 찍어 봤는데
new Thread(new Runnable() {
            public void run() {

                double pointY[] = {127.011016, 127.011081, 127.011113, 127.011167, 127.011253, 127.010480};
                double pointX[] = {37.583560, 37.583449, 37.583143, 37.582795, 37.582395, 37.582301};
                for (int i = 0; i < pointX.length; i++) {
                    Location locationA = new Location("point A");
                    locationA.setLatitude(pointX[i]);
                    locationA.setLongitude(pointY[i]);
                    Location locationB = new Location("point B");
                    locationB.setLatitude(pointX[i + 1]);
                    locationB.setLongitude(pointY[i + 1]);
                    float distance = locationA.distanceTo(locationB);
                    distanceA =+distance;
               }
            }
        });

이부분은 로그 자체가 아예 안찍히네요......
로그가안찍힌다는말이 로그를 찍었는데 값이 없다는말인가여?
logcat에 아예 뜨지를 않아요...
아예 new Thread(new Runnable() 부분이 실행이 안되는거 같아요... ㅠㅠ
맨끝에...

  }).start();
해결했는지는 모르겠는데, for 문에서 pointX.length 에서 1을 안빼줘서 그런 것 같은데요?
끝에 }).start();을 쓰면 로그는 찍히는데 앱에서 팅기네요....
와 이게 답이었네요 ㅠㅠㅠ 감사합니다 ㅠㅠㅠㅠㅠ
...