-2

I have been trying to make Google maps display my current location, but it just shows me the whole map. Please how do I go about this, here is my code

 @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(1000);
    mLocationRequest.setFastestInterval(1000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    mMap.animateCamera(CameraUpdateFactory.zoomTo(11));

    if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
        if(ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){

        }else{
            checkLocationPermission();
        }
    }

    mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
    mMap.setMyLocationEnabled(true);
}

private void checkLocationPermission() {
    if(ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.ACCESS_FINE_LOCATION)) {
            new android.app.AlertDialog.Builder(this)
                    .setTitle("give permission")
                    .setMessage("give permission message")
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            ActivityCompat.requestPermissions(WorkerMap.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
                        }
                    })
                    .create()
                    .show();
        }
        else{
            ActivityCompat.requestPermissions(WorkerMap.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        }
    }
}
LocationCallback mLocationCallback = new LocationCallback(){
    @Override
    public void onLocationResult(LocationResult locationResult) {
        for(Location location : locationResult.getLocations()){
            if(getApplicationContext()!=null){
                mLastLocation = location;

                LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude());

                mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
                mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
            }
        }
    }
};

It's supposed to get my current location, but it's not doing that. What might be wrong please

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    Possible duplicate of [How can I show current location on a Google Map on Android Marshmallow?](https://stackoverflow.com/questions/34582370/how-can-i-show-current-location-on-a-google-map-on-android-marshmallow) – Abner Escócio Aug 11 '18 at 17:28
  • The Fused Location API used in the the answer you directed me to is deprecated – Clinton Oyelami Aug 12 '18 at 06:54

1 Answers1

0

1) You must to call this

mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
mMap.setMyLocationEnabled(true);

after you will get permissions from user, for example in function onRequestPermissionsResult.

2) Try to use mFusedLocationClient.getLastLocation() before you will make your request.

3) on which device you test this code?

Vadim Eksler
  • 865
  • 9
  • 24