0

I am trying to write an app for searching nearest places of interest, for example restaurants. My problem is that despite seemingly proper coding, the button that should move the map view to the current location does not appear. Here's my code, an excerpt from onMapReady function:

 if (mapView != null && mapView.findViewById(Integer.parseInt("1")) != null) {
            //View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("4"));
            View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            layoutParams.setMargins(0, 0, 30, 30);
        }      

I have tried looking for answers, but most of already given answers concern problems with just positioning and not not appearing at all. Android: Google Maps API - Change position of maps toolbar This question concerns the same problem, but in my application permissions are given on runtime, at least I think they are (There's a separate activity for grating permissions when the aplication is started for the first time). My location button is not showing in map fragment

user9507446
  • 333
  • 1
  • 3
  • 14

2 Answers2

0

Is this is what you are searching for?

map.setMyLocationEnabled(true);

map is object of GoogleMap

Vir Rajpurohit
  • 1,869
  • 1
  • 10
  • 23
  • No, adding this line does not change anything. Moreover, I already have `mMap.getUiSettings().setMyLocationButtonEnabled(true);` line and the button still does not appear. – user9507446 Nov 13 '19 at 11:46
0

you need to request permissios ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION at runtime

@Override
public void onMapReady(GoogleMap googleMap) {
            mMap= googleMap;
            if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, 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;
            }
            mMap.setMyLocationEnabled(true);
            mMap.getUiSettings().setMyLocationButtonEnabled(true);

}
Nensi Kasundra
  • 1,980
  • 6
  • 21
  • 34