首页 > 解决方案 > Android:如何让片段仅在地图加载完成时出现?

问题描述

我正在尝试为我的地图活动显示进度对话框。问题是,有时代码可以工作并且地图会立即加载,但有时它会滞后并且永远让用户在左下方屏幕出现白屏和谷歌图标。

下面基本上是我正在使用的方法,但是当活动加载时我根本看不到进度对话框。有人有什么建议吗?也许我正在接近这个错误。

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private ProgressDialog mapLoadingProgress;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);


    mapLoadingProgress = new ProgressDialog(this);
    mapLoadingProgress.setMessage("Loading map");
    mapLoadingProgress.show();
}

@Override
public void onMapReady(GoogleMap googleMap) {

    mapLoadingProgress.dismiss();
    mMap = googleMap;

    markCurrentAddress();
    markMyLocation();
}

}

这是我的布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.lusayo.mainproject.MapsActivity">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:id="@+id/branchSelector"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp">

        <Spinner
            android:layout_width="match_parent"
            android:background="@color/nfb_maroon"
            android:textColor="#fff"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_height="match_parent"
            android:id="@+id/branchSpinner"
            android:drawSelectorOnTop="true"
            android:layout_weight="1" />
    </LinearLayout>

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
        xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_below="@id/branchSelector"
    android:layout_height="match_parent"
    tools:context="com.lusayo.mainproject.MapsActivity"
        map:uiRotateGestures="true"
        map:uiScrollGestures="true"
        map:uiTiltGestures="true"
        map:uiZoomControls="false"
        map:uiZoomGestures="true"
        map:uiMapToolbar="true"
    tools:layout="@layout/maps_preview"
    android:layout_above="@+id/map_controls" />


    <RelativeLayout
        android:id="@+id/map_controls"
        android:background="@color/nfb_maroon"
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_height="50dp"
        android:layout_alignParentStart="true">

        <Button
            android:text="Get Directions"
            android:layout_width="wrap_content"
            android:background="@color/nfb_maroon"
            android:textColor="#fff"
            android:layout_height="wrap_content"
            android:id="@+id/getDirectionsButton"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true" />
    </RelativeLayout>

</RelativeLayout>

标签: javaandroid

解决方案


尝试调用:

mapLoadingProgress = new ProgressDialog(this);
mapLoadingProgress.setMessage("Loading map");
mapLoadingProgress.show();

前,

SupportMapFragment mapFragment = (SupportMapFragment) 
getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

如果它不起作用,请尝试在之后转移setContentView(R.layout.activity_maps);onMapReady()方法mapLoadingProgress.dismiss();


推荐阅读