What does this line of code do..? - android

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1200000, 200, ll);
My assumption is that it gets the location every 120000ms if it has changed more then 200 meters.
At least this is what I am experiencing anyway....which is what I want. But what happens if the phone ends up in a metal building where there is NO GPS reception..??
Does it just keep trying every 1200000ms for a GPS fix forever..?? Or does this eventually die off and not work again..??
If I put the phone on my dash and drive around...every 120000ms I send a SMS message with the latest coordinates. Everything works just fine. But if the phones ends up in a building for 3-4-5 hours and comes back out side 3-4-5 hours later....my hope is that it will pick back up where it left off and send me a new location (if it has changed more than 200 meters). But this does not appear to be the case. It just stops working and I never get a new location even if I do have a clear view of the sky. Just wondering if this stops searching for a GPS fix or something after so many attempts to obtain a GPS fix...?

It sounds like you are searching for the Critera class.
You basically define a set of criteria which then is used to get the best suitable provider for this criteria. You then pass this criteria to the getProviders() method of the LocationManager class to get the best suitable provider as a string. Be it GPS, Network etc.
You can alternatively specify the fix provider manually by doing something like this
// Get a fix from the GPS provider
LocationProvider provider = LocationManager.GPS_PROVIDER;
// Get a fix from the Network provider
LocationProvider provider = LocationManager.NETWORK_PROVIDER;
There is a very interesting read on the developers site about obtaining users location you might want to check out. Especially the part about deciding when to start listening might be of interest for you but I recommend reading the whole article.
Using the Network provider will get your best possible position without a GPS fix in other words also inside a building. You have to keep in mind tho that the position fix you obtain might be quite off of the real position when querying the network provider.
The method you are asking for requestLocationUpdates(String provider, long minTime, long minDistance, LocationListener listener) will request a location update from the set provider every minTime milliseconds or every minDistance meters and inform the listener upon that.
Hope it helps.

Did you check this site?
Specifically this link: http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String, long, float, android.app.PendingIntent)
void requestLocationUpdates(String provider, long minTime, float minDistance, PendingIntent intent) Registers the current activity to be notified periodically by the named provider.void requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener) Registers the current activity to be notified periodically by the named provider.void requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener, Looper looper) Registers the current activity to be notified periodically by the named provider.But the call you're showing uses an int as last parameter. That's not a documented function.

Related

Android - Get a GPS Update based on specific amount of Time AND a specific distance travelled

I want to get location updates every 60 seconds OR every 1500 meters traveled.
Obviously, the LocationManager allows you to set a minimum for time and meters, but the GPSUpdate will not trigger until both the time and distance minimums are met.
But what i want is to have the GPS Update every 60 seconds (no matter how far the user has traveled) and every 1500 meters (no matter how long it took).
I have tried using multiple locationClients with the locationRequests set to each criteria, but they both cannot connect at the same time.
Is there any simple way to achieve this?
Apologies for never posting back, i had forgotten all about this question.
Whatever my solution was in the past would not have been correct as i recently added a more efficient way to do this.
First.. if you are using FusedLocationAPI and GoogleApiClient, then see my answer posted here.
If you are using the LocationManager class and not the GoogleApiClient, then the solution is similar.
You need to create two requestLocationUpdates, one for Time only, and one for Distance only, with the unused field set to 0, for example:
locationManager.requestLocationUpdates(bestProvider, 0, UPDATE_MIN_DISTNACE, myBackupLocationDistanceIntervalListener);
locationManager.requestLocationUpdates(bestProvider, UPDATE_TIME_INTERVAL, 0, myBackupLocationTimeIntervalListener);
This can also be done with Criteria if need be:
locationManager.requestLocationUpdates(0, UPDATE_MIN_DISTNACE, gpsCriteria, myBackupLocationDistanceIntervalListener, Looper.getMainLooper());
locationManager.requestLocationUpdates(UPDATE_TIME_INTERVAL, 0, gpsCriteria, myBackupLocationTimeIntervalListener, Looper.getMainLooper());
Then just define your listeners. You can use either the same or separate listeners to handle the results from the requests.
Hope this helps.

LocationListener how it's working

I have question about:
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, time, distance, locationListener);
I don't know how it's work, in this listener i have method onLocationChanged(Location location), but when listener call this method ? I have two variable, time in miliseconds and distance in meters, first variable tell when the location should be refresh, but we have second variable, and what ? Meybe it's working that, when the time(value from time variable) is gone, listener check distance between last postion and new position and if the distance between this positions is bigger than value from distance variable, listener will call onLocationChanged method. Right ? And i can use LocationListener for NETWORK_PROVIDER and for GPS_PROVIDER and it's works the same way. Right ? That this working ?
Time is your refresh interval i.e. location is refreshed by that amount of time. distance is the minimum change in distance in order to call onLocationChanged, basically these 2 parameters kinda filter the location you want. NETWORK and GPS provider more or less work same way, GPS is more accuracte but slow and network provider is less accurate and fast. You can check the accuracy on each location object though. So you are on the right track. Also check google play services location api which can be found here: https://developer.android.com/training/location/retrieve-current.html

Why I am getting different GPS point at same location

I am developing an android application to track user movement over a Google map. I am using GPS to pull the user location. It works perfectly fine when I move over road and change my location continuously.
The issue is, when I stop and remain at a point (stops my movement) the GPS keep sending different GPS coordinates.
Please note that I am using GPS in open space and I have used -
Here is the location manager details I am using -
LocationManager locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);
criteria.setBearingAccuracy(Criteria.ACCURACY_LOW);
criteria.setSpeedAccuracy(Criteria.ACCURACY_HIGH);
String provider = locManager.getBestProvider(criteria, true);
locManager.requestLocationUpdates(provider,0,0, this);
Please let me know, if I am not using it properly.
I have also gone through various blogs and tutorials and learnt that GPS is not perfect at a single location. In that case how I will handle this situation. My aim is to get a single GPS while I am not moving and standing on a single position.
Please let me know.
Thank you !
RequestLocationUpdates method uses a float value for the third parameter, if you leave it at 0 it will continue to update even if you move a step or two. If you set it to a different number like 50.0 it will update every 50 metres.

Android get Location doesnt work properly

Im trying to get location and the code works but it does not work properly. Here is my code
// Get the location manager
LocationManager locationManager = (LocationManager)
getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(bestProvider);
try {
lat = (int)Math.round((location.getLatitude () * 1e6) ) ;
lon = (int)Math.round((location.getLongitude() * 1e6) ) ;
}
catch (NullPointerException e){
lat = -1;
lon = -1;
}
the problem is when Im using foursqure or facebook they get the location directly but my application returns -1,-1 (sometimes) and sometimes it finds the location accurately. What might be the problem or how can I improve to get the current location better ?? Thanks
You should implements a LocationListerner and request location update. Once you get a fix you can remove the location update. You can read more info at http://developer.android.com/reference/android/location/LocationListener.html
The call to getLastKnownLocation sometimes returns null, as specified in the Andorid documentation. So sometimes that code will indeed produce lat=lon=-1. Also, getBestProvider is a way of determining the provider that best satisfies given criteria. Since you're not specifying any criteria, the logical conclusion is that all providers are equally good, so the result may be random. Finally, note that lat=-1 and lon=-1 is a valid location, so in general it's not a good way of flagging an error.
As suggested in another answer here, you should implement http://developer.android.com/reference/android/location/LocationListener.html . You then have the problem of which providers to use. My approach is to use them all and use a simple Kalman Filter to take care of the fact that different providers at different times have different levels of accuracy. I posted the simple kalman filter that I use for this here Smooth GPS data

Android gps how to skip distance?

I am working on a GPS-enabled application and I need to record a point each N meters. However, I can't see how I can use onLocationChanged() method in the LocationListener or any other method/class. The onLocationChanged() method gives a point each second, and I need to store each N-meter point.
I believe that this has a simple solution, but since I am beginner in Android, cant find it.
Any help will be much appreciated.
requestLocationUpdates has a minDistance parameter, that
if I recall correctly does what you want. I haven't been able to test this on a real phone though, so I don't know how accurate it is.
In onLocationChanged, compare the location you get with the last one you stored. If it's less than n meters, discard it. If not, store it. Rinse. Repeat.
EDIT: Wait, even easier - doesn't requestLocationUpdates have a minDistance parameter? See here: http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates
it is working perfectly
myManager = ((LocationManager) ApplicationController.getAppContext().getSystemService( Context.LOCATION_SERVICE ));
myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1 * 1000, 0.00001f, this);
mintime =1000ms always it is calling ....

Categories

Resources