Risk
Risk

Reputation: 111

Progress dialog on Google map until current location is not shown

enter image description here

Code :

-- Camera focus on current location

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));

Upvotes: 0

Views: 2009

Answers (1)

DJphy
DJphy

Reputation: 1302

In your xml where u have ur map; declare a progress bar, like this; intially its visibility will be gone(mean it will be hidden):

<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" />

Then initialize ur progressbar in the activity where ur displaying ur map; declare it as a class variable:

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

Then in the oncreate of ur map activity set the visibility of the progressbar to visible:

mProgressBar.setVisibility(View.VISIBLE);

Then at the place where ur calling gmap.AnimateCamera(i.e when ur zooming) where location is not null if clause; u set the visibility to gone:

mProgressBar.setVisibility(View.GONE);

Enjoy

Upvotes: 4

Related Questions