mapbox / mapbox/mapbox-navigation-android
Point list will not accept location coordinates to create linestring to draw a trailing line
Nobody has claimed this yet.
- Dominant language
- Kotlin
- Stars
- 651
- Forks
- 321
- PR merge metrics
- No merged PRs in 30d
Description
**Android API:**
**Mapbox Navigation SDK version:**
### Steps to trigger behavior
1. Get user coordinates using Location.getLatitude/getLongitude
2. Add these to Point list
3. Create Linestring from pointlist
### Expected behavior
To intake user coordinates anytime they move and draw a trailing line behind the puck
### Actual behavior
The app crashes when the coordinates are trying to be added into the Point list, if this line of code is uncommented the app runs but no line is drawn
I believe it is partly due to looking for location history and not actively calling for location updates but I dont see anywhere how to add location updates into the point list to create the linestring
See below my main activity
private MapView mapView;
private static final int PERMISSION_LOCATION = 1000;
public MapboxMap mapboxMap;
ArrayList pointList = new ArrayList();
private PermissionsManager permissionsManager;
private static final String TAG = "SimplifyLineActivity";
private List routeCoordinates;
private LatLng locationHistory;
public LocationManager locationManager;
@SuppressWarnings( {"MissingPermission"})
private void enableLocationComponent(@NonNull Style loadedMapStyle) {
// Check if permissions are enabled and if not request
if (PermissionsManager.areLocationPermissionsGranted(this)) {
// Get an instance of the LocationComponent.
LocationComponent locationComponent = mapboxMap.getLocationComponent();
// Activate the LocationComponent
locationComponent.activateLocationComponent(
LocationComponentActivationOptions.builder(this, loadedMapStyle).build());
// Enable the LocationComponent so that it's actually visible on the map
locationComponent.setLocationComponentEnabled(true);
// Set the LocationComponent's camera mode
locationComponent.setCameraMode(CameraMode.TRACKING);
// Set the LocationComponent's render mode
locationComponent.setRenderMode(RenderMode.NORMAL);
} else {
permissionsManager = new PermissionsManager(this);
permissionsManager.requestLocationPermissions(this);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
permissionsManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
public void onExplanationNeeded(List permissionsToExplain) {
Toast.makeText(this, R.string.user_location_permission_explanation, Toast.LENGTH_LONG).show();
}
@Override
public void onPermissionResult(boolean granted) {
if (granted) {
mapboxMap.getStyle(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
enableLocationComponent(style);
}
});
} else {
Toast.makeText(this, R.string.user_location_permission_not_granted, Toast.LENGTH_LONG).show();
finish();
}
}
@Override
public void onLocationChanged(Location location) {
location.getLongitude();
location.getLatitude();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, "pk.eyJ1IjoiYWFyb25jb252ZXJ5IiwiYSI6ImNsN2l6ZHptMTB0YnYzcHBid2hrY2Q5cXUifQ.vMh6oO3R5sWWPPxklfrHRA");
setContentView(R.layout.activity_main);
// Create supportMapFragment
SupportMapFragment mapFragment;
if (savedInstanceState == null) {
// Create fragment
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Build a Mapbox map
MapboxMapOptions options = MapboxMapOptions.createFromAttributes(this, null);
options.camera(new CameraPosition.Builder()
.target(new LatLng(38.899895, -77.03401))
.zoom(18)
.tilt(45)
.build());
// Create map fragment
mapFragment = SupportMapFragment.newInstance(options);
// Add map fragment to parent container
transaction.add(R.id.location_frag_container, mapFragment, "com.mapbox.map");
transaction.commit();
} else {
mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentByTag("com.mapbox.map");
}
if (mapFragment != null) {
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull MapboxMap mapboxMap) {
LocationComponentActivity.this.mapboxMap = mapboxMap;
mapboxMap.setStyle(Style.OUTDOORS, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
enableLocationComponent(style);
List routeCoordinates = new ArrayList<>();
routeCoordinates.add(Point.fromLngLat(locationHistory.getLongitude(), locationHistory.getLatitude()));
LineString lineString = LineString.fromLngLats(routeCoordinates);
// Create the LineString from the list of coordinates and then make a GeoJSON
// FeatureCollection so we can add the line to our map as a layer.
style.addSource(new GeoJsonSource("line-source",
FeatureCollection.fromFeatures(new Feature[]{Feature.fromGeometry(lineString)
})));
// The layer properties for our line. This is where we make the line dotted, set the
// color, etc.
style.addLayer(new LineLayer("linelayer", "line-source").withProperties(
PropertyFactory.lineDasharray(new Float[]{0.01f, 2f}),
PropertyFactory.lineCap(Property.LINE_CAP_ROUND),
PropertyFactory.lineJoin(Property.LINE_JOIN_ROUND),
PropertyFactory.lineWidth(5f),
PropertyFactory.lineColor(Color.parseColor("#e55e5e"))
));
}
});
}
});
}
}
@Override
public void onMapReady(@NonNull MapboxMap mapboxMap) {
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the onLocationChanged and onMapReady entry points in the activity code shown, including how locationHistory and routeCoordinates are initialized. Reproduce the crash and determine the required location-update flow; done means location changes can be added to the point list and the trailing LineString renders without crashing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, java
- Domain
- mobile
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100