我正在尝试使用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();
}
}
答案 0 :(得分:1)
我从没用过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());}
答案 1 :(得分:0)
我建议您在物理设备上构建您的应用,以查看其收到位置更新后的行为是否相同。您的代码似乎是正确的,但是当您将位置服务构建到Android Studio的模拟器时,位置服务有时可能会显得有些挑剔。