안녕하세요
gps 연동을 해서 주소를 가져오는데 어느폰에서는 잘 가져와지고 어떤 폰은 오류 떨어지고 하네요
주소는 현재위치가 아닌 이전에 위치했던 주소를 가져오고요
어케어케 검색으로 해서 만들기는 했는데 현재위치를 못 가져오니.. 답답하네요..
휴대폰은 GPS기능 키면 아래 색칠한부분에서 nullpointerException 이 나고요.. 흠..
먼가 소스가 꼬인듯한 기분인데.. 해결 좀 부탁드리겠습니다 꾸벅
아래는 소스입니다.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mygps);
Button btn = (Button) findViewById(R.id.gpsButton);
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
GetLocations();
}
});
}
public void GetLocations() {
Intent intent = new Intent();
double Latitude = 0;
double Longitude = 0;
StringBuffer strJUSO = new StringBuffer();
// GPS를 이용한 위치정보
String context = Context.LOCATION_SERVICE;
mGPS = (LocationManager) getSystemService(context);
// GPS 환경설정
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE); // 정확도
criteria.setPowerRequirement(Criteria.POWER_LOW); // 전원 소비량
criteria.setAltitudeRequired(false); // 고도, 높이 값을 얻어 올지를 결정
criteria.setBearingRequired(false); // PROVIDER 기본 정보
criteria.setSpeedRequired(false); // 속도
criteria.setCostAllowed(true); // 위치 정보를 얻어 오는데 들어가는 금전적 비용
provider = mGPS.getBestProvider(criteria, true);
// GPS 장치가 없는 휴대폰이거나 설정이 꺼져있는 경우 바로 alert 처리하거나 GPS 설정으로 이동
if( provider == null ) {
result = chkGpsService();
if( result ) {
GetLocations();
}
} else {
mGPS = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//mGPS.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); // GPS 위치정보 업데이트
mGPS.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this); // 기지국 위치정보 업데이트
mAddress = new Geocoder(this, Locale.KOREAN);
// 가장 최근의 로케이션을 가져온다
myLoc = mGPS.getLastKnownLocation(provider);
mGPS.requestLocationUpdates(provider, 1000, 5, loclistener);
if( myLoc == null ) {
myLoc = mGPS.getLastKnownLocation(provider);
}
Latitude = myLoc.getLatitude();
Longitude = myLoc.getLongitude();
try {
// 위도,경도를 이용하여 현재 위치의 주소를 가져온다.
List<Address> addresses;
addresses = mAddress.getFromLocation(Latitude, Longitude, 1);
for( Address addr: addresses ) {
int index = addr.getMaxAddressLineIndex();
for( int i=0;i<=index;i++ ) {
strJUSO.append(addr.getAddressLine(i));
strJUSO.append(" ");
}
strJUSO.append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
intent.putExtra("Latitude", String.valueOf(Latitude));
intent.putExtra("Longitude", String.valueOf(Longitude));
intent.putExtra("strJUSO", String.valueOf(strJUSO.toString()));
setResult(Activity.RESULT_OK, intent);
finish();
}
private boolean chkGpsService() {
String gs = android.provider.Settings.Secure.getString(getContentResolver(),
android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if( gs.indexOf("gps", 0) < 0 ) {
// GPS OFF 일때 Dialog 띄워서 설정 화면으로 이동.
AlertDialog.Builder gsDialog = new AlertDialog.Builder(this);
gsDialog.setTitle("GPS Status OFF !!!");
gsDialog.setMessage("Change Setting !!");
gsDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog, int which ) {
// GPS설정 화면으로 이동
Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);
}
}).create().show();
return false;
} else {
return true;
}
}