I'm trying to zoom in on the user's location but it doesn't work


It's for a school project. No matter how much i've tried changing the value or the command it just loads the map and move only a little bit (not even zooming) it's suppoesd to be a running app that tracks your location at real time , it's not really neccesery but i want it to zoom in on the user's location when he opens the map

public class Map extends AppCompatActivity {
    boolean hasCenteredOnce = false;
    MyLocationNewOverlay mLocationOverlay;
    MapView map;
    LocationManager locationManager;
    LocationListener locationListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Configuration.getInstance().setUserAgentValue(getPackageName());
        setContentView(R.layout.activity_map);
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        }

        map = findViewById(R.id.map);
        map.setMultiTouchControls(true);

            int size = 60;
            Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            Paint paint = new Paint();
            paint.setColor(Color.RED);
            paint.setStyle(Paint.Style.FILL);
            paint.setAntiAlias(true);
            canvas.drawCircle(size / 2f, size / 2f, size / 2.5f, paint);

        mLocationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(this), map);
        mLocationOverlay.enableMyLocation();
        mLocationOverlay.enableFollowLocation(); // This makes the map follow the user
        mLocationOverlay.setDrawAccuracyEnabled(true); // Shows the blue circle accuracy
        mLocationOverlay.setPersonIcon(bitmap);
        mLocationOverlay.setDirectionIcon(bitmap);
        map.getOverlays().add(mLocationOverlay);

        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        locationListener = location -> {
            double lat = location.getLatitude();
            double lon = location.getLongitude();
            GeoPoint userPoint = new GeoPoint(lat, lon);

            // 2. Only zoom and snap the first time location is found
            // This prevents the map from "jumping" while the user is trying to      look around
            if (!hasCenteredOnce) {
                map.getController().setCenter(userPoint);
                map.getController().setZoom(17.0);
                map.getController().animateTo(userPoint);
                hasCenteredOnce = true;
            }
        };


    }

    @Override
    protected void onResume() {
        super.onResume();
        map.onResume();
        mLocationOverlay.enableMyLocation();
        mLocationOverlay.enableFollowLocation();
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            try {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        2000,
                        2,
                        locationListener);
            } catch (SecurityException e) {
                e.printStackTrace();
            }
        }
    }

0
Mar 3 at 8:09 PM
User AvatarNoam_B92
#java#android#openstreetmap#osmdroid

Accepted Answer

Instead of relying on LocationManager, let MyLocationNewOverlay notify you when the first location is available.

Replace your manual listener logic with this:

mLocationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(this), map);
mLocationOverlay.enableMyLocation();
mLocationOverlay.setDrawAccuracyEnabled(true);
mLocationOverlay.setPersonIcon(bitmap);
mLocationOverlay.setDirectionIcon(bitmap);

map.getOverlays().add(mLocationOverlay);

mLocationOverlay.runOnFirstFix(() -> {
    runOnUiThread(() -> {
        GeoPoint userPoint = mLocationOverlay.getMyLocation();
        if (userPoint != null) {
            map.getController().setZoom(18.0);
            map.getController().animateTo(userPoint);
        }
    });
});

This waits until GPS actually returns a location before zooming.

Also,

Remove enableFollowLocation(). Since you're manually centering:

mLocationOverlay.enableFollowLocation();

Remove it, because it overrides your map controller.

Also,

Remove the LocationManager code. MyLocationNewOverlay already handles GPS updates internally.

So delete:

locationManager
locationListener
requestLocationUpdates()
User AvatarSomil Jain
Mar 4 at 1:23 PM
1