简体   繁体   English

为什么我的应用地图没有以 GPS 坐标为中心?

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

I'm trying to make an android application using mapbox in which the system tracks the user's current location.我正在尝试使用 mapbox 制作一个 android 应用程序,其中系统会跟踪用户的当前位置。 When the application starts, it centers correctly using the current gps coordinates.当应用程序启动时,它会使用当前的 GPS 坐标正确居中。 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?当我尝试使用 android 模拟器更改当前的 GPS 时,蓝点会发生变化,但地图并未在更改后的位置居中...我该怎么办? 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();
}
}

I have never used mapBox, I am a libgdx guy.我从未使用过 mapBox,我是一个 libgdx 人。

In spite of this, I think you should provide a listener that will listen GEOLOCATION changes and center the camera on the dot.尽管如此,我认为您应该提供一个监听器来监听 GEOLOCATION 的变化并将相机放在点上。

dont know any coding in mapbox but it might look like this:不知道 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());}

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.您的代码看起来是正确的,但是当您将它们构建到 Android Studio 的模拟器时,位置服务有时会很挑剔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM