Reputation: 9361
I have made a program using GPS and its Latitude and Longitude are about 8 miles off.
I thought it may be because I am inside but when I use google maps on my phone, it shows me at my house.
Any idea why this may be?
Code below:
public class Clue extends Activity {
public static double latitude;
public static double longitude;
LocationManager lm;
LocationListener ll;
Location location;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blah);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
ll = new MyLocationListener()}
private boolean getLoc() {
location = getLocation();
longitude = location.getLongitude();
latitude = location.getLatitude();
return inCorrectPlace(params);
}
private Location getLocation() {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
return lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
I have stripped out irrelevant code, all help will be appreciated.
Upvotes: 1
Views: 711
Reputation: 3905
the key mistake you're making is thinking that you can instantaneously request an accurate location fix. calling getLastKnownLocation()
gives you a location that could be quite old. you have to first tell the phone to start to try to get a location fix. this takes time, so you receive the location update asynchronously.
what you need to do is call requestLocationUpdates()
, then receive the updates in the onLocationChanged()
handler in your LocationListener
class. the location updates will not necessarily be received right away. it could take several seconds to several minutes.
see the android dev guide http://developer.android.com/guide/topics/location/obtaining-user-location.html
Upvotes: 3
Reputation: 9908
You can't really rely on getLastKnownLocation
to be reliable. It could be out of date, inaccurate, or null.
What you should do is use requestLocationUpdates
to actively get a fix.
As far as why google maps is fixing your location, it might be using the Network provider while you are only checking the last GPS fix.
Upvotes: 2