title: Android 6 - Location permissions and map display
body: <p>I have implemented code to request for accessing location and that seems to be working fine. When the app is started for the very first time, it is showing a dialog box asking whether to allow access to the location or not.</p>
  
  <p>Once the access is granted, how is it possible to zoom to the current location? I have tried to restart the activity, but that does not help and all I get is the whole world map.</p>
  
  <p>If I exit the app and start, it works as expected. Is there a way to overcome this behaviour?</p>
  
  <pre><code>@Override
      public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
          switch (requestCode) {
  
              case LOCATION_REQUEST:
                  if (canAccessLocation()) {
                      //Tried to restart activity here 
                  } else {
                      System.err.println("Cannot access location");
                  }
                  break;
          }
      }
  </code></pre>
  
  <p>here is the part from onCreate where the map is zoomed into (in the method plotLocationOnMap)...</p>
  
  <pre><code>if (canAccessLocation()) {
      if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
                              ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
          mMap.setMyLocationEnabled(true);
          mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
              @Override
              public void onCameraChange(CameraPosition position) {
                  LatLng pos = position.target;
  
                  double distance = LineStation.getDistance(lastLat, lastLong, pos.latitude, pos.longitude);
                  if (distance &lt; 0.3) {
                      return;
              }
  
              lastLat = pos.latitude;
              lastLong = pos.longitude;
  
              plotLocationOnMap(pos.latitude, pos.longitude);
              timer = new Timer();
              timer.schedule(new RemindTask(pos.latitude, pos.longitude), 1000);
             }
          });
  
          mMap.setOnMapLongClickListener(this);
  
          double latitude = 0;
          double longitude = 0;
          Location myLocation = mMap.getMyLocation();
          if (myLocation != null) {
              plotLocationOnMap(myLocation.getLatitude(), myLocation.getLongitude());
          } else {
              GPSTracker gpst = new GPSTracker(context);
              latitude = gpst.getLatitude();
              longitude = gpst.getLongitude();
  
              plotLocationOnMap(latitude, longitude);
          }
      }
  } else {
     requestPermissions(LOCATION_PERMS, LOCATION_REQUEST);
  }
  
  private void plotLocationOnMap(double latitude, double longitude) {
          mMap.clear();
          Marker m1;
  
          String address = getAddress(latitude, longitude);
  
          m1 = mMap.addMarker(new MarkerOptions()
                  .position(new LatLng(latitude, longitude))
                  .icon(BitmapDescriptorFactory.fromResource(R.drawable.flag))
                  .title(address));
          m1.showInfoWindow();
  
          final LatLngBounds.Builder builder = new LatLngBounds.Builder();
          builder.include(m1.getPosition());
  
          CameraPosition cameraPosition = new CameraPosition.Builder()
                  .target(new LatLng(latitude, longitude))        // Sets the center of the map to Mountain View
                  .zoom(ZOOM)                                     // Sets the zoom
                  .bearing(0)                                     // Sets the orientation of the camera to east
                  .tilt(45)                                       // Sets the tilt of the camera to 45 degrees
                  .build();                                       // Creates a CameraPosition from the builder
          mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
      }
  </code></pre>