LocationListener how it's working - android

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

Related

Android: How to move a GPS Location object closer to another Location object

I'm currently doing some research about a very small app I wanna try building.
The core concept will consist of moving randomly spawned GPS Location objects closer to the Location of the user as time passes by.
I have been looking through the Location API and as I understand all I need to do is updating the altitude and latitude of the spawned Location object to change its position. Now I'm curious what the best method is to: "Move a Location closer to another Location by 10 meter".
Is there already some existing API that achieves that goal like this hypothetical function:
Location newLocation = Location.moveTo(Location locationFrom, Location locationTo, int distance);
or do I have to write my custom algorithm?
What would be an efficient approach in this case? Any inputs are highly appreciated!
If the problem would exist in a Cartesian coordinate system I would just substract the user Location from the spawned Location to get the direction I need to move too and then add parts of the new vector to the spawned Location to get it closer.
How can this get solved with Location's ?
EDIT: I sumbled upon this thread: Move a LatLng 5 meters towards another LatLng
I guess using SphericalUtil.java is the way to do it.

GPS Undesired Offset in MapFragment

I have a google map fragment set up in my activity and an onLocationChanged() and getLastKnownLocation running in coherence with it.
However when i run my application , my map points to the right city and everything but the location is off from the original location by about a few km or so .
To understand why this was happening ,
I tried the following
1. Used the Button to reset the location with the location update
2. Logged my Location
3. Waited for the interval to reset my location
In all these cases my actual location was the end result either in the map or in the log with no significance of the fake location anywhere in the log .
So i set up a camera changed listener to understand the new location and acquire the coordinates but the coordinates it gave had a much higher precision than the original coordinates and im guessing somehow the problem arises when the coordinates are parametrised into the latlng class.
Ex:
Normal:
xx.xxxxxxx xx.xxxxxxx
Camera Changed Listener coordinates:
xx.xxxxxxxxxxxxxxx xx.xxxxxxxxxxxxxxx
Can anyone explain this?

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 implement live update on user's current location?

I am using MyLocationOverlay to display the current location. However, it takes a while for the location to get updated. I wanted to implement something like the "follow me" feature on iPhone wherein the location of the user is also moving as the user moves. How can I make the marker of MyLocationOverlay to perform live update on its location?
Please note that I override the draw() and drawMyLocation() because I need to draw a custom marker.
My current code looks like this: (The problem with my code is that the marker does not move along with the user. Since it takes a while before onLocationChanged is called, the marker will just suddenly jump from one place to another.)
#Override
public synchronized void onLocationChanged(Location location)
{
if (location != null && mapController != null)
{
//Recenter map based on current location
mapController.animateTo(new GeoPoint((int) (location.getLatitude() * 1e6), (int) (location.getLongitude() * 1e6)));
}
super.onLocationChanged(location);
}
You can give location manager minimal interval to wait between calling that method, but it's not exactly going to mind that interval 100% of the time (see description of LocationManager.requestLocationUpdates()). Might be shorter, might be longer - because, e.g. it can be cloudy and gps chip won't be able to get location updates as regularly as you'd want it to.
There's no proper way to overcome this - location services will always have imperfections. Every location-aware app I used "hung" the marker from time to time because gps chip lost fix for a moment.
The only thing you can do to smooth this a bit is to remember the speed of movement and should gps lose fix give the map marker fake updates based on the assumption that the user is moving in the same direction with the same speed they were when gps had a fix. But it only makes sense to do that for like 2-5 skipped real updates (just to smooth the cases when fix is lost for several seconds).
UPD For that, you can implement kind of proxy location listener, which will update your marker strictly minding the intervals you specify, e.g. every 200 ms, and do so based on it's stored location. And the stored location can be asynchronously updated by the real location listener. Location objects have time of the fix that provided data (see getTime() method), you can use that to determine whether you should still "predict" next marker movement or the location is old enough to give up and notify user that gps has no clue where they are :)

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