Search code examples
javaandroidgoogle-mapsandroid-maps-v2

problem with my location in android studio


good evening everybody i want to tell you my problem , so i want that in google maps in my android appears the button that take to my current location but when i try it in my real phone (samsung SM-J730G android 7.0 api 24) the little button does not appear (i did not try it in the emulator cause cause i doesnt work too) . so i would like to know whats the problem please friends help me here THANKS SO MUCH FOR YOUR HELPING is the code:

public class MapFragment extends Fragment implements View.OnClickListener {


private View rootView;
private MapView mapView;
private GoogleMap mMap;
private FloatingActionButton fab;


public MapFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    this.rootView = inflater.inflate(R.layout.fragment_map, container, false);
    this.fab = (FloatingActionButton) rootView.findViewById(R.id.fab);
    return rootView;
}


 @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    this.fab.setOnClickListener(this);
    this.mapView = (MapView) this.rootView.findViewById(R.id.map);
    if (mapView != null) {
        this.mapView.onCreate(null);
        this.mapView.onResume();
        this.mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                mMap = googleMap;
                if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                mMap.setMyLocationEnabled(true); //I DONT KNOW WHY IT DOES NOT WORK


                  }
              });

    }



    super.onViewCreated(view, savedInstanceState);
}

ohh by the way i debbuged it and

 if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    return;   //is entrying here and i donw know why if i have the perssions in the manifest the accces_fine_location and acces_coarse_location
                }
                mMap.setMyLocationEnabled(true);   


please somebody help me thanks you

Solution

  • You have only checked the run time permission,if it's not provided you have not requested for the permission.Update the code like below and onRequestPermissionsResult check if permission provided set mMap.setMyLocationEnabled(true); from there

         @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            this.fab.setOnClickListener(this);
            this.mapView = (MapView) this.rootView.findViewById(R.id.map);
            if (mapView != null) {
               his.mapView.getMapAsync(new OnMapReadyCallback() {
                    @Override
                    public void onMapReady(GoogleMap googleMap) {
                        mMap = googleMap;
                        if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    
      ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
                            return;
                        }
                        mMap.setMyLocationEnabled(true); //I DONT KNOW WHY IT DOES NOT WORK
    
    
                          }
                      });
    
            }
    

    On Permission result

    @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            if(requestCode == 101) {
      f (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    mMap.setMyLocationEnabled(true);  }
      }                 
    }