2
votes

I'm trying to make an android application using mapbox in which the system tracks the user's current location. When the application starts, it centers correctly using the current gps coordinates. When I try to change the current gps with the android emulator, the blue dot changes, but the map doesn't center in the changed position...What can I do? Here's my code!

public class MainActivity extends AppCompatActivity implements
            OnMapReadyCallback, PermissionsListener {
    private PermissionsManager permissionsManager;
    private MapboxMap mapboxMap;
    private MapView mapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Mapbox access token is configured here. This needs to be called either in your application
    // object or in the same activity which contains the mapview.
    Mapbox.getInstance(this, getString(R.string.access_token));

    // This contains the MapView in XML and needs to be called after the access token is configured.
    setContentView(R.layout.activity_main);

    mapView = findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(this);
}

@Override
public void onMapReady(MapboxMap mapboxMap) {
    MainActivity.this.mapboxMap = mapboxMap;
    enableLocationComponent();
}

// @SuppressLint("WrongConstant")
    @SuppressWarnings( {"MissingPermission"})
private void enableLocationComponent() {
    // Check if permissions are enabled and if not request
    if (PermissionsManager.areLocationPermissionsGranted(this)) {

        // Get an instance of the component
        LocationComponent locationComponent = mapboxMap.getLocationComponent();

        // Activate
        locationComponent.activateLocationComponent(this);

        // Enable to make component visible
        locationComponent.setLocationComponentEnabled(true);

        // Set the component's camera mode
        locationComponent.setCameraMode(CameraMode.TRACKING_GPS);

        // Set the component's render mode
        locationComponent.setRenderMode(RenderMode.COMPASS);
    } else {
        permissionsManager = new PermissionsManager(this);
        permissionsManager.requestLocationPermissions(this);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    permissionsManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

@Override
public void onExplanationNeeded(List<String> permissionsToExplain) {
    Toast.makeText(this, "This app needs location permissions in order to show its functionality", Toast.LENGTH_LONG).show();
}

@Override
public void onPermissionResult(boolean granted) {
    if (granted) {
        enableLocationComponent();
    } else {
        Toast.makeText(this, "You didn't grant location permissions.", Toast.LENGTH_LONG).show();
        finish();
    }
}

@Override
@SuppressWarnings( {"MissingPermission"})
protected void onStart() {
    super.onStart();
    mapView.onStart();
}

@Override
protected void onResume() {
    super.onResume();
    mapView.onResume();
}

@Override
protected void onPause() {
    super.onPause();
    mapView.onPause();
}

@Override
protected void onStop() {
    super.onStop();
    mapView.onStop();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mapView.onSaveInstanceState(outState);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
}
}
2
locationComponent.zoomWhileTracking(22); works a little bit but it's not the proper solution! - oreocookiemaster

2 Answers

1
votes

I have never used mapBox, I am a libgdx guy.

In spite of this, I think you should provide a listener that will listen GEOLOCATION changes and center the camera on the dot.

dont know any coding in mapbox but it might look like this:

if(GEOLOCATION_CHANGE > someValue){
camera.setPosition(x,y);    or   map.setPosition(x,y);   //can change cam or map pos
or  camera.setPosition(blueDot.getX(),blueDot.gety());}
0
votes

I would recommend building your app to a physical device to see if it behaves the same way when it receives location updates. Your code appears to be correct, but location services can sometimes be finicky when you build them to Android Studio's emulator.