0
votes

I'm new in android and java and I'm programming my first app in Android Studio with a database and a Mapbox Map. I have few Activities and a database in .SQLite using Room persistence library. Right now I'm programming a Mapbox Activity.

I'm programming an activity which shows the map. This map visualizes several markers. Now I want to implement small annotations with information for each marker (e.g. address, house number, coordinates) after clicking on. At the moment the markers themselves are created by going through a List with a for loop.

How can I create an annotation window without a geoJSON file, but with accessing the SQLite database. Is it possible? I found only examples like this: https://docs.mapbox.com/android/maps/examples/symbol-layer-info-window/?size=n_10_n which uses a geoJSON file, which is transformed to a List, but I have only a List.

How is the best way to implement the example with SQLite database as source? Thank you in advance!

SQLite database


@Override
    public void onMapReady(@NonNull final MapboxMap mapboxMap) {
        MapboxActivity.this.mapboxMap = mapboxMap;

        mapboxMap.setStyle(
                new Style.Builder().fromUri("mapbox://styles/aroid435/ckiohlr9a0c3m17nq6tx5ajjs")
                
                ,
                new Style.OnStyleLoaded() {
                    @Override
                    public void onStyleLoaded(@NonNull Style style) {
                        enableLocationComponent(style);
                        SymbolManager symbolManager = new SymbolManager(mapView, mapboxMap, style);
                        mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder().target(new LatLng(51.051877, 13.741517)).zoom(10.5).build()));


                        for (int i= 0; i < stolpersteine_list.size() ; i = i+1 ) {
                        mapboxMap.getStyle().addImage("my-star-marker", BitmapFactory.decodeResource(getResources(), R.drawable.rectangle));
                        symbolManager.create(new SymbolOptions()
                                .withLatLng(new LatLng( stolpersteine_list.get(i).getLatitude(),stolpersteine_list.get(i).getLongitude()))
                                .withIconImage("my-star-marker")
                                .withIconAnchor("bottom"));
                        
                        }

                    }
                });
    } // end of onMapReady
ยดยดยด
1

1 Answers

0
votes

I find the way, how to convert my database into mapbox List. Here is my code:

    @Override
    public void onMapReady(@NonNull final MapboxMap mapboxMap) {
        MapboxActivity.this.mapboxMap = mapboxMap;

        List<Feature> symbolLayerIconFeatureList = new ArrayList<>();
        Log.d("tg", String.valueOf(symbolLayerIconFeatureList));
        for (int i = 0; i < stolpersteine_list.size(); i = i + 1) {
            symbolLayerIconFeatureList.add(Feature
                    .fromGeometry(Point.fromLngLat(stolpersteine_list.get(i).getLongitude(), stolpersteine_list.get(i).getLatitude())));
            Log.d("tag", String.valueOf(symbolLayerIconFeatureList));
        }


        mapboxMap.setStyle(
                new Style.Builder().fromUri("mapbox://styles/aroid435/ckiohlr9a0c3m17nq6tx5ajjs")
                        .withSource(new GeoJsonSource(SOURCE_ID,
                                FeatureCollection.fromFeatures(symbolLayerIconFeatureList)))
                        .withImage(ICON_ID, BitmapFactory.decodeResource(
                                MapboxActivity.this.getResources(), R.drawable.rectangle))

                        .withLayer(new SymbolLayer(LAYER_ID, SOURCE_ID)
                                .withProperties(
                                        iconImage(ICON_ID),
                                        iconAllowOverlap(true),
                                        iconIgnorePlacement(true)
                                )
                        ), new Style.OnStyleLoaded() {
                    @Override
                    public void onStyleLoaded(@NonNull Style style) {

                            enableLocationComponent(style);
                            SymbolManager symbolManager = new SymbolManager(mapView, mapboxMap, style);
                            mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder().target(new LatLng(51.051877, 13.741517)).zoom(10.5).build()));



                    }
                });
    } // end of onMapReady

but now I don't know how to add a properties to a point. This way I can only vizualize the markers, but I cannot add other values to the List and plot the annotation window. I cannot use the addStringProperty(String key, String value) or addProperty(String key, com.google.gson.JsonElement value), in my for loop, because I get error that: Non-static method 'addStringProperty(java.lang.String, java.lang.String)' cannot be referenced from a static context.

https://docs.mapbox.com/archive/android/java/api/libjava-geojson/2.1.0/com/mapbox/services/commons/geojson/Feature.html

Do somebody know how to do it?