0

I'm trying to get the current location icon on top of the map. However when i run the app the map looks;

Here is the map's screenshot

The other case is when i wrote this line;

 mGoogleMap.setMyLocationEnabled(true);

I could't get any 'Add Permission Check' warning like; enter image description here

. So i wrote these permissions by hand.

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)

            if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }

    }

I don't know its right way to do that or not. Here is my full code;

    private Activity activity = this;
    ActionBarDrawerToggle drawerToggle;
    DrawerLayout drawerLayout;
    RecyclerView recyclerMenu;
    AdapterMenu obAdapter;
    private Tracker mTracker;
    GoogleMap mGoogleMap;
    GoogleApiClient mGoogleApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_harita);

        if (googleServicesAvailable()) {

            initmap();
        } else {
            //No google maps layout
        }


    }


    public boolean googleServicesAvailable() {
        GoogleApiAvailability api = GoogleApiAvailability.getInstance();
        int isAvailable = api.isGooglePlayServicesAvailable(this);
        if (isAvailable == ConnectionResult.SUCCESS)
            return true;
        else if (api.isUserResolvableError(isAvailable)) {
            Dialog dialog = api.getErrorDialog(this, isAvailable, 0);
            dialog.show();
        } else
            Toast.makeText(this, "Can't connect to play services", Toast.LENGTH_LONG).show();

        return false;
    }

    private void initmap() {
        MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.fragment);
        mapFragment.getMapAsync(this);
    }

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

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)

                if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }

        }
        mGoogleMap.setMyLocationEnabled(true);


    }

    public void geoLocate(View view) throws IOException {
        EditText searchtext = (EditText) findViewById(R.id.editAra);
        String location = searchtext.getText().toString();

        Geocoder geocoder = new Geocoder(this);
        List<Address> list = geocoder.getFromLocationName(location, 1);
        Address address = list.get(0);
        String locality = address.getLocality();

        Toast.makeText(this, locality, Toast.LENGTH_LONG).show();

        double latitude = address.getLatitude();
        double longitude = address.getLongitude();

        goToLocationZoom(latitude, longitude, 15);
    }

    private void goToLocationZoom(double latitude, double longitude, float zoom) {

        LatLng LatLang = new LatLng(latitude, longitude);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LatLang, zoom);
        mGoogleMap.moveCamera(update);
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
dizel
  • 25
  • 6

1 Answers1

1

If user has not granted for location permissions, you should request for it explicitly (Only required for Android M and above). Then you should override onRequestPermissionsResult and manage the results.

Here you have a working example for you are asking for:

public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
private static final String[] LOCATION_PERMISSIONS = {
        Manifest.permission.ACCESS_FINE_LOCATION,
        Manifest.permission.ACCESS_COARSE_LOCATION
};
private static final int LOCATION_PERMISSIONS_REQUEST_CODE = 1;

private GoogleMap mGoogleMap;

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public void onMapReady(final GoogleMap googleMap) {
    this.mGoogleMap = googleMap;
    if (hasGrantedLocationPermissions()) {
        showCurrentLocationMapControl();
    } else {
        requestForLocationPermissions();
    }
}

@Override
public void onRequestPermissionsResult(final int requestCode, final @NonNull String[] permissions, final @NonNull int[] grantResults) {
    if (requestCode == LOCATION_PERMISSIONS_REQUEST_CODE) {
        if (grantResults.length == LOCATION_PERMISSIONS.length) {
            for (final int grantResult : grantResults) {
                if (PackageManager.PERMISSION_GRANTED != grantResult) {
                    return;
                }
            }
            showCurrentLocationMapControl();
        }
    }
}

private boolean hasGrantedLocationPermissions() {
    return ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}

private void requestForLocationPermissions() {
    ActivityCompat.requestPermissions(this, LOCATION_PERMISSIONS, LOCATION_PERMISSIONS_REQUEST_CODE);
}

private void showCurrentLocationMapControl() {
    if (null != this.mGoogleMap) {
        this.mGoogleMap.setMyLocationEnabled(true);
    }
}

}

Do not forget to define needed permissions in manifest file:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
AlexTa
  • 5,133
  • 3
  • 29
  • 46