Recently I began to study the android and received from the teacher the task of writing a program that will determine the location, but alas, according to the materials that he provided, it is almost impossible to figure out what's what. If someone can, then explain to me why it does not work, what I was able to collect from those materials, and how I can do to make the program work.
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private GoogleMap mMap; private static final int PERMISSIONS_REQUEST_CODE = 10; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); // Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); requestPermissions(); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case PERMISSIONS_REQUEST_CODE: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { setUpMap(); } } } } private void requestPermissions() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_REQUEST_CODE); } } } private void setUpMap() { LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); String provider = locationManager.getBestProvider(criteria, true); LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { showCurrentLocationOnMap(location); } @Override public void onStatusChanged(String s, int i, Bundle bundle) { } @Override public void onProviderEnabled(String s) { } @Override public void onProviderDisabled(String s) { } }; locationManager.requestLocationUpdates(provider, 2000, 0, locationListener); Location location = locationManager.getLastKnownLocation(provider); if (location != null) { showCurrentLocationOnMap(location); } } private void showCurrentLocationOnMap(Location location) { mMap.clear(); LatLng currentPosition = new LatLng(location.getLatitude(), location.getLongitude()); mMap.addMarker(new MarkerOptions() .position(currentPosition) .snippet(location.getLatitude() + "'N " + location.getLongitude() + "'E") .flat(true) .title("Moja aktualna lokalizacja")); mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentPosition, 18)); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; // Add a marker in Sydney and move the camera LatLng sydney = new LatLng(-34, 151); mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); } }