Android 为什么我的应用程序映射没有';GPS坐标中的t中心?
我正在尝试使用mapbox制作一个android应用程序,系统在其中跟踪用户的当前位置。当应用程序启动时,它将使用当前gps坐标正确居中。 当我尝试用android emulator更改当前gps时,蓝点会更改,但地图不会在更改的位置居中…我该怎么办?这是我的密码Android 为什么我的应用程序映射没有';GPS坐标中的t中心?,android,gps,mapbox,Android,Gps,Mapbox,我正在尝试使用mapbox制作一个android应用程序,系统在其中跟踪用户的当前位置。当应用程序启动时,它将使用当前gps坐标正确居中。 当我尝试用android emulator更改当前gps时,蓝点会更改,但地图不会在更改的位置居中…我该怎么办?这是我的密码 public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, PermissionsListener {
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();
}
}
public类MainActivity扩展了AppCompatActivity实现
在MapreadyCallback上,许可证管理员{
私人许可证管理人许可证管理人;
私有MapboxMap MapboxMap;
私有地图视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//此处配置了Mapbox访问令牌。这需要在应用程序中调用
//对象或位于包含mapview的同一活动中。
getInstance(这个,getString(R.string.access_令牌));
//它包含XML格式的MapView,需要在配置访问令牌后调用。
setContentView(R.layout.activity_main);
mapView=findViewById(R.id.mapView);
onCreate(savedInstanceState);
getMapAsync(这个);
}
@凌驾
mapready上的公共无效(MapboxMap MapboxMap){
MainActivity.this.mapboxMap=mapboxMap;
enableLocationComponent();
}
//@SuppressLint(“错误常量”)
@SuppressWarnings({“MissingPermission”})
私有void enableLocationComponent(){
//检查权限是否已启用,如果未启用,则请求
if(许可证管理人areLocationPermissionsGranted(本)){
//获取组件的实例
LocationComponent LocationComponent=mapboxMap.getLocationComponent();
//激活
locationComponent.activateLocationComponent(此);
//启用以使组件可见
locationComponent.setLocationComponentEnabled(true);
//设置零部件的相机模式
locationComponent.setCameraMode(CameraMode.TRACKING_GPS);
//设置组件的渲染模式
locationComponent.setRenderMode(RenderMode.COMPASS);
}否则{
permissionsManager=新的permissionsManager(此);
permissionsManager.requestLocationPermissions(此);
}
}
@凌驾
public void onRequestPermissionsResult(int-requestCode,@NonNull-String[]permissions,@NonNull-int[]grantResults){
permissionsManager.onRequestPermissionsResult(请求代码、权限、grantResults);
}
@凌驾
public void OneExplanationRequired(列出许可证解释){
Toast.makeText(此“此应用程序需要位置权限才能显示其功能”,Toast.LENGTH_LONG.show();
}
@凌驾
public void onPermissionResult(已授予布尔值){
如果(授予){
enableLocationComponent();
}否则{
Toast.makeText(这是“您没有授予位置权限”,Toast.LENGTH_LONG.show();
完成();
}
}
@凌驾
@SuppressWarnings({“MissingPermission”})
受保护的void onStart(){
super.onStart();
onStart();
}
@凌驾
受保护的void onResume(){
super.onResume();
onResume();
}
@凌驾
受保护的void onPause(){
super.onPause();
onPause();
}
@凌驾
受保护的void onStop(){
super.onStop();
mapView.onStop();
}
@凌驾
SaveInstanceState上受保护的无效(束超出状态){
super.onSaveInstanceState(超出状态);
mapView.onSaveInstanceState(outState);
}
@凌驾
受保护的空onDestroy(){
super.ondestory();
mapView.onDestroy();
}
@凌驾
public void onLowMemory(){
super.onLowMemory();
onLowMemory();
}
}
我从未使用过mapBox,我是一个libgdx的家伙
尽管如此,我认为您应该提供一个侦听器,它将侦听地理位置的变化,并将相机置于圆点的中心
不知道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的模拟器上时,它们有时会很挑剔 私有void setCameraPosition(Location-Location){mapboxMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new-LatLng(Location.getLatitude(),Location.getLatitude()),22));}公共void onLocation已更改(Location-Location){if(Location!=null){originLocation=Location;setCameraPosition(location);}}}我尝试过这个方法,但没有结果..locationComponent.zoomWhileTracking(22);有点效果,但它不是正确的解决方案!