LocationManager best implementation? - android

Is it advisable to implement both LocationManager.NETWORK_PROVIDER and LocationManager.GPS_PROVIDER with the differenct listener and then unregister the listeners locationManager.removeUpdates(locationListener) ?
Because GPS_PROVIDER takes lot of time to return the values and sometimes doesn't even gives a call back?
Also, we can have a locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); in handler with a postDelayed to avoid a deadlock in case there aren't any callbacks?
Any help by anyone?
Thanks in advance!!!
BR,
J

Hm afaik locationManager.getLastKnownLocation returns instantly, so you should not see any deadlocks?
If you really need to manage your own LocationListeners, it is advisable to first register a Network-One and a two GPS-ones. Use the network-Listener to receive a first location (rough). Configure one GPS-Location-listener to receive all GPS-Updates. Once the location is accurate neough, switch to a managed GPS-Location-Listener, that only receives updates every x seconds and x meters diff. See http://developer.android.com/guide/topics/location/obtaining-user-location.html for more examples.
If you want to display the location on a map, try MyLocationOverlay. It does all that for you.

Ideally this question wont be valid anymore, since Google finally released newer version of location listeners which would take care of this more effectively.
Link for the same below:
http://developer.android.com/google/play-services/location.html

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.

How to avoid frequent location updates?

I am new to android. I want to get GPS locations at an interval of 3 minutes, but I'm getting location updates on every second.
How can I avoid this?
Please suggest something. I'm using the following code:
captureFrequencey=3*60*1000;
LocationMngr.requestLocationUpdates(
LocationManager.GPS_PROVIDER, captureFrequencey, 0, this);
LocationMngr.requestLocationUpdates(LocationManager.GPS_PROVIDER, captureFrequencey, 0, this);
Thats because you set the minimum distance to 0. Change it to 100 meters and check!
LocationMngr.requestLocationUpdates(LocationManager.GPS_PROVIDER, captureFrequencey, 100, this);
Refer to the documentation.
Lazy Ninja has already given you the solution.
But you should understand that, when you call request location updates it checks for conditions min time & min dist and if anyone is true it catches the location. In your code
LocationMngr.requestLocationUpdates(LocationManager.GPS_PROVIDER,
captureFrequencey, 0, this);
you have mentioned 0 as min distance, this condition is true all the time. So you are getting frequent location updates.
Hope this info helps u.
See the Location Strategies in docs
Just a quick point to add to the correct answers provided... setting the update time & distance will not work exactly the same on all devices.
These parameters are treated as "hints" to the Location Provider.
So while it is good practice, some devices may not always obey these exact values.

Android: Best way to get instant location once per use

I always had some difficulties to find a good way for the instant location of the device.
What I want to do is once per use of the app (e.g. when the onCreate of an activity is called) i want to know the coordinates of the device in that exact moment and never ask for them again.
What I think could be the best way is to have something like a static Class with a function similar to :
coordinates getCoordinates();
Some advice/snippet to give?
Use LocationManager.getLastKnownLocation or LocationManager.requestSingleUpdate.
The first will return immediately, but may return null if no location is already available.
The second will return your data on a callback, but will wake up whatever provider is needed and will get a good location (if possible).
I have found what I really was searching in THIS answer.
I have only added two controls; i check if:
gps_loc.getTime() (or net_loc.getTime()) is bigger than System.currentTimeMillis() - 300000 (5 minutes ago). In the method run() of the inner class GetLastLocation;
location.getAccuracy() < 100. In the method onLocationChanged() of both LocationListeners (network and GPS).
Hope it helps!

How to create an action if someone close to some certain location?

This might be a noob question for some of you. I want to create an action for android application where if someone is close to some certain location using geopoint and i want to verify this using mylocation to create an action which will location aware. Any idea ??
You could hold the location you want to reach in a Location object,
and then every time location gets update from the GPS, or every few seconds or so, check ths distance to it. and if it's smaller than a given value, you're there.
check this out
http://advback.com/android/finding-distance-between-two-points-with-google-maps-android/
EDIT:
Since you mentioned you want to use GeoPoint, here's a quick explanation on how to convert between the two:
Convert GeoPoint to Location

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