Distance between cities

Calculate the straight-line distance between two city centers in miles and kilometers. Add a state after a comma when a city name is ambiguous, such as Portland, ME.

A state after the comma picks the right one: Portland, ME.

Make sure it is the right city

Portland in Maine and Portland in Oregon are different endpoints. The returned city and region identify the place used in the calculation. A distance can look plausible even when one name resolved to the wrong city.

City-center distance is not a road route

The calculation measures the shortest path along the Earth's surface between the cities' reference coordinates. A journey between particular addresses can be longer and have different endpoints. This page does not calculate directions or travel time.

Build a straight-line city distance calculator

Resolve both city names to coordinates with the City API, then calculate the great-circle distance. The result is measured between city centers.

  1. Read a city and optional state for each endpoint.
  2. Resolve both cities in parallel and check the returned places.
  3. Apply the Haversine formula and display miles and kilometers.

Installation steps are included in each example. Set PARSEAPI_KEY on your server. Swift and Kotlin use an app key restricted to your app ID.

// npm install @parseapi/sdk@1.14.0
import { parseAPI } from '@parseapi/sdk';

// Set PARSEAPI_KEY on your server.
const parse = parseAPI();

async function city(input) {
  const [name, state] = input.split(',').map(part => part.trim());
  if (!name) throw new Error('Enter a city.');
  const row = await parse.city(name, { state });
  if (![row.latitude, row.longitude].every(Number.isFinite)) {
    throw new Error('City coordinates are unavailable.');
  }
  return row;
}
function distanceKm(a, b) {
  const rad = degrees => degrees * Math.PI / 180;
  const latitude = rad(b.latitude - a.latitude);
  const longitude = rad(b.longitude - a.longitude);
  const h = Math.sin(latitude / 2) ** 2 + Math.cos(rad(a.latitude)) *
    Math.cos(rad(b.latitude)) * Math.sin(longitude / 2) ** 2;
  return 6371 * 2 * Math.asin(Math.sqrt(Math.max(0, Math.min(1, h))));
}
const [from, to] = await Promise.all([
  city('Charlotte, NC'),
  city('Miami, FL'),
]);
const kilometers = distanceKm(from, to);
console.log({ from: from.name, fromState: from.state_name,
  to: to.name, toState: to.state_name,
  kilometers, miles: kilometers * 0.621371 });

City API documentation

How is the distance between cities calculated?

The calculator resolves each city to reference coordinates, then applies the great-circle formula. This is air distance, also called straight-line distance, between city centers. The result includes miles, kilometers and the two resolved places. Copy distance keeps them together.

How do I choose between cities with the same name?

Add a state after a comma, such as Portland, ME. Read the two returned place names and regions before using the total.

Does this calculate driving distance or travel time?

It calculates straight-line city-center distance. Driving, flight itineraries, traffic, and journey time require route-specific information.

Why can this differ from an airport-to-airport distance?

An airport and a city center are different points. Even the same city pair can have a different distance when different airports or addresses are used.

Can I enter ZIP codes instead?

Use the ZIP distance calculator for postal-code endpoints.

Why was a city not found?

Check its spelling and add a state when applicable. Both cities must resolve to usable coordinates; an unknown endpoint is not assigned an estimated distance.