我想做的是根据 2 个坐标之间的距离过滤一堆 wordpress 帖子。在 URL 中传递了用户输入的坐标、范围和类别,如下所示:
/?cat=0&s=5041GW&range=250&lat=51.5654368&lon=5.071263999999928
然后有一些帖子(不是所有帖子)都有我使用高级自定义字段插件创建的纬度和经度字段。这些是我传递给 get_posts 以获取按类别过滤的帖子的参数:
$args = array(
'posts_per_page' => 24,
'category' => $_GET["cat"],
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'adressen',
'post_status' => 'publish',
);
现在我要做的是修改它,以便在实际传递范围和位置时,帖子将被过滤以仅返回位置在用户搜索位置范围(以公里为单位)内的帖子为了。我似乎无法为此找到一个好的解决方案,因为我很难使用 wordpress 及其拥有的插件。我真的很感激我能理解的解决方案。
最佳答案
这在计算上可能相当昂贵。最直接的方法是获取所有符合条件的帖子,然后遍历所有帖子,丢弃指定范围之外的帖子。
困难的出现是因为米和纬度/经度之间没有线性映射。这取决于你在地球上的哪个位置。参见 this question了解详情。 PHPcoord library存在为您进行此计算,但由于我提出的答案略微近似,我将使用 this website 中描述的近似方法。使用 Haversine formula .
我将使用以下公式:
计算两个纬度/经度坐标之间的距离(以公里为单位):
x = Δλ ⋅ cos φm y = Δφ d = R ⋅ √(x² + y²)
其中 φ 是以弧度为单位的纬度,λ 是以弧度为单位的经度,R 是地球的半径(平均半径 = 6,371 公里)
在给定起始经纬度、距离和方位角的情况下计算目的地:
φ2 = asin( sin φ1 ⋅ cos δ + cos φ1 ⋅ sin δ ⋅ cos θ ) λ2 = λ1 + atan2( sin θ ⋅ sin δ ⋅ cos φ1, cos δ − sin φ1 ⋅ sin φ2 )
其中 θ 是方位角(从北顺时针方向),δ 是角距离 d/R,d 是行进的距离。参见 atan2 .
因此,我们将定义以下辅助函数:
const R = 6371; // km
function distance_between_points_rad($lat1, $lng1, $lat2, $lng2){
// latlng in radians
$x = ($lng2-$lng1) * cos(($lat1+$lat2)/2);
$y = ($lat2-$lat1);
// return distance in km
return sqrt($x*$x + $y*$y) * R;
}
function get_destination_lat_rad($lat1, $lng1, $d, $brng){
return asin( sin($lat1)*cos($d/R) +
cos($lat1)*sin($d/R)*cos($brng) );
}
function get_destination_lng_rad($lat1, $lng1, $d, $brng){
$lat2 = get_destination_lat_rad($lat1, $lng1, $d, $brng);
return $lng1 + atan2(sin($brng)*sin($d/R)*cos($lat1),
cos($d/R)-sin($lat1)*sin($lat2));
}
function get_bounding_box_rad($lat, $lng, $range){
// latlng in radians, $range in km
$latmin = get_destination_lat_rad($lat, $lng, $range, 0);
$latmax = get_destination_lat_rad($lat, $lng, $range, deg2rad(180));
$lngmax = get_destination_lng_rad($lat, $lng, $range, deg2rad(90));
$lngmin = get_destination_lng_rad($lat, $lng, $range, deg2rad(270));
// return approx bounding latlng in radians
return array($latmin, $latmax, $lngmin, $lngmax);
}
function distance_between_points_deg($lat1, $lng1, $lat2, $lng2){
// latlng in degrees
// return distance in km
return distance_between_points_rad(
deg2rad($lat1), deg2rad($lng1), deg2rad($lat2), deg2rad($lng2) );
}
function get_bounding_box_deg($lat, $lng, $range){
// latlng in degrees, $range in km
return array_map(rad2deg,
get_bounding_box_rad(deg2rad($lat), deg2rad($lng), $range));
}
现在,一般流程应该是:
- 创建一个方形边界框,将帖子过滤成仅 几乎没有什么是正确的。这不应该太计算 昂贵,但是可能会留下一些边缘帖子的近似值 出,并包括一些不适合的帖子。
- 优化返回的 仅发布给符合要求的人。这是一个计算 昂贵的过程,因此是第一阶段。排除在外的几个帖子 第一步仍将被排除在外。边界框可以 可能会变得更大以适应。
您要使用的查询应包含元信息: 见here对于其中一些元查询的有用指南
$lat1 = $_GET['lat']; // degrees
$lng1 = $_GET['lng']; // degrees
$range = $_GET['range']; // km
// get the approximate bounding box
$bbox = get_bounding_box_deg($lat1, $lng1, $range);
// query the posts
$args = array(
'posts_per_page' => 24,
'category' => $_GET["cat"],
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'adressen',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'lat',
'value' => array( $bbox[0], $bbox[1] ),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
array(
'key' => 'lng',
'value' => array( $bbox[2], $bbox[3] ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
$the_query = new WP_Query( $args );
然后帖子在循环中被过滤掉:
// Then filter the posts down in the loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$custom_fields = get_post_custom();
if (isset($custom_fields['lat']) && isset($custom_fields['lng'])){
$lat2 = $custom_fields['lat'];
$lng2 = $custom_fields['lng'];
$dist = distance_between_points_deg($lat1, $lng1, $lat2, $lng2);
if ($dist <= $range){
// post is in range
} else {
// post out of range, discard
}
} else {
// post has no latlng coords
}
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
WordPress 代码未经测试,如果仍然存在错误,我们深表歉意。不过,一般概念是正确的。
关于php - 按坐标之间的距离过滤 wordpress 帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29752033/