Google 地图上的进度对话框,直到未显示当前位置

Progress dialog on Google map until current location is not shown

代码:

-- 相机聚焦当前位置

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                Criteria criteria = new Criteria();

                if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
                if (location != null)
                {
                    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                            new LatLng(location.getLatitude(), location.getLongitude()), 19));

                    CameraPosition cameraPosition = new CameraPosition.Builder()
                            .target(new LatLng(location.getLatitude(), location.getLongitude()))      // Sets the center of the map to location user
                            .zoom(19)                   // Sets the zoom
                            .bearing(90)                // Sets the orientation of the camera to east
                                           // Sets the tilt of the camera to 30 degrees
                            .build();                   // Creates a CameraPosition from the builder
                    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

在您的 xml 中,您有自己的地图;像这样声明一个进度条;最初它的可见性将消失(意味着它将被隐藏):

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleSmall"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="45dp"
    android:padding="5dp"
    android:visibility="gone" />

然后在显示地图的 activity 中初始化进度条;将其声明为 class 变量:

mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
mProgressBar.setIndeterminate(true);

然后在你的地图的oncreate中activity将进度条的可见性设置为visible:

mProgressBar.setVisibility(View.VISIBLE);

然后在你调用 gmap.AnimateCamera 的地方(即当你缩放时),如果子句的位置不为空;你将可见性设置为消失:

mProgressBar.setVisibility(View.GONE);

尽情享受