My mobile not moving but GPS lat/long
values continuously changing that is why?
How to fix this issue? How to get reliable lat long values of my mobile ...Am using following code to get GPS locations:
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isGPSEnabled) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
// Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
// Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
There are lots of reasons that can make that happen, and it's probably not a problem at all.
Sometimes it's just your GPS getting a better fix of your location, or perhaps the location provider changed.
P.S.: The Accuracy is not THAT good, so your location might change from time to time. As you can see on this link. And you can obtain your location from 6 different sources, each one of them has different accuracy.
Most Androids have the ability to store the last know GPS location. This is typically used when an application first starts up, and you can retrieve the timestamp, latitude, longitude and accuracy.
Sample code:
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Cached Network.
Android devices can also store the last known location as determined by the cellular carrier’s network location provider. The provider gathers information from the cell network and WiFi, if it’s turned on, then sends that off to a remote server-side process that crunches the information and sends back an approximate location. This is not available in all countries. Just like the GPS, you’ll typically retrieve the timestamp, latitude, longitude and accuracy.
Sample code:
locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
This is the raw information that is streamed off the GPS. When a GPS is first turned on it won’t immediately return any information, it has to basically warm up first. The warm up time varies by device and can typically take from one minute or longer if you are inside a building. More on that in a bit. Depending on what your provider allows you can get access to timestamp, latitude, longitude, altitude, bearing, speed, accuracy and distance travelled.
Sample code:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,time,distance,listener);
This is the raw network location provider information returned by the cellular carrier, such as AT&T in the U.S. Different carriers use different information to determine location such as WiFi data, GPS information, nearby cell towers, etc. Depending on what your carrier allows, you can get access to timestamp, latitude, longitude, altitude, accuracy and distance travelled.
Sample code:
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,time,distance,listener);
This option just means that your application can listen for location updates while it is in a minimized state. The idea is to save battery power, such that your applications providers aren’t running full speed ahead while the app is minimized. That would be huge battery drain. Passive location can listen for another application to request location updates. You may be able to get access to timestamp, latitude, longitude, altitude and accuracy. As far as I know, you won’t be able to determine from which provider this information was derived.
Sample code:
<receiver android:name=".PassiveLocationChangedReceiver" android:enabled="true"/>
Although it’s not human readable, you can get access to the raw NMEA strings. Typically these strings are used for programmatic access and would only make sense to a developer or engineer. This data is often used in maritime apps. The data is only available once the GPS has warmed up.
Source: This info was taken from here.
Also, there are some very common sources of error while getting the location, as you can see from android docs:
Obtaining user location from a mobile device can be complicated. There are several reasons why a location reading (regardless of the source) can contain errors and be inaccurate. Some sources of error in the user location include:
- Multitude of location sources
GPS, Cell-ID, and Wi-Fi can each provide a clue to users location. Determining which to use and trust is a matter of trade-offs in accuracy, speed, and battery-efficiency.
- User movement
Because the user location changes, you must account for movement by re-estimating user location every so often.
- Varying accuracy
Location estimates coming from each location source are not consistent in their accuracy. A location obtained 10 seconds ago from one source might be more accurate than the newest location from another or same source.
Please, take some time to read the Location Strategies docs from Google.
More information on LocationManager
here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With