MapSnippets logo MapSnippets logo

Fuzzy Location Circle with MapLibre GL JS

MapLibre GL JS

Blur an exact location in MapLibre GL JS by building a circle polygon from Math.cos() and Math.sin() offsets and showing it as a fill layer.

progress_activity Loading map...
const map = new maplibregl.Map({
  container: 'map',
  style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY',
  center: [14.42, 50.08],
  zoom: 13
});
map.addControl(new maplibregl.NavigationControl());
function makeCircle(center, radiusKm, steps = 64) {
  const coords = [];
  for (let i = 0; i <= steps; i++) {
    const angle = (i / steps) * Math.PI * 2;
    const dx = radiusKm / (111.32 * Math.cos(center[1] * Math.PI / 180));
    const dy = radiusKm / 110.574;
    coords.push([center[0] + Math.cos(angle) * dx, center[1] + Math.sin(angle) * dy]);
  }
  return { type: 'Feature', geometry: { type: 'Polygon', coordinates: [coords] } };
}
map.on('load', () => {
  map.addSource('fuzzy', { type: 'geojson', data: makeCircle([14.42, 50.08], 0.5) });
  map.addLayer({ id: 'fuzzy-fill', type: 'fill', source: 'fuzzy',
    paint: { 'fill-color': '#2196F3', 'fill-opacity': 0.15 } });
  map.addLayer({ id: 'fuzzy-border', type: 'line', source: 'fuzzy',
    paint: { 'line-color': '#2196F3', 'line-width': 2, 'line-dasharray': [4, 2] } });
});

How to show an approximate location in MapLibre GL JS

Instead of showing an exact point, this snippet draws a circle polygon that indicates an approximate area. The makeCircle helper generates a GeoJSON polygon with 64 vertices forming a circle of a specified radius in kilometers.

The radius conversion accounts for latitude. Longitude degrees are shorter at higher latitudes, so the formula divides by cos(lat) to keep the circle visually round. The fill is semi-transparent with a dashed border to communicate uncertainty.

This pattern is common for privacy-sensitive applications like user profiles, rental listings, or approximate business locations.

Key APIs

API Description Docs
fill layer Render the fuzzy circle area maplibre.org docs
line-dasharray Dashed border for uncertainty maplibre.org docs
GeoJSON Polygon Circle represented as polygon ring geojson.org

FAQ

Why not use a circle layer instead?

The circle layer type renders pixels, not geographic units. A polygon circle scales correctly with zoom and represents a real-world area.

How do I adjust the obfuscation radius?

Change the second argument of makeCircle(). Use 0.5 km for neighborhood-level and 2-5 km for city-level obfuscation.

Common mistakes

  • Using a fixed pixel-radius circle layer, which does not represent a real geographic area
  • Forgetting the cos(lat) latitude correction, which makes circles elliptical at high latitudes
  • Showing both the exact point and the fuzzy circle, which defeats the purpose of obfuscation

Build maps using MapLibre GL JS with AI

Generate code like this fuzzy location circle with maplibre gl js example using the MapLibre GL JS skill in your IDE.