views:

138

answers:

3

Hey Guys,

I've got a question on making an navigation app more faster and more stable.

The basic layer of my app is a simple mapview, covered with several overlays (2 markers for start and destination and one for the route).

My idea is to implement a thread to display the route, so that the app won't hang up during the calculation of a more complex route (like it does right now).

After implementing the thread there are no updates shows any more, maybe you can help me with a short glance at an excerpt of my code below:

    private class MyLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location loc) {
        posUser = new GeoPoint((int) (loc.getLatitude() * 1E6), (int) (loc
                .getLongitude() * 1E6));

        new Thread(){
        public void run(){
            mapView.invalidate();

            // Erase old overlays
            mapView.getOverlays().clear();

            // Draw updated overlay elements and adjust basic map settings
            updateText();
            if(firstRefresh){
                adjustMap();
                firstRefresh = false;
            }
            getAndPaintRoute();
            drawMarkers();
        }
        };
    }

Some features have been summarized to a method like "drawMarkers()" or "updateText()"...(they don't need any more attention ;-))

A: 

You can only update the UI on the UI thread so I think that is the problem. Check out this article on threading for solutions to this problem.

BrennaSoft
A: 

I may be barking up the wrong tree here, but my guess is that it's something to do with the fact that you're making changes to the MapView on a thread that isn't the UI thread.

I'd expect this to result in one of these possibilities:

  • Your changes are throwing an exception that you're not seeing (again possibly because it's on another thread)
  • Your changes are being ignored because they're being made on the wrong thread.
  • The map's being updated but your UI thread doesn't know it needs to redraw the map.

Hope this helps - at least by pointing you in vaguely the right direction.

teedyay
+1  A: 

When are you actually asking for the thread to run? I only see code for creating it. If you did, you'd discover that only the main (UI) thread is allowed to update, as RPond notes.

Instead, split off your work and post the results back to the main thread via a Handler.

Pontus Gagge
hi, you're right, i didn't start the thread *shame*. Now my route is displayed, I'm gonna have some tests on performance ;-)
poeschlorn
Note: I think you are into unsupported/undefined behaviour when you update outside the main UI thread. The Handler pattern is your friend.
Pontus Gagge
and what about using a synchronized method for updating the map?
poeschlorn