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开发者_如何学Python 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>
postInvalidate() after a suitable delay should do as a workaround, what kind of delay times have you tried?
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();
I filed a bug report on this, number 10317. From my research, when you turn on either traffic or streetview mode on a MapView, a thread starts up to retrieve the data from Google servers. If the data is not back by the time the MapView draws itself, then the appropriate lines do not get displayed. User interaction causes a redraw and by then the data is usually present. Calling invalidate doesn't work because the thread doesn't start until your code finishes executing (i.e., the code that set the mode on). The workaround is to post a future message to invalidate the MapView causing a redraw then when (hopefully) the data is present. The fix would be for the thread that does the data fetching to cause a redraw when the data's been retrieved.
精彩评论