tv1 = (TextView)findViewById(R.id.tv1);
tv2 = (TextView)findViewById(R.id.tv2);
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria c=
new
Criteria();
provider = lm.getBestProvider(c,
true
);
if
(provider ==
null
|| !lm.isProviderEnabled(provider)){
List<String> list = lm.getAllProviders();
for
(
int
i =
0
; i< list.size(); i++){
String temp = list.get(i);
if
(lm.isProviderEnabled(temp)){
provider = temp;
break
;
}
}
}
Location location = lm.getLastKnownLocation(provider);
if
(location ==
null
){
Toast.makeText(
this
,
"사용가능한 위치 정보 제공자가 없습니다."
,Toast.LENGTH_SHORT ).show();
}
else
{
onLocationChanged(location);
}
@Override
public
void
onResume(){
super
.onResume();
lm.requestLocationUpdates(provider,
500
,
1
,
this
);
}
@Override
public
void
onPause(){
super
.onPause();
lm.removeUpdates(
this
);
}
@Override
public
void
onLocationChanged(Location location) {
double
lat = location.getLatitude();
double
lng = location.getLongitude();
tv1.setText(String.valueOf(lat) +
"/"
+ String.valueOf(lng));
tv2.setText(getAddress(lat,lng));
}
private
CharSequence getAddress(
double
lat,
double
lng) {
String address =
null
;
Geocoder geocoder =
new
Geocoder(
this
, Locale.KOREA);
List<Address> list =
null
;
try
{
list = geocoder.getFromLocation(lat, lng,
1
);
}
catch
(Exception e){
e.printStackTrace();
}
if
(list ==
null
){
Log.e(
"getAddress"
,
"주소 데이터 얻기 실패"
);
return
null
;
}
if
(list.size() >
0
){
Address addr = list.get(
0
);
address = addr.getCountryName() +
""
+ addr.getPostalCode() +
""
+ addr.getLocality() +
""
+ addr.getThoroughfare() +
""
+ addr.getFeatureName();
}
return
address;
}