안드로이드 개발 질문/답변
(글 수 45,052)
package com.GpsEx1;
import java.io.IOException; import java.util.List; import java.util.Locale;
import android.app.Activity; import android.content.Context; import android.location.Address; import android.location.Criteria; import android.location.Geocoder; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView;
public class GpsEx1 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String context = Context.LOCATION_SERVICE; LocationManager locationManager; locationManager = (LocationManager) getSystemService(context); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(true); criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true); Location location = locationManager.getLastKnownLocation(provider); updateWithNewLocation(location); locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
} private void updateWithNewLocation (Location location) { final String latlongString; final TextView myLocationText; Button Btn1; Button Btn2 = (Button)findViewById(R.id.Btn1); myLocationText = (TextView) findViewById(R.id.myLocationText);
String addressString = "No address found";
if (location != null) { double lat = location.getLatitude(); double lng = location.getLongitude(); latlongString = "위도 : " + lat + "\n경도 : " + lng;
double latitude = location.getLatitude(); double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this, Locale.KOREAN);
try { List<Address> addresses = gc.getFromLocation(latitude, longitude, 1); StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) { Address address = addresses.get(0); for (int i = 0; i < address.getMaxAddressLineIndex(); i++) sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n"); sb.append(address.getPostalCode()).append("\n"); sb.append(address.getCountryName()).append("\n");
addressString = sb.toString(); }
} catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
} else { latlongString = "location 발견안됨"; } myLocationText.setText("당신의 현재 위치는 \n" + "" + "이고 \n\n" + "주소는 \n" + "" + "입니다"); Btn2.setOnClickListener(new OnClickListener() { public void onClick(View main) { // TODO Auto-generated method stub myLocationText.setText("당신의 현재 위치는 \n" + latlongString + "이고 \n\n" + "주소는 \n" + addressString + "입니다"); } }); }
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) { } }; }
안되는 부분이
addressString = sb.toString(); // 1번
myLocationText.setText("당신의 현재 위치는 \n" + latlongString + "이고 \n\n"
+ "주소는 \n" + addressString + "입니다"); // 2번
1번을 고치면 2번이 오류가납니다.
final 로 픽셀시킬경우그럽니다.