Search code examples
androidgoogle-mapsmylocationoverlay

alert location services disabled when click mylocation button


I've enabled My Location button with this code in my google map

googleMap.setMyLocationEnabled(true);

When I click on My Location button

If the location services is disabled, related messages ("location services disabled") are not shown for me

I want this message to be shown if it is disabled, such as Google Maps software.


Solution

  • Well you can implement that yourself quite easily:

    googleMap.setOnMyLocationButtonClickListener(new GoogleMap
            .OnMyLocationButtonClickListener() {
        @Override
        public boolean onMyLocationButtonClick() {
            final LocationManager manager = (LocationManager)
                    getSystemService(Context.LOCATION_SERVICE);
    
            if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER) ) {
                Toast.makeText(MainActivity.this, "GPS not available!",
                        Toast.LENGTH_SHORT).show();
                return true; // GPS not available, consume the click
            }
            return false;
        }
    });