Search code examples
distance

Manhattan Distance for two geolocations


Let's say I have two locations represented by latitude and longitude. Location 1 : 37.5613 , 126.978 Location 2 : 37.5776 , 126.973

How can I calculate the distance using Manhattan distance ?

Edit : I know the formula for calculating Manhattan distance like stated by Emd4600 on the answer which is |x1-x2| - |y1-y2| but I think it's for Cartesian. If it is can be applied that straight forward |37.5613-37.5776| + |126.978-126.973| what is the distance unit of the result ?


Solution

  • Given a plane with p1 at (x1, y1) and p2 at (x2, y2), it is, the formula to calculate the Manhattan Distance is |x1 - x2| + |y1 - y2|. (that is, the difference between the latitudes and the longitudes). So, in your case, it would be:

    |126.978 - 126.973| + |37.5613 - 37.5776| = 0.0213
    

    EDIT: As you have said, that would give us the difference in latitude-longitude units. Basing on this webpage, this is what I think you must do to convert it to the metric system. I haven't tried it, so I don't know if it's correct:

    First, we get the latitude difference:

    Δφ = |Δ2 - Δ1|
    Δφ = |37.5613 - 37.5776| = 0.0163
    

    Now, the longitude difference:

    Δλ = |λ2 - λ1|
    Δλ = |126.978 - 126.973| = 0.005
    

    Now, we will use the haversine formula. In the webpage it uses a = sin²(Δφ/2) + cos φ1 ⋅ cos φ2 ⋅ sin²(Δλ/2), but that would give us a straight-line distance. So to do it with Manhattan distance, we will do the latitude and longitude distances separately.

    First, we get the latitude distance, as if longitude was 0 (that's why a big part of the formula got omitted):

    a = sin²(Δφ/2)
    c = 2 ⋅ atan2( √a, √(1−a) )
    latitudeDistance = R ⋅ c // R is the Earth's radius, 6,371km
    

    Now, the longitude distance, as if the latitude was 0:

    a = sin²(Δλ/2)
    c = 2 ⋅ atan2( √a, √(1−a) )
    longitudeDistance = R ⋅ c // R is the Earth's radius, 6,371km
    

    Finally, just add up |latitudeDistance| + |longitudeDistance|.