Я пытаюсь получить координаты широты и долготы в качестве точного числа с плавающей точкой, чтобы выполнить некоторые вычисления для моего приложения. Я хочу получать текущее местоположение с плавающей точкой каждую секунду, чтобы я мог рассчитать расстояние между «точкой» и моим текущим местоположением ... как я могу решить эту проблему?
(Я не хочу использовать мое мобильное приложение в качестве навигатора).
Ниже приведен код, который я сделал до сих пор. У вас есть идеи?
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();
}
}