Custom color ramp (color ramp)
This example shows how to create a custom color ramp and use it to visualize a point layer. Download the sample data here.
NPM module setup
npm install --save @maptiler/sdk
import { Map, MapStyle, config } from '@maptiler/sdk';
import '@maptiler/sdk/dist/maptiler-sdk.css';
config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
const map = new Map({
container: 'map',
zoom: 11.6,
center: [-119.807, 36.7828],
style: MapStyle.DATAVIZ.DARK
});
const myCustomRamp = new maptilersdk.ColorRamp({
stops: [
{ value: 100, color: [252, 222, 156] },
{ value: 500, color: [250, 164, 118] },
{ value: 1000, color: [240, 116, 110] },
{ value: 1500, color: [227, 79, 111] },
{ value: 2000, color: [220, 57, 119] },
{ value: 3000, color: [185, 37, 122] },
{ value: 4000, color: [124, 29, 111] },
]
});
map.on('load', async function () {
await maptilersdk.helpers.addPoint(map, {
data: 'schools.geojson',
property: "students",
pointColor: myCustomRamp,
pointOpacity: 0.7,
minPointRadius: 1,
maxPointRadius: 100,
showLabel: true,
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="robots" content="noindex">
<title>Custom color ramp (color ramp) | NPM example</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div id="map"></div>
<script type="module" src="./main.js"></script>
</body>
</html>
body {margin: 0; padding: 0;}
#map {position: absolute; top: 0; bottom: 0; width: 100%;}
Related examples
Color ramp resampling (color ramp)
ExamplesVisualize point layers by resampling existing map color ramps.