为什么我的应用程序地图不以 GPS 坐标为中心?

why my application map doesn't center in GPS coordinates?

我正在尝试使用 mapbox 创建一个 android 应用程序,系统会在其中跟踪用户的当前位置。当应用程序启动时,它会使用当前的 gps 坐标正确居中。 当我尝试使用 android 模拟器更改当前 gps 时,蓝点发生了变化,但地图并未在更改后的位置居中...我该怎么办?这是我的代码!

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();
}
}

我没用过mapBox,我是libgdx人

尽管如此,我认为您应该提供一个侦听器来侦听 GEOLOCATION 变化并将相机置于点的中心。

不知道 mapbox 中的任何编码,但它可能看起来像这样:

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());}

我建议将您的应用程序构建到物理设备上,以查看它在接收位置更新时的行为是否相同。您的代码似乎是正确的,但是当您将位置服务构建到 Android Studio 的模拟器时,它们有时会很挑剔。