Question

I am experiencing an unusual behavior on my MapView, i am using RoboGuice by the way i am not sure it could influence the issue.

The problem is, if i enable a MyLocationListener on my map, then every overlay on it gets continually redrawn ad infinitum.
Give a look at this example:

MyMapActivity

@ContentView(R.layout.map)
public class MyMapActivity extends RoboMapActivity {

@InjectView(R.id.map)
private MapView map;

private List<Overlay> mapOverlays;
private MyLocationOverlay compass;
private TestOverlay testOverlay;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    map.setBuiltInZoomControls(true);
    mapOverlays = map.getOverlays();
    compass = new MyLocationOverlay(this, map);
    testOverlay = new TestOverlay();
}

@Override
protected void onResume() {
    super.onResume();
    compass.enableCompass();
    refreshMap();
}

private void refreshMap() {
    mapOverlays.clear();
    mapOverlays.add(testOverlay);
}

@Override
protected void onPause() {
    compass.disableCompass();
    super.onPause();
}

@Override
protected boolean isRouteDisplayed() {
    return false;
}
}

TestOverlay

public class TestOverlay extends Overlay {

@Override
public void draw(Canvas arg0, MapView arg1, boolean arg2) {
    System.out.println("drawing...");
    super.draw(arg0, arg1, arg2);
}
}

if i run this, i get drawing... printed about ten times per second for ever.
but, if i avoid enabling MyLocationOverlay's compass by commenting out this line:

// compass.enableCompass();

then the redrawings stop.
any idea about why this happens? is it a bug or am i doing something wrong?

Edit

Just reproduced it in a project that does not make use of RoboGuice, the problem still occurs.
I guess i will do without that compass from now on

Was it helpful?

Solution

Why it happens

Because you want compass to point always in the north (or some other direction) independently of device rotation, it requires a continous read of magnetic sensor and a continous readraw of compass.

Using MapView to force an overlay redraw you must use the invalidate() mathod that requests the redraw of every overlay in the map, including the ovarlays that didn't change.

How to solve it

I've never used RoboGuice so I can only advise on a standard solution.

You can't really stop the redrawing, if you want the compass to point in the right direction, neither you can can just draw one of the overlays (because you are drawing to a bitmap, that needs to be cleared and have every overlay redraw on it).

In my case, I used two tweaks to improve this:

  • I set the sensor update frequency to the minimum frequency value.
  • Before updating overlay I compare current direction with the one that was used to draw the compass. If they differ for a small number of degrees (like 3 degrees) I ignore the update and wait for an update with a bigger difference to request an overlay redraw.

Regards.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top