php 用PHP计算邮政编码之间的距离

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/407989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:38:20  来源:igfitidea点击:

Calculating distance between zip codes in PHP

phpmysqlalgorithmmathdistance

提问by Click Upvote

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, LONGITUDE, CITY, STATE, COUNTY, ZIP_CLASS

邮编、纬度、经度、城市、州、县、邮编类别

The data was in a text file but I inserted it into a MySQL table. My question now is, how can i utilise the fields above to calculate the distance between two zip codes that a user can enter on the website? Working code in PHP will be appreciated

数据在文本文件中,但我将其插入到 MySQL 表中。我现在的问题是,如何利用上面的字段来计算用户可以在网站上输入的两个邮政编码之间的距离?PHP中的工作代码将不胜感激

采纳答案by brendan

You can also try hitting a web service to calc the distance. Let someone else do the heavy lifting.

您也可以尝试使用网络服务来计算距离。让别人做繁重的工作。

https://www.zipcodeapi.com/API#distance

https://www.zipcodeapi.com/API#distance

回答by Adam Bellaire

This is mike's answer with some annotations for the magic numbers. It seemed to work fine for me for some test data:

这是 Mike 的回答,并附有一些对幻数的注释。对于一些测试数据,它似乎对我来说很好用:

function calc_distance($point1, $point2)
{
    $radius      = 3958;      // Earth's radius (miles)
    $deg_per_rad = 57.29578;  // Number of degrees/radian (for conversion)

    $distance = ($radius * pi() * sqrt(
                ($point1['lat'] - $point2['lat'])
                * ($point1['lat'] - $point2['lat'])
                + cos($point1['lat'] / $deg_per_rad)  // Convert these to
                * cos($point2['lat'] / $deg_per_rad)  // radians for cos()
                * ($point1['long'] - $point2['long'])
                * ($point1['long'] - $point2['long'])
        ) / 180);

    return $distance;  // Returned using the units used for $radius.
}

回答by Mike Paterson

It can be done with just maths...

它可以通过数学来完成......

function calc_distance($point1, $point2)
{
    $distance = (3958 * 3.1415926 * sqrt(
            ($point1['lat'] - $point2['lat'])
            * ($point1['lat'] - $point2['lat'])
            + cos($point1['lat'] / 57.29578)
            * cos($point2['lat'] / 57.29578)
            * ($point1['long'] - $point2['long'])
            * ($point1['long'] - $point2['long'])
        ) / 180);

    return $distance;
}

回答by Paul Dixon

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

查看Haversine公式来计算两点之间的大圆距离。可以在此处找到更多示例

Haversine formula:

半正弦公式:

  • R = earth's radius (mean radius = 6,371km)
  • Δlat = lat2? lat1
  • Δlong = long2? long1
  • a = sin2(Δlat/2) + cos(lat1).cos(lat2).sin2(Δlong/2)
  • c = 2.atan2(√a, √(1?a))
  • d = R.c
  • R = 地球半径(平均半径 = 6,371km)
  • Δlat = lat2?纬度1
  • Δlong = long2? 长1
  • a = sin2(Δlat/2) + cos(lat1).cos(lat2).sin2(Δlong/2)
  • c = 2.atan2(√a, √(1?a))
  • d = Rc

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

(请注意,角度需要以弧度表示才能传递给三角函数)。

回答by JV-

In your zip table you need to grab the coordinates (lat,long) for the two points for which you want to get the distance.

在您的 zip 表中,您需要获取要获取距离的两个点的坐标(纬度、经度)。

You can then either calculate the distance right in the SQL or with PHP. Both methods are outlined in this post:

然后,您可以直接在 SQL 中或使用 PHP 计算距离。这篇文章中概述了这两种方法:

http://dev.strategystar.net/2011/10/mysql-php-get-distance-between-two-coordinates-in-miles-kilometers/

http://dev.strategystar.net/2011/10/mysql-php-get-distance-between-two-coordinates-in-miles-kilometers/

The calculation in the example is based on the formula already discussed in this thread. It provides the radius for the earth in both miles and kilometers so it will allow you to get the distance between two points in both units.

示例中的计算基于此线程中已经讨论过的公式。它以英里和公里为单位提供地球的半径,因此它可以让您以两种单位获得两点之间的距离。

The link above is great because the method for calculation does not include any magic numbers, just the radius of the earth!

上面的链接很棒,因为计算方法不包含任何幻数,只包含地球的半径!