加载Google地图时的ProgressBar

时间:2018-04-19 13:38:36

标签: android google-maps loading

我从数据库获取地址并使用第一个Geocoder在Google Maps活动上标记它们,如果返回LatLng null,我使用asynctask从googleapi获取它。

代码效果很好,但是当选择500-600个地址时,加载地图需要一段时间(15-30秒,具体取决于数字)。

如何在地图加载时添加圆形progressBar?问题是在加载地图时,屏幕是黑色的。我尝试在asynctask中添加progressBar,但它不起作用。

即使在加载地图之后,asynctask仍然不断向地图添加引脚,你不知道它何时完成,请记住有很多引脚。

我在onCreate()中创建了progressBar并将其设置为不可见:

 progressBar = new ProgressBar(MapsActivity.this, null,android.R.attr.progressBarStyleSmall);
 progressBar.setVisibility(View.INVISIBLE);  

在asynctask中,在onPreExecute()上我将其设置为可见:

 @Override
 protected void onPreExecute() {
      progressBar.setVisibility(View.VISIBLE);
    }

2 个答案:

答案 0 :(得分:3)

您的问题基本上与用户体验有关,而与您的代码无关。

您可以使用两种解决方案来告诉用户应用正在做某事而不仅仅是冻结或崩溃:

解决方案1 ​​
您可以在加载信息时使用显示的加载视图而不是地图

<FrameLayout
     <!-- same layout information as your current MapView -->
     ...
     >
     <MapView
         ...
         />
     <View
         <!-- This can be a ProgressBar or a custom loading View with animation -->
         />
</FrameLayout>

然后,您可以在加载数据时生成查看Visible,然后在完成后显示地图..这将有助于避免黑屏

  

更新
  在answer之后,您可以找到扩展Fragment并添加MapView

设置显示地图的布局。 location_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.google.android.gms.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <View
        android:id="@+id/loadingView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

现在我们编写java类来显示Map。 MapViewFragment

public class MapViewFragment extends Fragment {

    MapView mMapView;
    View mLoadingView;
    private GoogleMap googleMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.location_fragment, container, false);

        mMapView = (MapView) rootView.findViewById(R.id.mapView);
        mLoadingView = rootView.findViewById(R.id.loadingView); // you can handle your view as you wish
        mMapView.onCreate(savedInstanceState);

        mMapView.onResume(); // needed to get the map to display immediately

        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }

        mMapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap mMap) {
                googleMap = mMap;

                // For showing a move to my location button
                googleMap.setMyLocationEnabled(true);

                // For dropping a marker at a point on the Map
                LatLng sydney = new LatLng(-34, 151);
                googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker Title").snippet("Marker Description"));

                // For zooming automatically to the location of the marker
                CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build();
                googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
            }
        });

        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mMapView.onLowMemory();
    }
}

这样,只要您完成提取数据,就可以隐藏/显示loadingView

解决方案2
您可以在加载数据之前仅显示地图,然后在获取数据时在其上方显示ProgressBar。加载数据后,您可以填充地图。

答案 1 :(得分:0)

来自here

public abstract void onMapLoaded ()

地图完成渲染后调用。这只会被调用一次。如果您想再次收到通知,则必须请求另一个回调。

这是在Android UI线程上调用的。

相关问题