Android google map on click of current location button show alert box if gps not enabled

北城以北 提交于 2019-12-08 10:58:21

问题


I have implemented Google map in my app. When i click on the current location button i wanted to show alert box if Gps is not enabled.

I tried this way where i got the view of that button.

    SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);


        View plusMinusButton = fm.getView().findViewById(1);
        View locationButton = fm.getView().findViewById(2);

Using that locationButton i tried to use setOnclickListener but that did not help.

My requirement is simple when i click on this image

if gps is not enabled then i have to show alert box saying gps is not enabled.

回答1:


This was a request and Google added it as a feature (in August 2013 I think). Now you can listen to clicks on the location button like this:

final Context context = this;
mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
    @Override
    public boolean onMyLocationButtonClick() {
        LocationManager mgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (!mgr.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Toast.makeText(context, "GPS is disabled!", Toast.LENGTH_SHORT).show();
        }
        return false;
    }
});


来源:https://stackoverflow.com/questions/25361367/android-google-map-on-click-of-current-location-button-show-alert-box-if-gps-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!