mapbox/mapbox-gl-native

App crashes when trying to draw polyline by adding coordinates using onLocationChanged but works using hardcoded coordinates

AaronConvery opened this issue · 0 comments

Platform:
Android Studio

Mapbox SDK version:
10.7.0

Steps to trigger behavior

  1. Search for current location
  2. App crashes when trying to load the map

Expected behavior

Current location should be displayed and a polyline output on the user's location changing

Actual behavior

The app crashes when trying to load the map but if I remove the current location code and hardcode the coordinates it works and draws a line

`
public class MapActivity extends LocationComponentActivity implements PermissionsListener, LocationListener {

private MapView mapView;
private List<Point> routeCoordinates;

private PermissionsManager permissionsManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Mapbox.getInstance(this, "pk.eyJ1IjoiYWFyb25jb252ZXJ5IiwiYSI6ImNsN2l6ZHptMTB0YnYzcHBid2hrY2Q5cXUifQ.vMh6oO3R5sWWPPxklfrHRA");
    setContentView(R.layout.map_layout);
    mapView = findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(@NonNull final MapboxMap mapboxMap) {

            mapboxMap.setStyle(Style.OUTDOORS, new Style.OnStyleLoaded() {
                @Override
                public void onStyleLoaded(@NonNull Style style) {
                    enableLocationComponent(style);
                    // 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.fromLngLats(routeCoordinates)
                            )})));

                    // 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"))
                    ));
                }
            });
        }
    });
}

@SuppressWarnings({"MissingPermission"})
public 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);

        //Set the zoom level
        locationComponent.zoomWhileTracking(17);

        locationComponent.getLastKnownLocation();

        LocationComponentActivationOptions
                .builder(this, loadedMapStyle)
                .useDefaultLocationEngine(true)
                .locationEngineRequest(new LocationEngineRequest.Builder(750)
                        .setFastestInterval(750)
                        .setPriority(LocationEngineRequest.PRIORITY_HIGH_ACCURACY)
                        .build())
                .build();

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

public void onLocationChanged(@NonNull Location location) {

    routeCoordinates = new ArrayList<>();
    routeCoordinates.add(Point.fromLngLat(location.getLongitude(), location.getLatitude()));

}

}

`