当谷歌地图加载时显示进度条

4

我从数据库中获取地址,并使用Geocoder在Google Maps活动中标记它们。如果返回的LatLng为空,我会使用asynctask从googleapi获取它。

代码工作得很好,但当选择500-600个地址时,加载地图需要一段时间(取决于数量,需15-30秒左右)。

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

即使在地图加载完毕后,asynctask仍在将标记添加到地图上,无法知道何时完成,记住有很多标记。

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

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

在异步任务中,在onPreExecute()方法中,我将其设置为可见:
 @Override
 protected void onPreExecute() {
      progressBar.setVisibility(View.VISIBLE);
    }

现在屏幕黑屏15-30秒?那么您需要在问题中添加更多信息。请添加您的“AsyncTask”,并告诉我们您在哪里调用它。屏幕不应该因任何原因而变黑。 - Barns
那段时间屏幕是黑色的,因为应用程序需要向数据库发出查询,收集地址,500-600甚至更多,通过Geocoder传递它们,如果返回null,则通过googleapis/Maps传递它们。所以,是的,这需要一些时间。我认为这不是错误,只是需要时间。我只想添加一些内容,让用户知道它正在处理中,应用程序没有崩溃。 - Alex E.
2个回答

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>

你可以在数据加载时将View设为Visible,然后等到地图加载完成后再显示。这样可以避免黑屏的问题。
更新: 根据这个答案,你可以扩展一个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类。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。一旦数据加载完成,您就可以填充地图。

我喜欢第一个解决方案,但由于Maps Activity是一个片段,我不知道如何或是否可能在片段上实现视图。 - Alex E.
你可以展示你的加载视图--> 加载数据,然后用地图片段替换视图..所有这些都在你的活动中完成..否则,你可以扩展一个简单的片段,并添加一个带有MapView而不是MapFragment的布局。 - ColdFire
对不起,我是个初学者,这是我的第一个项目。我明白你的意思,但我不知道该怎么做。 - Alex E.
没问题,我更新了我的答案,说明如何创建一个带有mapView和loadingView的片段。 - ColdFire

0

这里开始

public abstract void onMapLoaded ()

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

此方法在Android UI线程上调用。


如果我在那个方法中添加对话框,是否也会解决黑屏问题?因为似乎它已经在黑屏状态下崩溃了30秒或更长时间。根据文档,该方法是用于地图加载完成时的事件:如果由于连接问题导致地图永远无法加载,或者如果地图不断变化并且由于用户不断与地图交互而无法完成加载,则此事件将不会触发。 - Alex E.
最初在地图加载之前,您可以使“progressBar”可见。一旦地图加载完成,在onMapLoaded()中,您可以将其隐藏。如果地图加载一次,则无论它是否持续更改都没有任何区别。 - user4035628

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接