I'm trying to get the lat, long coordinates as a precise float number, in order to make some calculations for my application. I want to receive the current float location every one second, so I can calculate the distance between 'a point' and my current location... how can I solve this problem? (I don't want to use my mobile app as a navigator). Below is the code that I have done till now.Have you got any ideas?
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();
}
}
you must add location Engine to your location provider
Don't forget add
private LocationEngine locationEngine;above ofonCreate()And this dependencie