mapbox/mapbox-gl-native

Android: can't call java.lang.String com.mapbox.geojson.GeoJson.type() on null object when adding trailing line behind location history

AaronConvery opened this issue · 0 comments

Platform:
Android Studio
Mapbox SDK version:
9.1.0

Steps to trigger behavior

1.Create a new linestring using point list of location coordinates
2.Add linestring to a new feature
3.Create new geoJson source using the feature

Expected behavior

The app does not crash and displays a trailing line behind the users movement

Actual behavior

The app crashes when loaded up due to the geoJsonSource code with the error code as seen in the title

Please see main activity below

public class LocationComponentActivity extends AppCompatActivity implements OnMapReadyCallback, PermissionsListener, LocationListener, IBaseGpsListener {

public MapboxMap mapboxMap;
private PermissionsManager permissionsManager;
List<Feature> featureList = new ArrayList();
List routeCoordinates = new ArrayList<Point>();
LineString lineString;

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

        locationComponent.getLastKnownLocation();

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


}

@SuppressLint("MissingPermission")
private void showLocation() {
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // check if gps is enabled
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        // Start locating
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, this);
    } else {
        // enable GPS
        Toast.makeText(this, "Enable GPS!", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
    }

}

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

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

                       // FeatureCollection fc = FeatureCollection.fromFeatures(featureList);

                        Feature feature = Feature.fromGeometry(lineString);

                        GeoJsonSource geoJsonSource = new GeoJsonSource("geojson-source", feature);

                        // FeatureCollection so we can add the line to our map as a layer.
                        style.addSource(geoJsonSource);

                        // 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) {


}

@Override
public void onLocationChanged(@NonNull Location location) {

    /*Point pointA = Point.fromLngLat(location.getLatitude(), location.getLatitude());
    Point pointB = Point.fromLngLat(location.getLongitude(), location.getLongitude());

    Feature featureA = Feature.fromGeometry(pointA);
    Feature featureB = Feature.fromGeometry(pointB);

    featureList.add(featureA);
    featureList.add(featureB);*/

    // Add point to pointlist
    routeCoordinates.add(Point.fromLngLat(location.getLongitude(), location.getLatitude()));
    // Create LineString using the point list
    lineString = LineString.fromLngLats(routeCoordinates);

}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onGpsStatusChanged(int event) {

}

}