MapSnippets logoMapSnippets logo

Fuzzy Location Circle with MapLibre GL JS

MapLibre GL JS

Show an approximate location as a fuzzy circle polygon layer instead of an exact point for privacy protection.

progress_activityLoading 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 it works

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 parameter to makeCircle(). Use 0.5 km for neighborhood-level, 2-5 km for city-level obfuscation.

Common mistakes

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

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.