Skip to content

Instantly share code, notes, and snippets.

@kidino
Created September 9, 2023 01:49
Show Gist options
  • Save kidino/c7d2cf848a64785d30ad44f8747858be to your computer and use it in GitHub Desktop.
Save kidino/c7d2cf848a64785d30ad44f8747858be to your computer and use it in GitHub Desktop.

To retrieve a list of locations within a certain distance (e.g., 5 kilometers) from a given latitude-longitude location using MySQL's spatial features, you can use the Haversine formula, which calculates distances on the Earth's surface based on latitude and longitude. Here's how you can do it step by step:

  1. Create a Spatial Table:

    • You need to have a spatial-enabled table where you store the latitude and longitude coordinates for each location. You can create a table with a POINT column for this purpose. For example:
    CREATE TABLE locations (
        id INT AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(255),
        coordinates POINT
    );
  2. Insert Data:

    • Insert the location data into the table. You can use the ST_GeomFromText function to convert latitude and longitude values into a POINT geometry. For example:
    INSERT INTO locations (name, coordinates) VALUES
        ('Location 1', ST_GeomFromText('POINT(40.7128 -74.0060)')),
        ('Location 2', ST_GeomFromText('POINT(34.0522 -118.2437)')),
        -- Add more locations
  3. Perform a Spatial Query:

    • Use a spatial query to find locations within a specified distance from a given latitude-longitude point. You can use the Haversine formula in MySQL to calculate the distance. Here's an example query to find locations within 5 kilometers of a specific point (e.g., New York City):
    SELECT
        id,
        name,
        ST_X(coordinates) AS latitude,
        ST_Y(coordinates) AS longitude,
        (
            6371 * ACOS(
                COS(RADIANS(40.7128)) * COS(RADIANS(ST_X(coordinates))) *
                COS(RADIANS(-74.0060 - ST_Y(coordinates))) +
                SIN(RADIANS(40.7128)) * SIN(RADIANS(ST_X(coordinates)))
            )
        ) AS distance_km
    FROM locations
    HAVING distance_km <= 5;
    • In this query:
      • 6371 is the approximate radius of the Earth in kilometers.
      • 40.7128 and -74.0060 are the latitude and longitude of the reference point (New York City). You can replace these values with your desired latitude and longitude.
      • ST_X(coordinates) and ST_Y(coordinates) extract the latitude and longitude from the POINT geometry column.
      • The HAVING clause filters locations that are within 5 kilometers of the reference point.
  4. Result:

    • The query will return a list of locations within the specified distance from the reference point. The distance_km column contains the calculated distances from the reference point to each location.

By using the Haversine formula and MySQL's spatial features, you can efficiently find locations within a certain distance from a given latitude-longitude location in your database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment