ARTICLE AD BOX
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(); } } }22k7 gold badges47 silver badges60 bronze badges
New contributor
Noam_B92 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Explore related questions
See similar questions with these tags.
