【问题标题】:Getting the latitude and longitude in the Log console in mapbox在 mapbox 的 Log 控制台中获取经纬度
【发布时间】:2019-01-14 08:40:42
【问题描述】:

我正在尝试将经纬度坐标作为精确的浮点数,以便为我的应用程序进行一些计算。我想每隔一秒接收一次当前的浮动位置,这样我就可以计算“一个点”和我当前位置之间的距离......我该如何解决这个问题? (我不想将我的移动应用程序用作导航器)。 下面是我到目前为止所做的代码。你有什么想法吗?

public class MainActivity extends AppCompatActivity implements
        OnMapReadyCallback, PermissionsListener, OnLocationClickListener, OnCameraTrackingChangedListener {

    private PermissionsManager permissionsManager;
    private MapboxMap mapboxMap;
    private MapView mapView;
    private LocationComponent locationComponent;
    private boolean isInTrackingMode;


    @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);

        Log.d("LocOptionsActivity", "isInTrackingMode = " + isInTrackingMode);


        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)) {
            //
            LocationComponentOptions options = LocationComponentOptions.builder(this)
                    .elevation(5)
                    .accuracyAlpha(.6f)
                    .accuracyColor(Color.RED)
                    .foregroundDrawable(R.drawable.gps)
                    .build();

            // 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_NORTH);


            // Set the component's render mode
            locationComponent.setRenderMode(RenderMode.GPS);

            // Add the location icon click listener
            locationComponent.addOnLocationClickListener(this);

            // Add the camera tracking listener. Fires if the map camera is manually moved.
            locationComponent.addOnCameraTrackingChangedListener(this);

           /*findViewById(R.id.back_to_camera_tracking_mode).setOnClickListener(view -> {
                if (!isInTrackingMode) {
                    isInTrackingMode = true;
                    locationComponent.setCameraMode(CameraMode.TRACKING);
                    locationComponent.zoomWhileTracking(16f);
                    Toast.makeText(this, getString(R.string.tracking_enabled),
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, getString(R.string.tracking_already_enabled),
                            Toast.LENGTH_SHORT).show();
                }
            });*/

            //Set the component's zoom level
            locationComponent.zoomWhileTracking(22);

        } else {
            permissionsManager = new PermissionsManager(this);
            permissionsManager.requestLocationPermissions(this);
        }
    }

    @SuppressWarnings( {"MissingPermission"})
    @Override
    public void onLocationComponentClick() {
        if (locationComponent.getLastKnownLocation() != null) {
            Toast.makeText(this, String.format(getString(R.string.current_location),
                     locationComponent.getLastKnownLocation().getLatitude(),
                   locationComponent.getLastKnownLocation().getLongitude()), Toast.LENGTH_LONG).show();
        }

    }
    @Override
    public void onCameraTrackingDismissed() {
        isInTrackingMode = false;

    }

    @Override
    public void onCameraTrackingChanged(int currentMode) {
        // Empty on purpose
    }

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

}

【问题讨论】:

    标签: android mapbox android-location android-gps location-provider


    【解决方案1】:

    您必须将位置引擎添加到您的位置提供程序

    private void enableLocationComponent() {
            locationEngine = new LocationEngineProvider(this).obtainLocationEngineBy(LocationEngine.Type.GOOGLE_PLAY_SERVICES);
            locationEngine.setInterval(1000);//this is means get location every 1 second
            locationEngine.setFastestInterval(1000);
            locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
            locationEngine.addLocationEngineListener(new LocationEngineListener() {
                @SuppressLint("MissingPermission")
                @Override
                public void onConnected() {
                    locationEngine.requestLocationUpdates();
                }
    
                @Override
                public void onLocationChanged(Location location) {
                    mCurrentLocation = location;
                }
            });
            locationEngine.activate();
            // Check if permissions are enabled and if not request
            if (PermissionsManager.areLocationPermissionsGranted(this)) {
    
                LocationComponentOptions options = LocationComponentOptions.builder(this)
                    .elevation(5)
                    .accuracyAlpha(.6f)
                    .accuracyColor(Color.RED)
                    .foregroundDrawable(R.drawable.gps)
                    .build();
                // Get an instance of the component
                LocationComponent locationComponent = mapboxMap.getLocationComponent();
    
                //ADD THIS LINE 
                locationComponent.setLocationEngine(locationEngine);
                // Activate
                locationComponent.activateLocationComponent(this);
    
                // Enable to make component visible
                locationComponent.setLocationComponentEnabled(true);
    
                // Set the component's camera mode
                locationComponent.setCameraMode(CameraMode.TRACKING_GPS_NORTH);
    
    
                // Set the component's render mode
                locationComponent.setRenderMode(RenderMode.GPS);
    
                // Add the location icon click listener
                locationComponent.addOnLocationClickListener(this);
    
                // Add the camera tracking listener. Fires if the map camera is manually moved.
                locationComponent.addOnCameraTrackingChangedListener(this);
    
    
                //Set the component's zoom level
                locationComponent.zoomWhileTracking(22);
    
            } else {
                permissionsManager = new PermissionsManager(this);
                permissionsManager.requestLocationPermissions(this);
            }
        }
    

    别忘了在onCreate()上方添加private LocationEngine locationEngine;

    还有这个依赖

    implementation 'com.google.android.gms:play-services-location:16.0.0'
    

    【讨论】:

    • 当我添加此代码时,它会在应用程序中返回一个空白页面......它根本没有显示任何地图......为什么? ://
    • @oreocookiemaster 请检查您的日志猫并查看异常。也添加上面的依赖项
    • 错误:程序类型已经存在:com.mapbox.android.core.location.GoogleLocationEngine$1$2 并用红色下划线这行代码: implementation 'com.android.support:appcompat-v7: 28.0.0'
    • 如果添加我的依赖项,则不需要 com.mapbox.android.core.location.GoogleLocationEngine 。所以只需删除它
    • 删除'实施'com.google.android.gms:play-services-location:16.0.0' ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2023-01-13
    • 1970-01-01
    • 2022-11-01
    • 2019-03-25
    • 2016-09-29
    相关资源
    最近更新 更多