package com.android.location1;
import java.io.*;
import java.util.*;
import android.app.*;
import android.os.Bundle;
import android.content.*;
import android.location.*;
import android.util.Log;
import android.view.*;
import android.widget.Button;
import android.widget.TextView;
public class LocationDemo extends Activity implements LocationListener {
private LocationManager locManager;
Geocoder geoCoder;
private Location myLocation = null;
double latPoint = 0;
double lngPoint = 0;
float speed = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// LocationListener의 핸들을 얻음
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// GPS로 부터 위치정보를 업데이트 요청, 1초마다 5km 이동시
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000,
0, this);
// 기지국으로 부터 위치정보를 업데이트 요청
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
1000, 0, this);
// 주소를 확인하기 위한 Geocoder KOREA 와 KOREAN 둘다 가능
geoCoder = new Geocoder(this, Locale.KOREAN);
final Button gpsButton = (Button) findViewById(R.id.gpsButton);
gpsButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
GetLocations();
Log.d("location", "button pressed");
}
});
}
public void GetLocations() {
// 텍스트뷰를 찾음
TextView latText = (TextView) findViewById(R.id.tvLatitude);
TextView lngText = (TextView) findViewById(R.id.tvLongitude);
TextView speedText = (TextView) findViewById(R.id.tvSpeed);
TextView jusoText = (TextView) findViewById(R.id.tvAddress);
StringBuffer juso = new StringBuffer();
if (myLocation != null) {
latPoint = myLocation.getLatitude();
lngPoint = myLocation.getLongitude();
speed = (float) (myLocation.getSpeed() * 3.6);
try {
// 위도,경도를 이용하여 현재 위치의 주소를 가져온다.
List<Address> addresses;
addresses = geoCoder.getFromLocation(latPoint, lngPoint, 1);
for (Address addr : addresses) {
int index = addr.getMaxAddressLineIndex();
for (int i = 0; i <= index; i++) {
juso.append(addr.getAddressLine(i));
juso.append(" ");
}
juso.append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
latText.setText(String.valueOf(latPoint));
lngText.setText(String.valueOf(lngPoint));
speedText.setText(String.valueOf(speed));
jusoText.setText(String.valueOf(juso));
}
public void onLocationChanged(Location location) {
Log.d("location", "location changed");
myLocation = location;
}
public void onProviderDisabled(String s) {
}
public void onProviderEnabled(String s) {
}
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
finish();
System.exit(0);
case KeyEvent.KEYCODE_HOME:
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
am.restartPackage(getPackageName());
}
return super.onKeyDown(keyCode, event);
}
}
-현재 실시간이 아닌 버튼으로 좌표를 가지고 오는데 시간을 두고 계속 값을 표기하는 방법이 있나요?