public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); // 타이틀바를 없개기 위함
setContentView(R.layout.activity_main);
loadGps();
getLocation();
}
public void loadGps() {
Log.w("tag", "loadGPS");
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE); // 정확도
criteria.setPowerRequirement(Criteria.POWER_LOW); // 전원 소비량
criteria.setAltitudeRequired(false); // 고도, 높이 값을 얻어 올지를 결정
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false); //속도
criteria.setCostAllowed(true); //위치 정보를 얻어 오는데 들어가는 금전적 비용
provider = locationManager.getBestProvider(criteria, true); // 사용자의 상황에 따라 gps로 연동할지 네트워크로 추적하지 자동으로 결정
listener = new GpsLocationListener();
locationManager.requestLocationUpdates(provider, 10000, 10, listener); // 시간 & 거리
}
public Location getLocation() {
Location location = locationManager.getLastKnownLocation(provider);
if(location == null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//Toast.makeText(this, "GPS Disabled, NETWORK Enabled", 1000).show();
Log.w("tag", "get Location from NETWORK provider");
place="";
}
else if(location != null){
double latitude = location.getLatitude();
double longitude = location.getLongitude();
location.getLatitude();
location.getLongitude();
String Text = "My current location is:"+ "Latitud ="+location.getLatitude()
+"Longitud ="+location.getLongitude();
List<Address> addresses= null;
Geocoder gc = new Geocoder(MainActivity.this, Locale.getDefault());
//place="";
try{
addresses = gc.getFromLocation(latitude, longitude, 5);
StringBuffer sb = new StringBuffer();
if (addresses.size() > 0) {
for (Address addr : addresses) {
for (int i=0;i < addr.getMaxAddressLineIndex();i++)
sb.append(addr.getAddressLine(i)).append("<< \n\n");
}
Address address = addresses.get(0);
sb.append(address.getAddressLine(0)).append(" ");
place = sb.toString();
Toast.makeText(MainActivity.this, place, 2000).show();
}
else {
place="";
}
}catch(Exception e){
e.printStackTrace();
place="";
}
}
return location;
}
1. 코드 상 의문점 하나가.
geocoder를 이용하여 지금 주소를 받고 있는데요
소스상 궁금한 점이 sb.append(address.getAddressLine(0)).append(" ");
이 부분에서 getAddressLine(0)에서 0이 의미하는 바를 모르겠구요.
다른 숫자를 넣어도 되는 건지 궁금합니다.
두번째로,
주소를 얻어올때 어떨때는 도로명으로 나올때도 있고,
어떨때는 시,구, 동으로 표시가 되는데
기준점이 뭔지를 못찾겠네요;;
이게 랜덤으로 할수밖에 없는건지....
하나로 픽스 시켜서 값을 얻을 수 있는 지 궁금합니다.
부탁드려요.~