Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding and displaying data from a locally stored GeoJSON file using MapBox

I'm attempting to add markers to a map from a GeoJSON File that has been added to the "asset" folder.

I've attempted to follow the documentation however have been unable to get the expected result since the markers are no where to be found when running the app.

My Attempt:

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

    mapboxMap.setStyle(Style.MAPBOX_STREETS,
            new Style.OnStyleLoaded() {
                @Override
                public void onStyleLoaded(@NonNull Style style) {
                    enableLocationComponent(style);
                    GeoJsonSource source = null;
                    try {
                        source = new GeoJsonSource("geojson-source", new URI("asset://markerdata.geojson"));
                    } catch (URISyntaxException e) {
                        e.printStackTrace();
                    }

                    style.addSource(source);

                    Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.marker);
                    style.addImage("marker", icon);


                    SymbolLayer symbolLayer = new SymbolLayer("layer-id", "source-id"); // ?
                    symbolLayer.setProperties(PropertyFactory.iconImage("marker"));

                    style.addLayer(symbolLayer);

                }
            });

}

I have noticed that SymbolLayer expects a layer-id and source-id however fail to understand what these are.

like image 531
ThatOneNoob Avatar asked Jan 30 '20 17:01

ThatOneNoob


People also ask

How do I load a GeoJSON file into Mapbox?

You can upload GeoJSON files to Mapbox as tilesets using Mapbox Tiling Service or as datasets or tilesets using the Mapbox Uploads API. You can also upload GeoJSON files to Mapbox Studio, which uses the Uploads API, as either datasets or tilesets.

How do I show map in Mapbox?

Map to initialize a Mapbox map inside an HTML element on a webpage. You can use the map parameters style , center , and zoom to define the initial appearance of the map. The string value for accessToken should be a valid access token from a Mapbox user account.


Video Answer


1 Answers

The markers should appear if you put the GeoJsonSource id as the SymbolLayer source id:

SymbolLayer symbolLayer = new SymbolLayer("layer-id", "geojson-source");

The layer id is an identifier for the layer, the source id is the id of the data source to display, in your case it's "geojson-source".

like image 52
Loris Securo Avatar answered Sep 26 '22 16:09

Loris Securo