Tech. Forum 개발자들을 위한 정보 공유의 장입니다
닫기

Tech.Forum

현위치 계속 갱신 하고싶은데 한번만 봐주세요!

#1
    LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                double latitude = location.getLatitude();
                double longitude = location.getLongitude();
            }

            currentLatitude = location.getLatitude();
            currentLongitude = location.getLongitude();

            lati=String.valueOf(currentLatitude);
            longi=String.valueOf(currentLongitude);

            tp= new TMapPoint(currentLatitude,currentLongitude);

                Log.d("debug", tp.toString() );



        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

질문 1

저번에 올린걸 보고 코딩을 해봤습니다.

코딩이 부분적이긴 한데

현재 위치가 계속 갱신되는건 Log를 통해 볼 수 있습니다.

하지만 하나 궁금한 점이 있는데

05-11 19:14:03.189 D/debug: Lat 35.1312423 Lon 129.1099718
05-11 19:14:23.314 D/debug: Lat 35.1312643 Lon 129.109921
05-11 19:14:43.339 D/debug: Lat 35.1312557 Lon 129.1099515
05-11 19:15:03.339 D/debug: Lat 35.1312819 Lon 129.1098636
05-11 19:15:23.339 D/debug: Lat 35.1312702 Lon 129.1099267
05-11 19:15:43.614 D/debug: Lat 35.1312504 Lon 129.109941

갱신시간이 20초인데
혹시 1초정도로 설정할순 없나요?.

질문2

저는 집에서 위치하고 있는데

갱신하는 좌표는 아파트 옆 길 입니다.

이정도 오차는 원래 있는건가요?

0 Likes

#3

안녕하세요.

T map API 운영담당자입니다.

답변 1 requestLocationUpdates 메소드의 인자는 위치 획득할 프로바이더, 갱신시간, 갱신이동거리, 리스너입니다. locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 갱신시간을 1000으로 설정하면 원하는 결과을 얻으실 수 있을겁니다.

자세한 내용은 아래의 링크를 참고하시면 도움이 되실겁니다. ------------------------- https://developer.android.com/reference/android/location/LocationManager ------------------------- 답변 2 GPS는 위성의 신호를 받으므로 신호의 세기나 단말기에 따라 오차가 발생할 수 있습니다.

감사합니다.

0 Likes

#4

혹시
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener);

이렇게 했는데도 갱신이 20초로 되네요. 다른 부분에 설정할게 있나요?

대충코드는 이렇습니다

public void onLocationChange(Location location) {

    if (m_bTrackingMode) {
        tMapView.setLocationPoint(location.getLongitude(), location.getLatitude());
        tMapView.setCenterPoint(location.getLongitude(), location.getLatitude());


    }

}


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.riding);

    button1 = (Button) findViewById(R.id.find);
    button2 = (Button) findViewById(R.id.delete);


    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            drawCarPath();
        }
    });

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            removeMapPath();
        }
    });
    TMapPoint tp1 = new TMapPoint(35.1331597, 129.1030233);

    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                double latitude = location.getLatitude();
                double longitude = location.getLongitude();
            }

            currentLatitude = location.getLatitude();
            currentLongitude = location.getLongitude();

            lati=String.valueOf(currentLatitude);
            longi=String.valueOf(currentLongitude);

            tp= new TMapPoint(currentLatitude,currentLongitude);

            Log.d("debug", tp.toString() );



        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 1, locationListener);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100, 1, locationListener);





    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
    LinearLayout LinearLayoutTmap = (LinearLayout) findViewById(R.id.map_view);

    tMapView = new TMapView(this);
    tmapdata = new TMapData();
    LinearLayoutTmap.addView(tMapView);
    TMapPoint startpoint = new TMapPoint(35.133418, 129.101288); //출발지
    TMapPoint endpoint = new TMapPoint(35.134819, 129.102992); //목적지
    tMapView.setSKTMapApiKey(mApiKey);


    //tMapView.setCenterPoint( 127.058677, 37.585061);
    tMapView.setZoomLevel(15);
    tMapView.setCompassMode(true); /*현재보는방향*/
    tMapView.setIconVisibility(true); /*현위치 아이콘 표시*/

    tMapView.setMapType(TMapView.MAPTYPE_STANDARD);
    tMapView.setLanguage(TMapView.LANGUAGE_KOREAN);

    tmapgps = new TMapGpsManager(riding.this);
    tmapgps.setMinTime(1000);
    tmapgps.setMinDistance(1);
    tmapgps.setProvider(tmapgps.NETWORK_PROVIDER);

    //tmapgps.setProvider(tmapgps.GPS_PROVIDER); //gps로 현 위치 잡기
    tmapgps.OpenGps();

    tMapView.setTrackingMode(true);
    tMapView.setSightVisible(true);

    tmapdata.findPathDataAllType(TMapData.TMapPathType.CAR_PATH,startpoint,endpoint,new TMapData.FindPathDataAllListenerCallback()
    {
        @Override
        public void onFindPathDataAll(Document document) {
            Element root = document.getDocumentElement();
            NodeList nodeListPlacemark = root.getElementsByTagName("Point");
            for( int i=0; i<nodeListPlacemark.getLength(); i++ ) {
                NodeList nodeListPlacemarkItem = nodeListPlacemark.item(i).getChildNodes();
                for( int j=0; j<nodeListPlacemarkItem.getLength(); j++ ) {
                    if( nodeListPlacemarkItem.item(j).getNodeName().equals("coordinates") ) {

                        pathlist.add(nodeListPlacemarkItem.item(j).getTextContent().trim());
                        Log.d("debug", pathlist.get(i).toString() );
                    }

                }

            }
        }
    });
0 Likes

#5

안녕하세요.

T map API 운영담당자입니다.

답변드린 것 이외의 별도 설정 없이 requestLocationUpdates의 갱신시간을 1000으로 설정하여 테스트해본 결과 1초 단위로 로그가 출력되는 것을 확인하였습니다.

테스트에 이용된 코드를 참고하시면 도움이 되실겁니다.

--- public class MainActivity extends AppCompatActivity { private final String TMAP_API_KEY = "발급받으신 API KEY" ; private TMapView tMapView = null; // 티맵 정의 private TMapGpsManager gps = null; // 티맵 gps 매니저 정의 private LinearLayout linearLayout = null; private TMapPoint tMapPoint = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	linearLayout = (LinearLayout) findViewById(R.id.main_layout_tmap);
	tMapView = new TMapView(this);
	tMapPoint = new TMapPoint(35.1331597, 129.1030233);
	tMapView.setSKTMapApiKey(TMAP_API_KEY);
	linearLayout.addView(tMapView);

	LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
	LocationListener locationListener = new LocationListener() {
		@Override
		public void onLocationChanged(Location location) {
			double latitude = 0;
			double longitude = 0;

			if (location != null) {
				latitude = location.getLatitude();
				longitude = location.getLongitude();
			}
			TMapPoint tp = new TMapPoint(latitude, longitude);
			Log.d("테스트", tp.toString());
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {

		}

		@Override
		public void onProviderEnabled(String provider) {

		}

		@Override
		public void onProviderDisabled(String provider) {

		}
	};

	if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
		// TODO: Consider calling
		//    ActivityCompat#requestPermissions
		// here to request the missing permissions, and then overriding
		//   public void onRequestPermissionsResult(int requestCode, String[] permissions,
		//                                          int[] grantResults)
		// to handle the case where the user grants the permission. See the documentation
		// for ActivityCompat#requestPermissions for more details.
		return;
	}
	locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
	locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);

	
}

}

감사합니다.

0 Likes

#6

계속 동일한 질문을 계속 해서 죄송합니다.

혹시 갱신되는 것좀 보여주실수 있나요?

저는 답글에 참고하라고 올리신 코딩을 따라해서 해봤는데

20초 갱신이 계속됩니다.

노트북을 바꿔봐야하나요?

폰은 여러번 바꿔봤습니다.
참고로 안드로이드 5.0 / API 21 입니다

전체 코드입니다.

package com.example.myapplication;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;

import com.skt.Tmap.TMapData;
import com.skt.Tmap.TMapGpsManager;
import com.skt.Tmap.TMapPoint;
import com.skt.Tmap.TMapView;

public class MainActivity extends AppCompatActivity {

private static final String mApiKey = "내키";

private boolean m_bTrackingMode = true;
private TMapGpsManager tmapgps = null;
private TMapView tMapView = null;
private TMapData tMapData = null;

private LinearLayout linearLayout = null;
private double currentLatitude;
private double currentLongitude;
TMapData tmapdata;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LinearLayout LinearLayoutTmap = (LinearLayout) findViewById(R.id.map_view);

    tMapView = new TMapView(this);
    tmapdata = new TMapData();
    LinearLayoutTmap.addView(tMapView);

    tMapView.setSKTMapApiKey(mApiKey);


    //tMapView.setCenterPoint( 127.058677, 37.585061);
    tMapView.setZoomLevel(15);
    tMapView.setCompassMode(true); /*현재보는방향*/
    tMapView.setIconVisibility(true); /*현위치 아이콘 표시*/

    tMapView.setMapType(TMapView.MAPTYPE_STANDARD);
    tMapView.setLanguage(TMapView.LANGUAGE_KOREAN);

    tmapgps = new TMapGpsManager(MainActivity.this);
    tmapgps.setMinTime(1000);
    tmapgps.setMinDistance(0);
    tmapgps.setProvider(tmapgps.NETWORK_PROVIDER);

    //tmapgps.setProvider(tmapgps.GPS_PROVIDER); //gps로 현 위치 잡기
    tmapgps.OpenGps();

    tMapView.setTrackingMode(true);
    tMapView.setSightVisible(true);



    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            double latitude = 0;
            double longitude = 0;

            if (location != null) {
                latitude = location.getLatitude();
                 longitude = location.getLongitude();
            }

            TMapPoint  tp= new TMapPoint(latitude,longitude);

            Log.d("테스트", tp.toString() );



        }




        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);









}

}

p.s 답글에 달아주신 테스트 코드만 해서 직접해봐도 20초로 갱신이 나옵니다!

제 설정이 잘못된걸까요?

파일을 올려도 될까요?

0 Likes

#7

안녕하세요.

T map API 운영담당자 입니다.

테스트 로그 메시지와 테스트에 이용된 안드로이드 정보입니다.

감사합니다.

0 Likes