Calculating distance between zip codes in PHP

前端 未结 5 1835
北荒
北荒 2020-11-28 04:32

I grabbed a database of the zip codes and their langitudes/latitudes, etc from this This page. It has got the following fields:

ZIP, LATITUDE, LONGIT

5条回答
  •  爱一瞬间的悲伤
    2020-11-28 05:09

    Check out the Haversine formula for calculating great circle distances between two points. Some more samples can be found here

    Haversine formula:

    • R = earth’s radius (mean radius = 6,371km)
    • Δlat = lat2− lat1
    • Δlong = long2− long1
    • a = sin²(Δlat/2) + cos(lat1).cos(lat2).sin²(Δlong/2)
    • c = 2.atan2(√a, √(1−a))
    • d = R.c

    (Note that angles need to be in radians to pass to trig functions).

提交回复
热议问题