0

Do Mapbox Supports, GeoJson source support in Runtime Styling.

I tried same Style (file with two Source 1. Vector, 2. GeoJson) file with mapbox-gl-native and mapbox-gl-js.

It was working as expected in Native SDK but it seems mapbox-gl-js is ignoring if source type is GeoJson.

I tried version 0.52

1 Answer 1

2

This is definitely possible. You just need to call .addSource on your map object before you use that source to generate the style layers. This example shows how the general flow for adding a GeoJson source: https://docs.mapbox.com/mapbox-gl-js/example/multiple-geometries/

If you're trying to reference a geojson file, you'll just need to specify that file's URL via the data field of your source object. If you're going that route, the GeoJson file needs to be on the same domain or accessible using CORS.

Here's a quick and dirty code snippet to illustrate what I mean:

map.on("load", function() {
    map.addSource("my-geojson-source", {
        "type": "geojson",
        "data": "path/to/data.geojson"
    });

    map.addLayer({
        "id": "styled-geojson-layer",
        "type": "circle", // this depends on your data & goals
        "source": "my-geojson-source",
        ... // add style things here
    });
});

If you get stuck, the library documentation should have everything you need:

Sign up to request clarification or add additional context in comments.

3 Comments

Yes this is happening via data driven styling, but i am trying to achieve using runtime styling . Adding two sources in style file.
I'm not entirely sure what you mean by that. Are you able to share an MVCE that shows the problem?
I am preparing it. I will definitely share once ready. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.