Monday, 21 June 2021

android mapbox source geojson not working

I took this example: https://docs.mapbox.com/android/maps/examples/marker-symbol-layer/

It worked.

I then unsuccessfully tried to replace the points by a source geojson as explained here: https://docs.mapbox.com/android/maps/guides/data-driven-styling/#geojson

I did not change anything beside the section where I add new stuff to the map. This section looks now like this:

 @Override
    public void onMapReady(@NonNull final MapboxMap mapboxMap) {

        try {
            mapboxMap.setStyle(new Style.Builder().fromUri("mapbox://styles/mapbox/cjf4m44iw0uza2spb3q0a7s41")

                    // Add the SymbolLayer icon image to the map style
                    .withImage(ICON_ID, BitmapFactory.decodeResource(
                            MainActivity.this.getResources(), R.drawable.mapbox_marker_icon_default))

                    // Adding a GeoJson source for the SymbolLayer icons.
                    .withSource(new GeoJsonSource("ports",
                            new URI("https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_10m_ports.geojson")))

                    // Adding the actual SymbolLayer to the map style. An offset is added that the bottom of the red
                    // marker icon gets fixed to the coordinate, rather than the middle of the icon being fixed to
                    // the coordinate point. This is offset is not always needed and is dependent on the image
                    // that you use for the SymbolLayer icon.
                    .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) {

                    // Map is set up and the style has loaded. Now you can add additional data or make other map adjustments.


                }
            });
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

When I run the app, i get a map canvas with the mapbox map but there are no points. What am I missing?



from android mapbox source geojson not working

No comments:

Post a Comment