如何在GoogleMap活动完全加载之前显示进度条

时间:2017-05-23 22:18:34

标签: android google-maps android-asynctask android-progressbar

我的一项活动中有一个Mapview。只要单击按钮并打开包含Mapview的活动,它就会冻结当前活动UI,直到完全加载MapView的活动。我在onCreate()方法中有以下代码:

mMapView = (MapView) findViewById(R.id.map);
    if(mMapView != null){
        mMapView.onCreate(null);
        mMapView.onResume();
       mMapView.getMapAsync(JobDetail.this);
    }

并且活动本身实现了OnMapReadyCallback:

@Override
public void onMapReady(GoogleMap googleMap) {

    MapsInitializer.initialize(JobDetail.this);

    mGoogleMap = googleMap;
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    if(latitude!= null && longitude!= null){
        googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude)).title(post_location));
        CameraPosition markposition = CameraPosition.builder().target(new LatLng(latitude,longitude)).zoom(16).bearing(0).tilt(45).build();
        googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(markposition));
    }
}

在googlemapview活动完全加载并准备好显示之前,如何使屏幕变暗并显示进度条?我已经阅读了一些建议使用Asynctask的建议,但我怎样才能使用GoogleMapView?

2 个答案:

答案 0 :(得分:0)

您可以做的是:在调用活动时显示进度,在onMapReady()中解除或隐藏进度并将可见性属性更改为VISIBLE。 这样的事情:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mProgressBar = (ProgressBar) findViewById(R.id.progressbar);
    mMapView = (MapView) findViewById(R.id.map);
    mMapView.setVisibility(View.INVISIBLE);
    progressBar.setVisibility(View.VISIBLE);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    MapsInitializer.initialize(JobDetail.this);

    mGoogleMap = googleMap;
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    if(latitude!= null && longitude!= null){
        googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude)).title(post_location));
        CameraPosition markposition = CameraPosition.builder().target(new LatLng(latitude,longitude)).zoom(16).bearing(0).tilt(45).build();
        googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(markposition));
    }

    mMapView.setVisibility(View.VISIBLE);
    progressBar.setVisibility(View.INVISIBLE);
}

答案 1 :(得分:0)

  

它会冻结UI,直到活动完全加载

我认为加载地图不应该首先冻结用户界面。我有一个应用程序,在一个活动中,地图占据整个房地产和另一个活动,其中地图占据屏幕的一半,以及一些图像和文本视图。在这两种情况下,我都没有在UI中看到任何冻结。请参阅下面的代码段。

mMapView = (MapView) rootView.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume(); // needed to get the map to display immediately

try {
    Log.i(TAG, "initializing map");
    MapsInitializer.initialize(mContext);
} catch (Exception e) {
    Log.e(TAG, "error while initializing map screen" + e);
    e.printStackTrace();
}

mMapView.getMapAsync(new OnMapReadyCallback() {
    @Override
    public void onMapReady(GoogleMap mMap) {
        googleMap = mMap;
        googleMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
            @Override
            public void onCameraIdle() {
                mCameraReady = true;
                if(latitude!= null && longitude!= null){
                    googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude)).title(post_location));
                    CameraPosition markposition = CameraPosition.builder().target(new LatLng(latitude,longitude)).zoom(16).bearing(0).tilt(45).build();
                    googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(markposition));
                }
                googleMap.setOnCameraIdleListener(null);
            }
        });
    }
});

除了

之外,它与你的相似
  1. 调用MapsInitializer.initialize()的点。
  2. 在绘制标记和缩放之前,我等待相机准备就绪。
  3. 我不认为#1很重要,按照Android documentation,呼叫本身并不是必需的。所以给#2一个镜头。

    希望这有帮助。

相关问题