안드로이드 개발 질문/답변
(글 수 45,052)
좌표를 서비스로 입력받고 이동한 경로를 Overlay하여 구현하는게 되지 않습니다.
현재 메인액티비티에는 3개의 버튼이 있습니다. 1은 서비스 호출버튼 2는 서비스 정지 버튼 3은 구글맵 호출 버튼입니다.
서비스 호출버튼을 누르면 LocationManager 이 생성되고 GPS신호를 수신합니다. 이때 ArrayList를 이용하여 좌표를 저장합니다.
그러면 지나온 좌표들이 ArrayList에 저장되었을 거고 맵을 실행하면 draw를 이용하여 이동경로를 그리게 하였습니다.
제가 예상하는 결과는 맵을 켰을 때 처음 서비스가 시작된 좌표부터 현재위치의 지점까지 이동경로가 그려진 화면이 보이는 것입니다.
하지만 현재 결과는 맵을 호출하는 순간의 위치부터 경로가 그려지게 됩니다.
서비스 구간에 있는 좌표 받아오는 소스 입니다.
LocationListener mListener=new LocationListener() {
public void onLocationChanged(Location location) {
mMyBeforeLocation = mMyCurrentLocation;
mMyCurrentLocation = location;
if(mMyBeforeLocation != null && mMyCurrentLocation != null && mMyBeforeLocation != mMyCurrentLocation)
{
mMyPathLocationArray.add(new MyPathLocation(mMyBeforeLocation, mMyCurrentLocation));
mMyDistance += mMyCurrentLocation.distanceTo(mMyBeforeLocation);
}
} public class MyPathLocation
{
public Location mMyBeforeLocation1;
public Location mMyCurrentLocation1;
public MyPathLocation(Location myBeforeLocation, Location myCurrentLocation)
{
mMyBeforeLocation1 = myBeforeLocation;
mMyCurrentLocation1 = myCurrentLocation;
}
}/*아랫부분은 overlay 부분입니다.*/
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
{
if(MyService.mMyBeforeLocation != null && MyService.mMyCurrentLocation != null)
{
mPath.reset();
canvas.drawPath(mPath, mPaint);
updatePath();
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawPath(mPath, mPaint);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawTextOnPath(String.valueOf(MyService.mMyDistance), mPath, 0, 0, mPaint);
}
return super.draw(canvas, mapView, shadow, when);
}
public void updatePath()
{
for(int i = 0 ; i < MyService.mMyPathLocationArray.size() ; i++)
{
Point startPoint = new Point();
Point endPoint = new Point();
MyPathLocation temp = MyService.mMyPathLocationArray.get(i);
mMapView.getProjection().toPixels(new GeoPoint((int)(temp.mMyBeforeLocation1.getLatitude()*1E6), (int)(temp.mMyBeforeLocation1.getLongitude()*1E6)), startPoint);
mMapView.getProjection().toPixels(new GeoPoint((int)(temp.mMyCurrentLocation1.getLatitude()*1E6), (int)(temp.mMyCurrentLocation1.getLongitude()*1E6)), endPoint);
Path p = new Path();
p.reset();
p.moveTo(startPoint.x, startPoint.y);
p.lineTo(endPoint.x, endPoint.y);
mPath.addPath(p);
}
}mMyBeforeLocation 변수등은 static을 사용하였습니다.
어느부분이 잘못되었는지 잘 모르겠습니다.ㅜ
도움 부탁드립니다.
공지사항 다 읽었음




자답입니다. 위의 소스에는 없지만 오버레이클래스 생성자 부분에서 ArrayList 생성자를 또 적었더군요..
결국 오버레이 호출될때 새로 정의되어 그때부터 경로가 그려지는것 이었습니다.
생성자 제거로 문제 해결하였습니다.