제가 지금 만들려는 앱이 뭐냐면요
gps 정보를 받아서 시간순으로 vector에 저장하려고요
gps정보 변화량이 일정수준 이상이면 vector에 저장하는 식입니다.
근데 문제가 뭐냐면
소스코드가 순환하면서 vector가 자꾸 초기화가 되요
package com.example.mapDemo;
import java.io.*;
import java.net.Socket;
import java.util.List;
import java.util.Calendar;
import java.util.GregorianCalendar;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker.OnTimeChangedListener;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
public class MapDemo extends MapActivity {
int curYear, curMonth, curDay, curHour, curMinute, curNoon, curSecond;
int i=0;
Calendar c;
String tag = null;
String noon = "";
Date curMillis;
TimerTask second;
TextView getData;
private final Handler handler = new Handler();
static final String TAG = "MapDemo Debug";
// 지도의 초기값
double lat = 37.565263;//서울의 위도
double lng = 126.980667;//서울의 경도
int a ;
int b ;
int[] vectorlat = new int[2] ;
int[] vectorlng = new int[2] ;
String latLongString;
static final int INITIAL_ZOOM_LEVEL = 15;
MapView map_view;
//위치가 변화할 때에 지도를 이동시킬 컨트롤러
MapController controller;
List<Overlay> mapOverlays;
Drawable drawable;
MapItemizedOverlay itemizedOverlay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
map_view = (MapView) findViewById(R.id.mapview);
map_view.setSatellite(true);
map_view.setStreetView(true);
map_view.displayZoomControls(true);
map_view.setBuiltInZoomControls(true);
controller = map_view.getController();
// 위치 정보를 얻기
String context = Context.LOCATION_SERVICE;
//위치 정보를 취득하기 위한 매니저
LocationManager locationManager = (LocationManager)getSystemService(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.NO_REQUIREMENT);
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
String provider = locationManager.getBestProvider(criteria, true);
//getLastKnownLocation를 호출해서 마지막 위치값 얻어와서
Location location = locationManager.getLastKnownLocation(provider);
//updateWithNewLocation메소드 호출!
TextView myLocationText;//지명의 표시용 텍스트뷰
myLocationText = (TextView)findViewById(R.id.myLocationText);
if(location != null){
//위도와 경도값을 뽑아서
lat = location.getLatitude(); //위도
lng = location.getLongitude();//경도
a = (int)(lat*1E6);
b = (int)(lng*1E6);
latLongString = "위도 : " + (int)(lat*1E6) + "\n경도 : " + (int)(lng*1E6);
if(vectorlat[0] == 0 ) {
vectorlat[0] = a;
vectorlng[0] = b;
}
while(vectorlat[i] !=0 ){
i=i+1;}
if ((a-vectorlat[i-1])*(a-vectorlat[i-1]) + (b-vectorlng[i-1])*(b-vectorlng[i-1]) > 1){
vectorlat[i] = a;
vectorlng[i] = b;
}
}
else{
latLongString = "위치를 찾을 수 없음"; }
try {
String abc[] = new String[2];
String bcd[] = new String[2];
FileWriter out = new FileWriter("\\sdcard\\download\\abc.txt");
// for(int j = 0;j<=2;j++){
abc[0] = Integer.toString(vectorlat[0]);
bcd[0] = Integer.toString(vectorlng[0]);
abc[1] = Integer.toString(vectorlat[1]);
bcd[1] = Integer.toString(vectorlng[1]);
out.write(abc[0]+",");
out.write(bcd[0]);
out.write("\r\n");
out.write(abc[1]+",");
out.write(bcd[1]);
out.write("\r\n");
//}
out.close();
}catch (IOException e) {
}
//텍스트뷰에 표시
myLocationText.setText("당신의 현재 위치는 : \n" + latLongString + "\n");
// Location을 GeoPoint로 변경하여 경도와 위도로 지도에 표시할 위치를 지정
GeoPoint point =new GeoPoint(
(int)(lat*1E6),
(int)(lng*1E6));
// 좌표점으로 이동
controller.animateTo(point);
// 지도를 확대
controller.setZoom(INITIAL_ZOOM_LEVEL);
mapOverlays = map_view.getOverlays();
drawable = this.getResources().getDrawable(R.drawable.marker);
itemizedOverlay = new MapItemizedOverlay(drawable, this);
OverlayItem overlayitem = new OverlayItem(point, "", "");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
getData = (TextView)findViewById(R.id.TextView01);
second = new TimerTask()
{
private String tag;
@Override
public void run()
{
Log.d(tag, curYear+"."+curMonth+"."+curDay+"."+curHour+":"+curMinute+"."+curSecond);
Update(curSecond);
}
};
Timer timer = new Timer();
timer.schedule(second, 0, 1000);
c = Calendar.getInstance();
curMillis = c.getTime();
curYear = c.get(Calendar.YEAR);
curMonth = c.get(Calendar.MONTH)+1;
curDay = c.get(Calendar.DAY_OF_MONTH);
curHour = c.get(Calendar.HOUR_OF_DAY);
curNoon = c.get(Calendar.AM_PM);
if(curNoon == 0)
{
noon = "오전";
}
else
{
noon = "오후";
curHour -= 12;
}
curMinute = c.get(Calendar.MINUTE);
curSecond = c.get(Calendar.SECOND);
Runnable updater = new Runnable()
{
public void run()
{
getData.setText("현재 날짜와 시간은 "+curYear+"년 "+curMonth+"월 "+curDay+"일 "+
noon+curHour+"시 "+curMinute+"분 "+curSecond+"초 입니다. ");
}
};
handler.post(updater);
// updateWithNewLocation(location);
//현재 위치가 변하면 메소드가 호출되도록 등록
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
//시간 2000(2초마다) 거리10(10미터이상)일때
/* Button button = (Button)findViewById(R.id.Button01);
button.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v){
try {
String abc[] = new String[2];
String bcd[] = new String[2];
FileWriter out = new FileWriter("\\sdcard\\download\\abc.txt");
// for(int j = 0;j<=2;j++){
abc[0] = Integer.toString(vectorlat[0]);
bcd[0] = Integer.toString(vectorlng[0]);
abc[1] = Integer.toString(vectorlat[1]);
bcd[1] = Integer.toString(vectorlng[1]);
out.write(abc[0]+",");
out.write(bcd[0]);
out.write("\r\n");
out.write(abc[1]+",");
out.write(bcd[1]);
out.write("\r\n");
//}
out.close();
/* String abc;
String bcd;
abc = Integer.toString(a);
bcd = Integer.toString(b);
FileWriter out = new FileWriter("\\sdcard\\download\\abc.txt");
out.write ("위도:");
out.write (abc);
out.write ("\n"); //유닉스 계열에서는 \r 을 사용하지 않는다.
out.write ("\r\n"); //윈도우에서는 줄바꿈을 위해 \r\n 을 해줘야 한다.
out.write ("경도:");
out.write (bcd);
out.write ("\r\n");
out.write ("시간:");
out.write (+curYear+"년 "+curMonth+"월 "+curDay+"일 "+noon+curHour+"시 "+curMinute+"분 "+curSecond+"초");
out.write ("\r\n");
//// out.close();
}catch (IOException e) {
} return true;
}
}); */
}
private void Test()
{
getData = (TextView)findViewById(R.id.TextView01);
second = new TimerTask()
{
private String tag;
@Override
public void run()
{
Log.d(tag, curYear+"."+curMonth+"."+curDay+"."+curHour+":"+curMinute+"."+curSecond);
Update(curSecond);
}
};
Timer timer = new Timer();
timer.schedule(second, 0, 1000);
}
protected void Update(final int i1)
{
c = Calendar.getInstance();
curMillis = c.getTime();
curYear = c.get(Calendar.YEAR);
curMonth = c.get(Calendar.MONTH)+1;
curDay = c.get(Calendar.DAY_OF_MONTH);
curHour = c.get(Calendar.HOUR_OF_DAY);
curNoon = c.get(Calendar.AM_PM);
if(curNoon == 0)
{
noon = "오전";
}
else
{
noon = "오후";
curHour -= 12;
}
curMinute = c.get(Calendar.MINUTE);
curSecond = c.get(Calendar.SECOND);
Runnable updater = new Runnable()
{
public void run()
{
getData.setText("현재 날짜와 시간은 "+curYear+"년 "+curMonth+"월 "+curDay+"일 "+
noon+curHour+"시 "+curMinute+"분 "+curSecond+"초 입니다. ");
}
};
handler.post(updater);
}
public void updateWithNewLocation(Location location) {
String latLongString;
TextView myLocationText;//지명의 표시용 텍스트뷰
myLocationText = (TextView)findViewById(R.id.myLocationText);
if(location != null){
//위도와 경도값을 뽑아서
lat = location.getLatitude(); //위도
lng = location.getLongitude();//경도
a = (int)(lat*1E6);
b = (int)(lng*1E6);
latLongString = "위도 : " + (int)(lat*1E6) + "\n경도 : " + (int)(lng*1E6);
}else{
latLongString = "위치를 찾을 수 없음";
}
//텍스트뷰에 표시
myLocationText.setText("당신의 현재 위치는 : \n" + latLongString + "\n");
// Location을 GeoPoint로 변경하여 경도와 위도로 지도에 표시할 위치를 지정
GeoPoint point =new GeoPoint(
(int)(lat*1E6),
(int)(lng*1E6));
// 좌표점으로 이동
controller.animateTo(point);
// 지도를 확대
controller.setZoom(INITIAL_ZOOM_LEVEL);
mapOverlays = map_view.getOverlays();
drawable = this.getResources().getDrawable(R.drawable.marker);
itemizedOverlay = new MapItemizedOverlay(drawable, this);
OverlayItem overlayitem = new OverlayItem(point, "", "");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
}
//위치변화가 감지될 때 마다 updateWithNewLocation메소드를 호출
private final LocationListener locationListener = new LocationListener(){
public void onLocationChanged(Location location){
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider){
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider){}
public void onStatusChanged(String provider, int status, Bundle extras){}
};
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_S) {
map_view.setSatellite(!map_view.isSatellite());
return(true);
}
else if (keyCode == KeyEvent.KEYCODE_Z) {
map_view.displayZoomControls(true);
return(true);
}
return(super.onKeyDown(keyCode, event));
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}