How to actively populate a Point list with the users current location coordinates to allow generation of a trailing line using GeoJson

Viewed 14

I have the user's current location displayed on the map as a puck but when I try drawing a trailing line behind the user's movement it does not seem to be pulling in the long lat into my Point list which I then use to assign to the LineString to generate the line

Currently I have nothing writing to my routeCoordinate Point list, can anyone suggest what would work? I previously tried location component

See below my main activity

public class LocationComponentActivity extends AppCompatActivity implements OnMapReadyCallback, PermissionsListener {

    MapView mapView;
    private static final int PERMISSION_LOCATION = 1000;
    public MapboxMap mapboxMap;
    ArrayList<Point> pointList = new ArrayList<Point>();
    private PermissionsManager permissionsManager;
    private static final String TAG = "SimplifyLineActivity";
    private List<Point> routeCoordinates;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Mapbox.getInstance(this, "pk.eyJ1IjoiYWFyb25jb252ZXJ5IiwiYSI6ImNsN2l6ZHptMTB0YnYzcHBid2hrY2Q5cXUifQ.vMh6oO3R5sWWPPxklfrHRA");

        // 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<Point> routeCoordinates = new ArrayList<>();
                            
                            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"))
                            ));
                        }
                    });
                }
            });
        }
    }

    public double hereLocationPointLat(Location location) {
        return location.getLatitude();

    }

    public double hereLocationPointLong(Location location) {
        return location.getLongitude();

    }

    @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 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
    public void onMapReady(@NonNull MapboxMap mapboxMap) {

    }
}
0 Answers
Related