2

I've been doing this in MySQL for years, but how can I do this in PHP?

Example CoordinateArray: http://pastebin.com/grVsbgL9

I'd like to return all the coordinates within 100 miles of a given coordinate.

function getCoordinatesWithinRadius ($coordinateArray, $center, $radius) {
    //
    return $resultArray;
}

In MySQL I typically used a query like:

SELECT *, ( 3959 * acos( cos( radians($latitude) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians($longitude) ) + sin( radians($latitude) ) * sin( radians( latitude ) ) ) ) AS distance FROM table HAVING distance < $radius ORDER BY distance;

How can I do the same in PHP?

4

2 回答 2

3

在下面的代码中,我假设您的坐标对象仅存储为两个元素数组(用于$center参数和$coordinateArray 中的坐标)。如果不是,那么您需要更改的内容应该是相当明显的。

function getCoordinatesWithinRadius ($coordinateArray, $center, $radius) {
  $resultArray= array();
  $lat1 = $center[0];
  $long1 = $center[1];
  foreach ($coordinateArray as $coordinate) {
    $lat2 = $coordinate[0];
    $long2 = $coordinate[1];
    $distance = 3959 * acos(cos(radians($lat1)) * cos(radians($lat2)) * cos(radians($long2) - radians($long1)) + sin(radians($lat1)) * sin(radians($lat2)));
    if ($distance < $radius) $resultArray[] = $coordinate;
  }
  return $resultArray;  
}

代码只是遍历所有坐标,计算到中心点的距离,如果它小于给定的$radius,则将坐标添加到$resultArray

我没有检查$distance计算 - 这基本上只是从你的 mysql 查询中剪切和粘贴,所以我假设这是正确的。

上面的代码还假设存在弧度函数。如果你没有,你可以像这样添加一个:

function radians($deg) { 
  return $deg * M_PI / 180;
}

这是假设坐标以度为单位。

于 2013-06-24T23:00:53.870 回答
0

这是詹姆斯分享的更完善的功能版本

 /**
 * @author WPExperts
 * @usange to get array of all markers within certain radius
 * @param $coordinateArray
 * @param $center
 * @param $radius
 * @return array
 */
function wpe_getCoordinatesWithinRadius ( $lat1 , $lng1 , $radius , $available_markers ) {
    $stores_data = $available_markers;
    $resultArray= array();
    foreach ( $stores_data as $store ) {
        $lat2 = $store->lat;
        $lng2 = $store->lng;
        $distance = 3959 * acos(cos(wpe_radians($lat1)) * cos(wpe_radians($lat2)) * cos(wpe_radians($lng2) - wpe_radians($lng1)) + sin(wpe_radians($lat1)) * sin(wpe_radians($lat2)));
        if ($distance < $radius){
            $resultArray[] = (object) array( 'ID' => $store->ID , 'lat' => $store->lat , 'lng' => $store->lng , 'distance' => $distance );
        }
    }
    // need to return id,distance,lat and lng
    return $resultArray;
}

/**
 * @usage to convert degree into radians
 * @param $deg
 * @return float
 */
function wpe_radians($deg) {
    return $deg * M_PI / 180;
}

希望这可以帮助社区中的其他人。

于 2017-04-24T09:09:38.040 回答