我有一个带有lat / lon坐标的zipcodes数组,如下所示:
array:4 [
"04543" => array:3 [
"latitude" => "44.0300"
"longitude" => "-69.5100"
"hits" => 0
]
"04547" => array:3 [
"latitude" => "43.9800"
"longitude" => "-69.3300"
"hits" => 0
]
90210 => array:3 [
"latitude" => "34.0900"
"longitude" => "-118.4100"
"hits" => 0
]
"06042" => array:3 [
"latitude" => "41.7900"
"longitude" => "-72.5300"
"hits" => 0
]
]
我想一次一个地压缩这个数组,然后拉链确定是否有其他拉链距离该拉链的纬度/经度140英里以内。如果还有其他要点,那么我想将它们添加到' hits"键。这个想法是,如果邮政编码A太接近邮政编码B和邮政编码C,那么ZipCode A的命中值为2,因为它接近2个邮政编码。反之亦然,因为Zipcode B接近Zipcode A,它应该具有1的命中值,假设它不接近邮政编码C.
这是我到目前为止所尝试过的,但我觉得它可能不正确,因为我可能过度简化。
任何提示?它似乎与我的一些测试数据一起正常工作。
$maskedInputZips = $validInputZips;
foreach ($validInputZips as $zip => $latlon) {
foreach ($maskedInputZips as $maskedZip => $maskedLatlon) {
if ($zip != $maskedZip) {
$miles = $this->haversineGreatCircleDistance($latlon['latitude'], $latlon['longitude'], $maskedLatlon['latitude'], $maskedLatlon['longitude'], 3959);
if ($miles < (140)) {
$validInputZips[$zip]['hits'] += 1;
}
}
}
}
protected function haversineGreatCircleDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 3959)
{
// convert from degrees to radians
$latFrom = deg2rad($latitudeFrom);
$lonFrom = deg2rad($longitudeFrom);
$latTo = deg2rad($latitudeTo);
$lonTo = deg2rad($longitudeTo);
$latDelta = $latTo - $latFrom;
$lonDelta = $lonTo - $lonFrom;
$angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
return $angle * $earthRadius;
}