Pick up current location after connecting GPS - android

Asked

Viewed 56 times

1

I am new to Android and am in need of any help.

I’m making an app that when opening, I want to be displayed the map with the current location of the user.

The problem is that when the GPS is turned off, I open a popup asking the user to enter into Settings and turn on. However, while the user does this, the app has already loaded the map without the GPS on there I can no longer catch the current location. It’s async.

How do I create a loading popup to load the map only after the user turns on the GPS and the GPS is calibrated?

I’m using Fragment because I made a Drawer navigation.

public class MapFragment extends Fragment implements LocationListener {

    private View rootView;
    private GPSTracker gpsTracker;
    private GoogleMap map;
    double latitude;
    double longitude;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.map, container, false);

        //call this method to check gps enable or not
        setLocation();

        return rootView;
    }

    public void setLocation() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            gpsTracker = new GPSTracker(getContext());
        }

        if (gpsTracker.canGetLocation()) {
            latitude = gpsTracker.getLatitude();
            longitude = gpsTracker.getLongitude();
            //position found, show in map
            setMap(latitude, longitude);
        } else {
            // can't get location
            // GPS or Network is not enabled
            // Ask user to enable GPS/network in settings
            gpsTracker.showSettingsAlert();
        }
    }

    //this method is to show map
    public void setMap(final double latitude, final double longitude) {
        MapView mapView = (MapView) rootView.findViewById(R.id.mapView);
        mapView.onCreate(null);
        mapView.onResume();
        mapView.getMapAsync(
                new OnMapReadyCallback() {
                    @Override
                    public void onMapReady(GoogleMap googlemap) {
                        map = googlemap;

                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            MapsInitializer.initialize(getContext());
                        }
                        //change map type as your requirements
                        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                        //user will see a blue dot in the map at his location

                        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), 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;
                        }
                        map.setMyLocationEnabled(true);
                        LatLng marker = new LatLng(latitude, longitude);

                        //move the camera default animation
                        map.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, Constants.CAMERA_ZOOM));

                        //add a default marker in the position
                        map.addMarker(new MarkerOptions()
                                .position(marker));

                    }
                }
        );
    }

    @Override
    public void onLocationChanged(Location location) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onProviderDisabled(String provider) {

    }

}

Method showing the popup

public void showSettingsAlert(){
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

        // Setting Dialog Title
        alertDialog.setTitle("GPS is settings");

        // Setting Dialog Message
        alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

        // On pressing Settings button
        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mContext.startActivity(intent);
            }
        });

        // on pressing cancel button
        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        // Showing Alert Message
        alertDialog.show();
    }

** I’m not worried about the permits yet.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.