안녕하세요 구글맵을 사용중인데요..
구글맵이 실내에 있을때에는 GPS가 켜져있으면 GPS를 통해 받지 못하고 Network로 다시 시도해서 Location을 받게 돼있잖아요..?
그런데 GPS가 켜져있으면 Location이 계속 Null로 떨어지는 현상이 발견되드라구요 이거 왜그런건가요..?
또 단말에 기본적으로 있는 구글맵이나 네이버지도 등을 사용해서 위치를 잡으면 실내에서도 위치를 바로바로 잘 잡는데
왜 제가 구현한 구글맵은 계속 위치를 못잡는걸까요..
소스입니다...
public class LocationTracker implements LocationListener {
private static final int TWO_MINUTES = 1000 * 60 * 2;
private Context ctx;
private Timer timer;
private Handler handler;
private int what;
private boolean busy;
private Location location;
public LocationTracker(Context ctx) {
this.ctx = ctx;
timer = null;
handler = null;
what = 0;
busy = false;
location = null;
}
public void updateLocation(Handler callbackHandler, final int what) {
busy = true;
this.handler = callbackHandler;
this.what = what;
LocationManager locationManager = (LocationManager)ctx.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = locationManager.getBestProvider(criteria, true);
if (provider == null)
handler.sendMessage(handler.obtainMessage(what, null));
locationManager.requestLocationUpdates(provider, 0, 0, this);
location = locationManager.getLastKnownLocation(provider);
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
synchronized (LocationTracker.this) {
if (busy) {
LocationManager locationManager = (LocationManager)ctx.getSystemService(Context.LOCATION_SERVICE);
locationManager.removeUpdates(LocationTracker.this);
handler.sendMessage(handler.obtainMessage(what, location));
busy = false;
}
}
}}, 5000);
}
public Location getLatestLocation() {
return location;
}
@Override
public void onLocationChanged(Location location) {
synchronized(this) {
if (busy) {
if (isBetterLocation(location, this.location)) {
timer.cancel();
this.location = location;
LocationManager locationManager = (LocationManager)ctx.getSystemService(Context.LOCATION_SERVICE);
locationManager.removeUpdates(LocationTracker.this);
handler.sendMessage(handler.obtainMessage(what, location));
busy = false;
}
}
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
private boolean isBetterLocation(Location newLocation, Location currentBestLocation) {
if (currentBestLocation == null)
return true; // A new location is always better than no location
// Check whether the new location fix is newer or older
long timeDelta = newLocation.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
boolean isNewer = timeDelta > 0;
// If it's been more than two minutes since the current location, use the new location
// because the user has likely moved
if (isSignificantlyNewer)
return true;
else if (isSignificantlyOlder)
return false; // If the new location is more than two minutes older, it must be worse
// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (newLocation.getAccuracy() - currentBestLocation.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(newLocation.getProvider(),
currentBestLocation.getProvider());
// Determine location quality using a combination of timeliness and accuracy
if (isMoreAccurate)
return true;
else if (isNewer && !isLessAccurate)
return true;
else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider)
return true;
return false;
}
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null)
return provider2 == null;
return provider1.equals(provider2);
}
}