Manhattan Distance for two geolocations

前端 未结 2 1793
有刺的猬
有刺的猬 2021-02-10 14:07

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

2条回答
  •  深忆病人
    2021-02-10 14:59

    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 sepparatedly.

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

    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|.

提交回复
热议问题