views:

206

answers:

3

I have a MapActivity. If it's set to an appropriate location and zoom level to see traffic none is shown after it's first created until you interact with the map (click on it, drag, etc) at which point traffic shows up. Naturally I want traffic to show up without any user interaction after it loads but I've been unable to figure out how to trigger it. Any ideas?

From my MapActivity inherited class:

private MapView mapView;

@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.map);
 mapView = (MapView) findViewById(R.id.mapview);
 mapView.setBuiltInZoomControls(true);
 mapView.setTraffic(true);
}

And here's whats in R.layout.map

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainlayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <com.google.android.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:apiKey="...MY API KEY HERE..."
    />
</RelativeLayout>
A: 

I tried to find the XML attrs of the MapView but I couldn't find them.

My guess is that the map is not redrawn after using setTraffic(true);

Try calling mapView.invalidate();

Macarse
Sadly I've already tried mapView.invalidate(); and mapView.postInvalidate() with a delay time but it doesn't seem to have any effect.
Brian
+1  A: 

postInvalidate() after a suitable delay should do as a workaround, what kind of delay times have you tried?

Steve
At your suggestion I gave this another try. I placed mapView.postInvalidateDelayed(5000); in OnStart() for this activity and watched it get hit in debug mode and then sat and waited for traffic to appear for a good 30 seconds. Didn't seem to have any effect.
Brian
Actually you are correct. postInvalidate after a delay does indeed work if it kicks off after the map is completely rendered. If it fires before first rendering is complete nothing happens.A cheezy test fix I just did which works from onCreate since postInvalidateDelayed doesn't seem to do the trick (because I fired it from the ui thread perhaps?) anyway though ugly this works if the map renders in < 1000ms: Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { mapView.postInvalidate(); } }, 1000);
Brian