Im using this code for getting the location for my app:
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 200.0f, this);
But when i tried the app in my real android phone it show this location about 80 kilometers away from the location im actualy at.. How would i make this code more accurate.. I want the result to be way more accurate for what im making..
Im using the onLocationChanged to display it at the map.. Here it is:
public void onLocationChanged(Location location) {
if (location != null) {
//Gets users longitude and latitude
lat = location.getLatitude();
lng = location.getLongitude();
//sets the GeoPoint usersLocation equal lat and lng
userLocation = new GeoPoint((int) lat * 1000000, (int) lng * 1000000);
OverlayItem usersLocationIcon = new OverlayItem(userLocation, null, null);
LocationPin myLocationPin = new LocationPin(userIcon, MainActivity.this);
//Removes the previous location
if(previousLocation != null)
mainMap.getOverlays().remove(previousLocation);
myLocationPin.getLocation(usersLocationIcon);
overlayList.add(myLocationPin);
//refresh the map
mainMap.invalidate();
//Making myLocationPin into the previousLocation just to be able to remove it later
previousLocation = myLocationPin;
}
The call
requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 200.0f, this);is asking to be updated no more than once every 1000ms when the location from GPS changes by more than 200.0 meters from the last update. If you want finer precision, try lowering these numbers.However, you shouldn’t be off by 80km. Are you testing this outside with a clear view of the sky?
I think the issue is with rounding. You are using
new GeoPoint((int) lat * 1000000, (int) lng * 1000000);, but instead do this:The difference is, the double values were converted to integers before the multiplication. This way the multiplication happens afterwards, and so the digits after the decimal point are maintained.