Probleme when using geolocation button after activating location in-app

56 views Asked by At

i've a big probleme for 4 months now, and i don't know what's the solution. When i'm launching my application, the app demand to the user if he want to activate the loalisation (its important), if he say "no", then nothing happen but the app will ask him later, and if he say "yes", then the app will open setting location of the app, and the user need to press "activate location" on phone setting.

The probleme is, after activating location (it doesn't show the userLocation bc i don't know how to do this and its not my first priority), when you click on the geolocalisationButton , you need to press 3 time the button for showing user his location (sometime 2, or 4 times, it's depend, i don't know why).

This is the video for the exemple --> video

onCreate Code

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_layout_map_priere);

    supportMapFragment = (SupportMapFragment)
    getSupportFragmentManager().findFragmentById(R.id.google_map);
    //location
       client = LocationServices.getFusedLocationProviderClient(layout_map_priere.this);

    //check Permissions
    if (ActivityCompat.checkSelfPermission(layout_map_priere.this,
            Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && 
            ActivityCompat.checkSelfPermission(layout_map_priere.this, 
            Manifest.permission.ACCESS_COARSE_LOCATION) == 
            PackageManager.PERMISSION_GRANTED) {
        //When both permission granted
        //Call method
        getCurrentLocation();
    }else {
        //When permissions denied
        //Request permission
        ActivityCompat.requestPermissions(layout_map_priere.this, new String[]{
                Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION
        }, 100);

    }
    trackButton();
   
}

My Customize Geolocalisation Button

private void geoButton() {
    locationButton = (View) supportMapFragment.getView().findViewById(Integer.parseInt("1")).getParent();
    locationButton = supportMapFragment.getView().findViewById(Integer.parseInt("2"));
    locationButton.setVisibility(View.GONE);
}

onClickable Geolocalisation Button

private void trackButton() {
    final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    ImageButton camera_track = (ImageButton) findViewById(R.id.back_to_camera_tracking_mode);
    geoButton();
    camera_track.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER) && 
                manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                getCurrentLocation();
            }else {
                buildAlertMessageNoGps();
            }
        }
    });

}

Getting The User Location

@SuppressLint("MissingPermission")
private void getCurrentLocation() {
    //Initialize task Location
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    //check Condition
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
        //When Location service is enabled
        //Get Last Location
        client.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
            @Override
            public void onComplete(@NonNull Task<Location> task) {
                Location location = task.getResult();
                //Check condition
                if (location != null){
                    //Sync map
                    supportMapFragment.getMapAsync(new OnMapReadyCallback() {
                        @Override
                        public void onMapReady(GoogleMap map) {
                            //initialize Lat Lng
                            LatLng myposition = new LatLng(location.getLatitude(), location.getLongitude());
                            map.animateCamera(CameraUpdateFactory.newLatLngZoom(myposition, 12));
                            LocationRequest locationRequest = LocationRequest.create();
                            locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

                            if (ActivityCompat.checkSelfPermission(layout_map_priere.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(layout_map_priere.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                                return;
                            }
                            map.setMyLocationEnabled(true);

                            locationButton = (View) supportMapFragment.getView().findViewById(Integer.parseInt("1")).getParent();
                            locationButton = supportMapFragment.getView().findViewById(Integer.parseInt("2"));
                            locationButton.setVisibility(View.GONE);

                            View toolbar = ((View) supportMapFragment.getView().findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("4"));
                            RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) toolbar.getLayoutParams();
                            rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
                            rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
                            rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
                            rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
                            rlp.setMargins(0, 0, 30, 30);
                            map.getUiSettings().setCompassEnabled(false);
                            map.getUiSettings().setMapToolbarEnabled(false);

                            map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
                                @Override
                                public void onMapClick(LatLng latLng) {
                                    final LinearLayout layout_scroll = (LinearLayout) findViewById(R.id.layout_scroll);
                                    final ViewGroup.LayoutParams params = layout_scroll.getLayoutParams();
                                    layout_scroll.setVisibility(View.INVISIBLE);
                                    params.height = 0;
                                    layout_scroll.setLayoutParams(params);

                                    final LinearLayout layout_more_block = (LinearLayout) findViewById(R.id.layout_more_block);
                                    final ViewGroup.LayoutParams params_more = layout_more_block.getLayoutParams();
                                    layout_more_block.setVisibility(View.INVISIBLE);
                                    params_more.height = 0;
                                    layout_more_block.setLayoutParams(params_more);
                                    Animation slideDown = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down);
                                    layout_more_block.startAnimation(slideDown);
                                }
                            });
                        }
                    });
                }else {
                    LocationRequest locationRequest = new LocationRequest()
                            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                            .setInterval(10000)
                            .setFastestInterval(1000)
                            .setNumUpdates(1);
                    //Initialize location call back
                    LocationCallback locationCallback = new LocationCallback(){
                        @Override
                        public void onLocationResult(LocationResult locationResult) {
                            //initalize location
                            Location location1 = locationResult.getLastLocation();
                        }
                    };
                    //Request location updates
                    client.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
                }
            }
        });
    }else {
        buildAlertMessageNoGps();
    }
}

Finnaly, My Builded Alert Message If No Gps

private void buildAlertMessageNoGps() {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Votre GPS semble être désactivé. Voulez-vous l'activer ?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(final DialogInterface dialog, final int id) {
                    startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));

                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(final DialogInterface dialog, final int id) {
                    dialog.cancel();
                }
            });
    final AlertDialog alert = builder.create();
    alert.show();
}

I really need your help, why i have this problem, and how can i fix it

0

There are 0 answers