VIK6Galado
VIK6Galado

Reputation: 680

how to fill color for a region based on value in mapbox?

i am using mapbox that draws zip-code outline based on the external geojson file as shown below:

import { Component, OnInit } from '@angular/core';
import * as mapboxgl from 'mapbox-gl';

@Component({
  selector: 'app-mapbox',
  templateUrl: './mapbox.component.html',
  styleUrls: ['./mapbox.component.css']
})
export class MapboxComponent implements OnInit {

  ngOnInit() {
    mapboxgl.accessToken = 'api-key';
    let map = new mapboxgl.Map({
      container: 'map',
      style: 'styles',
      zoom: 5,
      center: [-80.118, 25.897]
    });

    map.addControl(new mapboxgl.NavigationControl());

    map.on('load', function () {
      map.addSource("route", {
        "type": "geojson",
        "data": "./assets/shapeGeoJson.json"
      });

      map.addLayer({
        "id": "route",
        "type": "line",
        "source": "route",
        "paint": {
          'line-color': "gray"
        }
      });
    });
  }
}

here, am able to load the geojson in which i will get the outline of the zipcodes.

Now, i want to call a api where in will get the density count for zipcodes and i need to paint the zipcode layer based on the density value.

Can any one tell me how to achieve this?

any help?

Upvotes: 0

Views: 5713

Answers (1)

Raghav
Raghav

Reputation: 1229

You can make use of addSource of Mapbox. Just add these line in your code

map.addSource('zipDensity', {
          type: 'geojson',
          data: {
            ...geoJsonObj // this can be your data from API containing density prop
          } 
});

map.addLayer({
          'id': 'zipDensity',
          'source': 'zipDensity',
          'type': 'fill',
          'paint': {
            'fill-color': {
              property: 'density', // this will be your density property form you geojson
              stops: [
                [1000, '#CECECE'],
                [100000, '#DEDEDE'],
              ]
            },
            'fill-opacity': 1
          }
});

In addLayer you can add stops where you can add density value and color code. You can explore more props of addLayer

Hope It helps for you..

Upvotes: 6

Related Questions