DEV Community

Cover image for A 5-Mile Radius Isn't 5 Miles by Car: Draw Both on a Map with JavaScript
Casey Rivers for Geoapify Maps API

Posted on Edited on

A 5-Mile Radius Isn't 5 Miles by Car: Draw Both on a Map with JavaScript

"Show everything within 5 miles of the store." It sounds like one line of code: draw a circle. But your customers don't drive in straight lines. They drive on roads, cross rivers on bridges and go around parks.

In this tutorial we draw both answers on the same map: the radius circle and the drive-distance area, the part you can really reach within 5 miles by car. Then we ask an AI coding assistant to build the same page and look at what the prompt changes.

TL;DR

  • A radius circle measures the straight line. A drive-distance area (an isodistance) measures the distance along roads.
  • In downtown Memphis, only 35% of the 5-mile circle is within 5 miles by car. In San Francisco it's 22%, in Denver 53%.
  • The circle needs no API: it's a GeoJSON polygon you calculate in the browser. The drive-distance area is one request to the Geoapify Isoline API with type=distance.
  • In MapLibre, don't use a circle layer for a radius: its radius is in pixels, not meters.
  • AI assistants can write this page for you. A precise prompt gets you exactly the page you asked for; a vague one gets a page.

👉 Live demo: click the map to move the center.


Table of Contents

  1. The problem: a circle is the easy answer
  2. Same 5 miles, very different reach
  3. Step 1: Draw the radius circle
  4. Step 2: Get the drive-distance area
  5. Step 3: Draw both and compare
  6. Let an AI assistant write it
  7. Summary
  8. FAQ

The problem: a circle is the easy answer

Take downtown Memphis, Tennessee, and a 5-mile (8,047 m) radius. The circle covers 203 km², including a big piece of Arkansas on the other side of the Mississippi River.

A dashed orange 5-mile radius circle around downtown Memphis, Tennessee, covering the Mississippi River and part of Arkansas

Now pick a point inside that circle, on the Arkansas side: it's 5.96 km from downtown in a straight line, well within 5 miles. By car, it's 9.6 km, because the only way there is over the I-55 bridge.

Straight dashed line of 5.96 km from downtown Memphis to a point across the river, and the purple driving route of 9.6 km over the I-55 bridge

That point is inside the circle, but not within 5 miles by road. If your 5-mile radius decides a delivery fee, a service area or a "stores near you" list, the circle gives the wrong answer for it.

What you want instead is the drive-distance area: every place you can reach within 5 miles of driving.

Downtown Memphis with the dashed 5-mile radius circle and, inside it, the purple drive-distance area for 8,047 meters: 71.1 km², 35% of the circle

The purple area covers 71.1 km², 35% of the circle. It crosses the river only as thin strips along the two bridges.

Same 5 miles, very different reach

How much of the circle you can drive to depends on the city. We ran the same 5 miles from four downtowns:

Four maps comparing a 5-mile radius circle with the 5-mile drive-distance area: Memphis 35%, Chicago 32%, Denver 53%, San Francisco 22%

City What's in the way Drive-distance area Share of the circle
Denver almost nothing: a street grid 107.5 km² 53%
Memphis the Mississippi, 2 road bridges 71.1 km² 35%
Chicago Lake Michigan 64.5 km² 32%
San Francisco water on three sides 45.3 km² 22%

Even in Denver, the best case, half of the circle is more than 5 miles away by car. Streets don't run in straight lines from your center point.

Let's build the map.

Step 1: Draw the radius circle

The circle needs no API request. It's every point at the same straight-line distance from the center, so we calculate it in the browser as a GeoJSON polygon.

Why not a MapLibre circle layer? Because its circle-radius is in pixels: the circle would stay the same size on the screen while you zoom. We need meters.

// Build a circle polygon: `steps` points around the center, `radiusMeters` away.
function circlePolygon(lon, lat, radiusMeters, steps = 64) {
  const R = 6371008.8; // mean Earth radius, meters
  const rad = (deg) => (deg * Math.PI) / 180;
  const deg = (r) => (r * 180) / Math.PI;
  const d = radiusMeters / R; // the distance as an angle
  const lat1 = rad(lat);
  const lon1 = rad(lon);
  const ring = [];

  for (let i = 0; i <= steps; i++) {
    const bearing = (2 * Math.PI * i) / steps;
    // Great-circle "destination point" formula: keeps the circle round at any latitude.
    const lat2 = Math.asin(Math.sin(lat1) * Math.cos(d) + Math.cos(lat1) * Math.sin(d) * Math.cos(bearing));
    const lon2 = lon1 + Math.atan2(
      Math.sin(bearing) * Math.sin(d) * Math.cos(lat1),
      Math.cos(d) - Math.sin(lat1) * Math.sin(lat2)
    );
    ring.push([deg(lon2), deg(lat2)]); // GeoJSON order: [lon, lat]
  }
  return { type: "Feature", properties: {}, geometry: { type: "Polygon", coordinates: [ring] } };
}

const center = { lon: -90.049, lat: 35.1495 }; // downtown Memphis
const distance = 8047; // 5 miles in meters

const radiusCircle = circlePolygon(center.lon, center.lat, distance);
Enter fullscreen mode Exit fullscreen mode

Using Leaflet instead? L.circle([lat, lon], { radius: 8047 }) already takes meters, so you can skip this function.

Step 2: Get the drive-distance area

The drive-distance area comes from the Geoapify Isoline API. With type=distance it returns the area reachable within a distance along the road network:

https://api.geoapify.com/v1/isoline?lat=35.1495&lon=-90.049&type=distance&mode=drive&range=8047&apiKey=YOUR_API_KEY
Enter fullscreen mode Exit fullscreen mode
Parameter Value Meaning
lat, lon 35.1495, -90.049 the center, the same as the circle's
type distance an isodistance (use time for a travel-time area)
mode drive measure on roads open to cars; also walk, bicycle and more
range 8047 the distance in meters

In JavaScript:

async function getDriveDistanceArea(lon, lat, distance) {
  const params = new URLSearchParams({
    lat,
    lon,
    type: "distance",
    mode: "drive",
    range: distance, // meters
    apiKey: "YOUR_API_KEY",
  });
  const response = await fetch(`https://api.geoapify.com/v1/isoline?${params}`);
  return response.json(); // GeoJSON FeatureCollection, ready for a map source
}

const driveDistanceArea = await getDriveDistanceArea(center.lon, center.lat, distance);
Enter fullscreen mode Exit fullscreen mode

The answer is a GeoJSON FeatureCollection with one feature, usually a MultiPolygon: the area can have separate parts, like the strips along the bridges. A 5-mile (8 km) isodistance costs 2 credits, and the free plan calculates isodistances up to 10 km.

Step 3: Draw both and compare

Add the circle first and the drive-distance area on top, so the space between the two outlines is exactly "close on the map, far by road":

const map = new maplibregl.Map({
  container: "map",
  style: "https://maps.geoapify.com/v1/styles/osm-bright/style.json?apiKey=YOUR_API_KEY",
  center: [center.lon, center.lat],
  zoom: 11,
});

map.on("load", async () => {
  // 1. The circle: light fill + dashed outline, at the bottom.
  map.addSource("radius-circle", { type: "geojson", data: radiusCircle });
  map.addLayer({
    id: "radius-circle-fill",
    type: "fill",
    source: "radius-circle",
    paint: { "fill-color": "#f76707", "fill-opacity": 0.1 },
  });
  map.addLayer({
    id: "radius-circle-outline",
    type: "line",
    source: "radius-circle",
    paint: { "line-color": "#f76707", "line-width": 2, "line-dasharray": [3, 2] },
  });

  // 2. The drive-distance area on top.
  const driveDistanceArea = await getDriveDistanceArea(center.lon, center.lat, distance);
  map.addSource("drive-distance", { type: "geojson", data: driveDistanceArea });
  map.addLayer({
    id: "drive-distance-fill",
    type: "fill",
    source: "drive-distance",
    paint: { "fill-color": "#623aff", "fill-opacity": 0.3 },
  });
});
Enter fullscreen mode Exit fullscreen mode

The drive-distance area always stays inside the circle: a road route is never shorter than the straight line.

To put a number on the difference, the demo calculates both areas from the polygons. On the sphere, a ring's area is a short loop over its points:

// Area of one ring of [lon, lat] points, in square meters.
function ringArea(ring) {
  const R = 6371008.8;
  const rad = (deg) => (deg * Math.PI) / 180;
  let sum = 0;
  for (let i = 0; i < ring.length - 1; i++) {
    const [lon1, lat1] = ring[i];
    const [lon2, lat2] = ring[i + 1];
    sum += rad(lon2 - lon1) * (2 + Math.sin(rad(lat1)) + Math.sin(rad(lat2)));
  }
  return Math.abs((sum * R * R) / 2);
}
Enter fullscreen mode Exit fullscreen mode

For a MultiPolygon, add up the outer rings and subtract the holes. The full version, with the click handler and a distance menu, is in the demo's source on GitHub.

Let an AI assistant write it

Do you need to type all this yourself? Not really. We gave the same task to an AI coding assistant (Claude) three times, with different prompts, and ran each page it wrote with a real API key.

Prompt 1, vague:

Make a web page with a map that shows a 5 mile radius around downtown Memphis and the area you can actually drive to within 5 miles, using Geoapify.

Page written by an AI assistant from the vague prompt: Leaflet map of Memphis with a blue 5-mile circle and the green drive-distance area

It works. The assistant knew the Isoline API (type=distance, mode=drive, range=8047) and chose Leaflet, whose L.circle takes meters, so the circle is right. But you get only what you asked for: a picture. No numbers, no way to move the center.

Prompt 1b, vague, but "with a MapLibre map":

Page written by an AI assistant from the vague prompt with MapLibre: a blue 5-mile circle polygon and the red drive-distance area around downtown Memphis

We expected the classic mistake here, a circle layer with a radius in pixels. The assistant avoided it and built a 128-point polygon in meters. It did pick MapLibre GL JS 4.7.1, an older major version, because nothing in the prompt said which one. Still no numbers and no click.

Prompt 2, precise:

Build a single HTML page (MapLibre GL JS 5.24.1 from unpkg, no build step) that compares a radius circle with a drive-distance area for the same center and distance.

  • Center: downtown Memphis, lat 35.1495, lon -90.049. Distance: 5 miles = 8047 meters.
  • Base map: Geoapify style https://maps.geoapify.com/v1/styles/osm-bright/style.json?apiKey=YOUR_API_KEY
  • Radius circle: build it as a GeoJSON Polygon in meters (64 points, great-circle destination formula). Don't use a MapLibre circle layer: its radius is in pixels.
  • Drive-distance area: GET https://api.geoapify.com/v1/isoline?lat=…&lon=…&type=distance&mode=drive&range=8047&apiKey=YOUR_API_KEY. range is in meters. The response is a GeoJSON FeatureCollection (usually a MultiPolygon). Coordinates in GeoJSON are [lon, lat].
  • Draw the circle below the drive area: circle dashed orange outline with a light fill, drive area purple fill.
  • Show both areas in km² and the drive area as a percentage of the circle.
  • Clicking the map moves the center and redraws both; ignore a response that arrives after a newer click.
  • Plain JavaScript, short functions, comments only where the code isn't obvious.

Page written by an AI assistant from the precise prompt: MapLibre map of Memphis with the dashed orange circle, the purple drive area and a panel showing 203.1 km², 71.1 km² and 35.0%

This one matches our hand-written demo to the decimal: 203.1 km², 71.1 km², 35%. It also handles fast clicks correctly: a slow answer for an old center doesn't overwrite the new one.

What made the difference wasn't the API: all three prompts produced a working Isoline request. It was everything around it:

  • Units. Say "meters" for the radius and for range, and say how 5 miles converts (8,047 m).
  • The library and its traps. Name the library and its version. For MapLibre, one line ("not a circle layer: its radius is in pixels") rules out the classic mistake, even if the assistant may avoid it on its own.
  • Coordinate order. GeoJSON is [lon, lat]; the API's query parameters are lat and lon. Saying it costs one line.
  • What "done" means. Numbers in a panel, click to move, which layer on top. An assistant can't guess the comparison you wanted.

Building an AI agent instead of a page? The Geoapify MCP server gives agents a tool that calculates isolines directly, so an agent can answer "what can I reach within 5 miles by car from here?" without writing the request itself.

Summary

  • A radius circle answers "within X in a straight line". Calculate it in the browser as a GeoJSON polygon in meters; in MapLibre, not as a circle layer.
  • A drive-distance area answers "within X by road". It's one Isoline API request with type=distance, mode=drive and range in meters.
  • The gap is big: 5 miles by car reaches 22% (San Francisco) to 53% (Denver) of the 5-mile circle.
  • Use the circle for straight-line questions: a signal range, a legal buffer. Use the drive-distance area when the distance on roads matters: delivery fees, service areas, "near you" lists. For travel time, request type=time instead.
  • AI assistants already know how to call the API. Your prompt decides the units, the library pitfalls and what the result shows.

FAQ

What is a drive-distance area?
The area you can reach within a given distance driving on roads from a starting point. It's also called an isodistance. The Geoapify Isoline API returns it as GeoJSON with type=distance and mode=drive.

How do I draw a radius circle in meters with MapLibre GL?
Calculate a GeoJSON polygon around the center (the circlePolygon function above, or turf.circle from Turf.js) and add it as a fill and line layer. The circle layer type takes a radius in pixels.

How do I draw a radius circle with Leaflet?
L.circle([lat, lon], { radius: meters }).addTo(map). Leaflet's circle radius is already in meters.

Why is the drive-distance area always smaller than the circle?
A route on roads is never shorter than the straight line between two points. Rivers, lakes, rail yards and the street pattern make it longer.

Can I use walking or cycling instead of driving?
Yes: set mode=walk or mode=bicycle. The same range in meters is then measured on the network for that mode.

What about "within 10 minutes" instead of "within 5 miles"?
Use type=time with range in seconds, for example range=600. That's an isochrone, a travel-time area.

How much does one request cost?
A distance isoline costs 1 credit per 5 km, so 5 miles (8,047 m) costs 2 credits. The free plan calculates isodistances up to 10 km; larger ranges depend on the plan.


Try it

Get a free API key at myprojects.geoapify.com and try it with your own address. What share of your 5-mile circle can you really reach by car? Tell us in the comments.

Top comments (0)