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

안드로이드 스튜디오 현재 위치 질문

0 추천
저는 부산 사람인데 location.longitude 같이 위도와 경도를 구하면 왜 계속 중국이 뜰까요? ㅠㅠㅠ 중국한번도 안가봤는데
이이창민 (560 포인트) 님이 2021년 11월 19일 질문

1개의 답변

0 추천
 
채택된 답변
LocationManager를 통해 Location을 요청하신 후 그걸 사용하세요. 그리고 애뮬레이터의 경우는 디바이스랑 달리 님이 현재 있는 Location을 구하지 못할 겁니다. 대신 애뮬레이터에는 시뮬레이션 기능이 있어서 Location을 제대로 받아서 처리하는지 테스트해 볼 수는 있습니다.
spark (227,930 포인트) 님이 2021년 11월 20일 답변
이이창민님이 2021년 11월 20일 채택됨
안녕하세요 우선 답변 감사드립니다
    public void startLocationService(){
        LocationManager manager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
        try{
            Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if(location!=null){
                double latitude = location.getLatitude();
                double longitude = location.getLongitude();
                showCurrentLocation(latitude, longitude);
            }
            GPSListener gpsListener = new GPSListener();
            long minTime = 10000;
            float minDistance=0;
            manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,minTime,minDistance,gpsListener);
            Toast.makeText(getContext(),"내 위치 확인 요청함",Toast.LENGTH_LONG).show();
        }
제가 코드를 안올렸네요 ㅠ locationmanager를 쓰는데도 중국 으로 뜨네요...
네. 로케이션을 가져오려면 당연히 LocationManager를 사용하셔야죠. 제 말은 현재 로케이션을 요청해서 가져오라는 말이었습니다. getLastKnownLocation에 대한 API 문서를 보면,
public Location getLastKnownLocation (String provider)

This will never activate sensors to compute a new location, and will only ever return a cached location.

위 메소드는 이미 다른 앱등에서 로케이션 매니저를 통해 로케이션이 업데이트 되어있다면, 운좋게 원하는 데이터를 얻을 수 있지만, 전혀 엉뚱한 로케이션이 나올 수도 있습니다. 대신에 getCurrentLocation를 통해 현재의 로케이션을 가져오셔야 원하시는 정보가 호출될 겁니다. 그리고 말씀드리다시피 애뮬레이터는 좀 다를 수 있습니다.
아아 감사드립니다 ㅠㅠ 이거 때매 머리가 아팠는데 해보겠습니다!
정말 귀찮으시겠지만 하나만 더 물어봐도 될까요

    public void startLocationService(){
        LocationManager manager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
        try{
            Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            Location currentLocation = manager.getCurrentLocation(LocationManager.GPS_PROVIDER);
            if(location!=null){
                double latitude = location.getLatitude();
                double longitude = location.getLongitude();
                showCurrentLocation(latitude, longitude);

            }
            GPSListener gpsListener = new GPSListener();
            long minTime = 10000;
            float minDistance=0;
            manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,minTime,minDistance,gpsListener);
            Toast.makeText(getContext(),"내 위치 확인 요청함",Toast.LENGTH_LONG).show();
        }


        catch (SecurityException e){
            e.printStackTrace();
        }
    }

여기서 getcurrentLocation을 사용하려하는데  getcurrentLocation이 구글에 검색해도 잘나오지 않더군요 ㅠㅠ 파라미터에서 오류가나는데 무얼 넣어야할지 알수 있을까요 죄송합니다..
개발자 문서에 나온 예제코드를 읽어보시고 가져다 사용하시면 될 것 같아요.
Make a location request 부분을 확인해 보세요. 로케이션을 요청하고 받아서 처리하는 코드입니다.
https://developer.android.com/training/location/request-updates#updates
친절한 답변감사드립니다 ㅎㅎ
...