Android Permission Request Method when Using Maps

  • 2021-09-24 23:44:11
  • OfStack

Request location permission when initializing your own location:

Constants.ACCESS_FINE_LOCATION_COMMANDS_REQUEST_CODE is a custom constant value = = 0x01


if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
   != PackageManager.PERMISSION_GRANTED) {
  // Application WRITE_EXTERNAL_STORAGE Authority 
  ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, Constants.ACCESS_FINE_LOCATION_COMMANDS_REQUEST_CODE);
 } else {
  /**
  *  Initialize user location 
  */
  setMyLoctionPicture();
 }

/**
  *  Initialize user location 
  */
 public void setMyLoctionPicture(){
  LogUtils.d(TAG,"setMyLocationPicture() is running");

  if(aMap==null){
   aMap=mapview.getMap();
  }
  MyLocationStyle locationStyle = new MyLocationStyle();
  locationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.mipmap.location_marker));
  locationStyle.strokeColor(Color.BLACK);
  locationStyle.radiusFillColor(Color.argb(100, 0, 0, 100));
  locationStyle.strokeWidth(1.0f);
  aMap.setMyLocationStyle(locationStyle);
  aMap.setLocationSource(this);
  UiSettings uiSettings = aMap.getUiSettings();
  uiSettings .setMyLocationButtonEnabled(true);

  aMap.setMyLocationEnabled(true);
  aMap.setInfoWindowAdapter(this);
  aMap.setOnMarkerClickListener(this);

 }

This is to ensure that the location permission will not be blocked in the high version of Android; As a result, positioning and navigation can not be used.


Related articles: