【发布时间】:2017-11-01 19:42:31
【问题描述】:
大家好,我有一个 Activity,在我单击 Listview 中的一个项目后正在加载。
此活动显示 MapView 中的位置,如下所示:
<FrameLayout
android:id="@+id/mapContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toEndOf="@+id/imageView"
android:layout_above="@+id/button"
android:layout_below="@+id/imageView"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" >
<TextView
android:id="@+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Loading Map..." />
</FrameLayout>
问题是,如果他/她点击按钮,我想稍后将地图显示给用户。
但是 Activity 加载速度非常慢,甚至在显示任何内容之前都会出现黑色的空白屏幕。这看起来很不专业,而且似乎是错误的。
我尝试先使 MapView 不可见,并在用户点击按钮时显示它,但在 Activity 启动时它正在加载。
我正在尝试以下代码:
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_event);
mapView = new MapView(SingleEvent.this);
mapView.onCreate(savedInstanceState);
com.google.android.gms.maps.MapView.LayoutParams mapParams = new com.google.android.gms.maps.MapView.LayoutParams(com.google.android.gms.maps.MapView.LayoutParams.MATCH_PARENT,220);
mapContainer = (FrameLayout) findViewById(R.id.mapContainer);
mapContainer.addView(mapView, mapParams);
这个正在工作,它正在显示地图,但活动加载时间过长并出现黑色空白屏幕,直到我的活动被加载。
有什么方法可以首先加载 Activity,然后在单击按钮时加载 MapView?
我正在尝试做这样的事情:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mapView = new MapView(SingleEvent.this);
mapView.onCreate(savedInstanceState);
com.google.android.gms.maps.MapView.LayoutParams mapParams = new com.google.android.gms.maps.MapView.LayoutParams(com.google.android.gms.maps.MapView.LayoutParams.MATCH_PARENT,220);
mapContainer = (FrameLayout) findViewById(R.id.mapContainer);
mapContainer.addView(mapView, mapParams);
}
},1000);
但是使用该代码,地图并没有加载它只显示灰色的空白网格。 我认为这是因为 savedInstanceState 但也许有解决办法?
我尝试保存Bundle savedInstanceState,加载如上。
【问题讨论】:
-
如果地图仅在用户执行某个操作时才可见,那么当他们执行该操作时应该显示一个活动。在不保证会显示的情况下加载该点不是一个好主意
-
也许我可以以某种方式使用服务?
-
服务没有用户界面
-
您可能应该考虑使用带有
lite mode的静态地图,可用的是 google play services 6.5 developers.google.com/maps/documentation/android/lite
标签: android google-maps android-activity android-mapview