How to update gps location frequently? - android

I am writing an application that shows your location in a google map...
so far, so good... It does shows my location correctly
The problem is that this gps is not updated if I am moving (or it does, but only after some time)
Does anybody know how to do this in a way like the native google maps (for android) does?
This is, if you click on 'my location' it shows a flashing blue point that changes as you move...
This is my code:
//Initializing the listeners
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 35000, 10, networkLocationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, gpsLocationListener);
//and now, in both 'networkLocationListener' and 'gpsLocationListener'
//I overwrite the method 'onLocationChanged':
#Override
public void onLocationChanged(Location location) {
Log.i("test", "New network location: "+location.getLatitude()+
" , "+location.getLongitude());
myLongitude= location.getLongitude();
myLatitude= location.getLatitude();
}
//and the variables myLongitude and myLatitude are the only ones that I need to display my
//position in the map...
Does anybody knows if I am missing something?

The method call you are using is requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener), and you're telling it to only give you updates every 35000ms or every 35 seconds. Try lowering that number to whatever suits you. A lower number will mean more battery usage though.
If you want an indicator on a MapView, look into MyLocationOverlay.

You wouldn't want to leave it like this forever, but it might be worth setting the minTime and minDistance values to zero if you haven't given that a shot yet.
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsLocationListener);
If minTime is greater than 0, the LocationManager could potentially rest for minTime milliseconds between location updates to conserve power. If minDistance is greater than 0, a location will only be broadcasted if the device moves by minDistance meters. To obtain notifications as frequently as possible, set both parameters to 0.
-requestLocationUpdates Reference

Related

onProviderDisabled - need gps in battery saving mode

onProviderDisabled is telling me the gps is disabled. I want to be able to get the lat and lon in the wifi gps mode as well as the full gps mode.
I am using this code to start up
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
3000,
1, this);
It all works with the full GPS on but I need to be able to get the lat and lon in either mode.
You can request updates from more than one provider. Add the following line:
locationManager.requestLocationUpdates(LocationManager. NETWORK_PROVIDER, 3000, 1, this);
The same callback methods, i.e. onLocationChanged(), onProviderDisabled(), etc., will be used to pass the updates from both providers. If you need to know which provider is passing the update, you can use the Location.getProvider() method in onLocationChanged(), and the String provider parameter passed in the others.

Better way to query GPS regularly

I have a timer that runs every second. Every second I get the GPS location and do other stuffs.
I am wondering which way is better:
1- Request a single location update and then get the last known location
private void timeout(){
String data[] =new String[DATA_LENGTH];
locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, this, null);
Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
.
.
.
}
2- Start Location listener and then just get the last known location whenever my timer expire
OnCreate(){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
private void timeout(){
String data[] =new String[DATA_LENGTH];
Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
.
.
.
}
Thank you
PS: Note that battery is not a concern to me as per the requirement of the product
requestSingleUpdate is meant to be single, if you need to query the GPS frequently you should definitely go with option 2.
Keep a global Location object in memory, use it in you other stuff and update it whenever your listener gets an update from the LocationManager.
You can listen for changes via requestLocationUpdates - the code below is a quick-n-dirty example (untested). Remember, you have to have location services turned on to use this.
LocationListener locGPSListener= new LocationListener() {...}
LocationListener locNetworkListener= new LocationListener() {...}
mgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
// listens using GPS for location
mgr .requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locGPSListener);
// uses towers for location
mgr .requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locNetworkListener);
...
What approach is better, depends on
Androids GPS behaviour and
your Application.
ad 1. if explicitly getting a location delivers a more recent fix, than this is an advantage, because:
ad 2. if your application don't want the android filtering behaviour, and you can filter it yourself better, then this would be better for your app.
Example: (is for ios, but may apply here too:) if I drive with my car to a traffic signal, and do a harsh breaking, then ios still shows 5 km/h speed, although I am standing still. This I call unwanted filtering.
This has all nothing to do with battery: if you get the location via message or if you query it is the same from battery point of view. It smore a software design issue: (events vs. polling)
A difference would only be if GPS is disabled, but disabling GPS makes only sense if it can be disabled for long time.

onlocationChanged is called even if I am sitting at the same place

I have used the code below and everything is working fine except that onLocationChanged is called even if I am sitting at the same location .
I thought it should be called only when I am moving right ?
I only want to get the location after I have moved a certain distance.
Please help me out.
Thanks in advance.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
this);
}
#Override
public void onLocationChanged(Location location) {
Toast.makeText(this, "Working!", Toast.LENGTH_SHORT).show();
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
String Text = "Latitud = " + lat + "\nLongitud = " +
lng;
Toast.makeText(getBaseContext(),Text,Toast.LENGTH_SHORT).show();
}
}
You're requesting location updates at the shortest possible intervals/distances
locationMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
this);
This is what the documentation says about those parameters
" The location update interval can be controlled using the minTime parameter. The elapsed time between location updates will never be less than minTime, although it can be more depending on the Location Provider implementation and the update interval requested by other applications. "
The minDistance parameter can also be used to control the frequency of location updates. If it is greater than 0 then the location provider will only send your application an update when the location has changed by at least minDistance meters, AND at least minTime milliseconds have passed. However it is more difficult for location providers to save power using the minDistance parameter, so minTime should be the primary tool to conserving battery life.
I personally use a minTime of 10 seconds and 10 meters for my app
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000,
10, locationListener);
Network location is not as precise as you would think it is. Therefore the result returned by the sensors can fluctuate. This is even more true when you consider that GPS signal weakens if you don't have direct line of sight with the satellites, and the number of visible satellites also has effect on the precision. This gets even worse when you start using the network provider, where the position is calculated by triangulation of the signal strength of cell towers, and the number and SSIDs of visible wireless network. Since these can fluctuate quite a lot, the precision suffers greatly. There are bunch of averaging algorithms, and heuristics employed to minimize such fluctuations, but ultimately nothing can stabilize it to be as good as you expect it to be.
A simple averaging and variation filtering can help you. Adding a correction based on the device accelerometer can also help a lot, but it will make your code more complex.

Location coordinates does not change when using LocationListener

I am using LocationListener on my application. However, whenever my location changes or onLocationChanged(Location location) method is executed, the coordinates of my location does not change at all. I am riding inside a car and had traveled several meters to kilometers already without my location changing. What could be the problem?
My code:
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
String bestProvider = locationManager.getBestProvider(new Criteria(), true);
{LocationListener locationListener = new LocationListener()
{
public void onLocationChanged(Location location)
{
//location.getLatitude(), location.getLongtitude()
//location coordinates does not change at all everytime onLocationChanged is called even after I already move several meters or kilometers.
}
//...
}
locationManager.requestLocationUpdates(bestProvider, 0, 0, locationListener);
Try this
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locationListener);
You might be using the network provider. The listener is normally removed in the activty onPause. And you can set the listener in the onResume event of the activity.
Im not sure what is the logic behind finding the best provider by android. But GPS is always considered to be the most accurate compared to the network provider. But there are conditions when getting a location lock by the GPS is not possible. Thats when the network provider comes into use.
I normally create a simple class that calls a function to get location coords. Inside the function, I first request the location using GPS. I wait for 30 seconds for the location, if it is not received, then I request it using the network provider. This way I can always ensure that I am getting the GPS value as my first preference.
My guess is that your location is changing but you are overlooking the decimal part of latitude and longitude. Note the last digits of the decimal part before and after location change.
Try this way in your code
if(isInternetEnabled==true)//check for internet connection here
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locationListener);
}
else
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
locationListener);
}
Because it happens with me that GPS_PROVIDER doesn't give quick response to location update then NETWORK_PROVIDER.

GPS emulator tracks

I am using the android emulator ddms to simulate movement using a gpx file. There are about 1000 entries in the gpx file. However, I'm finding that my onLocationChanged method is only being triggered a few times during the course of the entire file. My code is as follows...
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, myListener);
myListener = new LocationListener(){
public void onLocationChanged(Location location){
double mylat = location.getLatitude();
double mylon = location.getLongitude();
...
}
...
}
My understanding of the requestLocationUpdates being set to 1000 is that it should request a location update every 1 second provided the location is more than 1m away from the previous. Is this correct? If so, why would I only be retrieving a few of the lat/lon pairs from the gpx file? Wouldn't I be retrieving nearly a thousand? (The GPS data is from someone running so there is constant motion.)
I haven't had very good luck with using the emulator locations using gpx, kml, or manually entering lat/long coordinates . In my experience, setting the location and distance minimums in requestLocationUpdates does work well on real hardware but not in the emulator.
If using the gpx file is just for testing purposes, try setting the time and location constraints to 0 and 0 just to see if it now registers all 1000 of your points within the emulator.

Categories

Resources