Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

defining a polygon fill color based on geojson with mapbox

I am using a mapbox example in order to create multiple polygons on a map, and I have pop-up event for each. My problem is that I need to set each polygon's fill color differently based on it's geojson properties.

This is my example. I am using the following javascript code:

mapboxgl.accessToken = 'pk.eyJ1IjoibWFoYW5tZWhydmFyeiIsImEiOiJ6SDdSWldRIn0.8zUNm01094D1aoSeHpWYqA';
var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/streets-v9',
    center: [51.40545845031738,
    35.75069181054449],
    zoom: 10

});

map.on('load', function (e) {
    // Add a layer showing the state polygons.
    map.addLayer({
        'id': 'states-layer',
        'type': 'fill',
        'source': {
            'type': 'geojson',
            'data': 'geojson.js'
        },
        'paint': {
            'fill-color': 'rgba(200, 100, 240, 0.4)',
            'fill-outline-color': 'rgba(200, 100, 240, 1)'
        }
    });

    // When a click event occurs on a feature in the states layer, open a popup at the
    // location of the click, with description HTML from its properties.
    map.on('click', 'states-layer', function (e) {
        new mapboxgl.Popup()
            .setLngLat(e.lngLat)
            //.setHTML(e.features[0].properties.name)
            .setHTML("<h1>"+e.features[0].properties.userone+"</h1>"+e.features[0].properties.name)

            .addTo(map);
    });

    // Change the cursor to a pointer when the mouse is over the states layer.
    map.on('mouseenter', 'states-layer', function () {
        map.getCanvas().style.cursor = 'pointer';
    });

    // Change it back to a pointer when it leaves.
    map.on('mouseleave', 'states-layer', function () {
        map.getCanvas().style.cursor = '';
    });
});

Here it loads the colors all the same

'paint': {
    'fill-color': 'rgba(200, 100, 240, 0.4)',
    'fill-outline-color': 'rgba(200, 100, 240, 1)'
}

On my geojson file I have a key for color:

"type": "Feature",
"properties": {
    "userone":"پیروزی",
    "name":"North Dafkota",
    "featureclass":"Admin-1 scale rank",
    "color":"red"
}

I want to use it to define the polygons fill color.

like image 643
Mahanmeh Avatar asked Jan 03 '23 10:01

Mahanmeh


1 Answers

If you just want to use a color that you define in geojson feature properties. Then you could use the layers identity property like this:

 map.addLayer({
    'id': 'states-layer',
    'type': 'fill',
    'source': {
        'type': 'geojson',
        'data': 'geojson.js'
    },
    'paint': {
        'fill-color': {
            type: 'identity',
            property: 'color',
        },
        'fill-outline-color': 'rgba(200, 100, 240, 1)'
    }
});

Also see: https://www.mapbox.com/mapbox-gl-js/style-spec/#function-type

And: https://www.mapbox.com/mapbox-gl-js/style-spec/#types-color

like image 52
Andi-lo Avatar answered Jan 10 '23 20:01

Andi-lo