【发布时间】:2014-12-10 18:38:52
【问题描述】:
我有一个名为“事件”的自定义帖子类型,其中包含经纬度的 acf。我想获得与帖子相关的所有“事件”的距离 我使用 geoplugin.com 来获取用户的经纬度。
$geoplugin = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR']) );
$geoiix = $geoplugin['geoplugin_regionName'] ;
$user_lat = $geoplugin['geoplugin_latitude'];
$user_long = $geoplugin['geoplugin_longitude'];
$artist_id = $tags[0]->term_id;
$args = array(
'post_type' => 'events',
'orderby' => 'meta_value',
'meta_key' => 'event_date',
'tag__in' => array( $artist_id ),
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) : while ($my_query->have_posts()) :
$my_query->the_post();
$event_lat = get_field( 'gp_latitude' );
$event_long = get_field( 'gp_longitude' );
$earth_radius = 3960.00; # in miles
$lat_1 = $event_lat;
$lon_1 = $event_long;
$lat_2 = $user_lat;
$lon_2 = $user_long;
$delta_lat = $lat_2 - $lat_1 ;
$delta_lon = $lon_2 - $lon_1 ;
function distance_haversine($lat1, $lon1, $lat2, $lon2) {
global $earth_radius;
global $delta_lat;
global $delta_lon;
$alpha = $delta_lat/2;
$beta = $delta_lon/2;
$a = sin(deg2rad($alpha)) * sin(deg2rad($alpha)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * sin(deg2rad($beta)) * sin(deg2rad($beta)) ;
$c = asin(min(1, sqrt($a)));
$distance = 2*$earth_radius * $c;
$distance = round($distance, 4);
return $distance;
}
$hav_distance = distance_haversine($lat_1, $lon_1, $lat_2, $lon_2);
echo $hav_distance;
?>
此代码破坏了它的显示 1 结果,我似乎无法弄清楚原因。我想也许高级自定义字段插件不是存储经纬度的最佳方式。
【问题讨论】:
-
你的代码没有关闭 if - while 标记。
-
我只是没有在这里展示
-
您的活动帖子有(全部)纬度和经度元值吗?
-
是的,它们都有 lat 和 long 值。我使用高级自定义字段插件来制作元字段
-
你确定你在循环中使用你的
distance_haversine()函数和echo()吗?