4

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);
}
Arci
  • 6,647
  • 20
  • 70
  • 98

1 Answers1

2

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 :)

Ivan Bartsov
  • 19,664
  • 7
  • 61
  • 59
  • Thank you! I've tested my application again today and it's working better than yesterday. Although there are times that the marker will still jump from one place to another. I haven't change anything on my code so it seems that I'm just in a bad location yesterday. I'm not directly using a LocationManager as I'm just reusing the MyLocationOverlay class by extending it. Should I create a LocationManager? By the way, do you know how often does the MyLocationOverlay gets updated? – Arci Feb 08 '12 at 02:42
  • Giving fake location updates have also come up to my mind but I decided not to implement it because there are lot of problems that could arise from it such as what if the user suddenly changes his direction or what if he is facing west when he's going east, etc. I've checked the getTime method. I haven't notice it before. Thanks for the tip! – Arci Feb 08 '12 at 02:46
  • 1
    You don't have to create LocationManager :) here: http://developer.android.com/guide/topics/location/obtaining-user-location.html is an example on how to obtain it, but you don't need it so long as you're using MyLocationOverlay - there are enable/disable- myLocation() methods that already do that internally. You can't set frequency of MyLocationOverlay updates (see answer here http://stackoverflow.com/questions/8394867/how-do-you-implement-follow-me-using-the-mylocationoverlay-class), so if you want full control over the frequency you'll have to write your own overlay and listener. – Ivan Bartsov Feb 08 '12 at 07:12
  • And yeah, fake updates can cause some serious confusion if the user turns, especially if they're driving a race car or something, the displacement in 10 seconds can be huge. I was going to suggest also using NETWORK_PROVIDER location updates to back up cases where you've lost gps, but then I saw that MyLocationOverlay already does that, so I guess using it is as close as you can get to smooth non-fake location tracking) – Ivan Bartsov Feb 08 '12 at 07:23
  • Thanks! I think I'm going to stick with MyLocationOverlay. :) There is no need to change its listener since the location seems to be getting updates frequently. By the way, the link you gave me is from my previous question. XD – Arci Feb 09 '12 at 03:32