人人商城根据经纬度计算距离
AD: 会员限时优惠仅需29元起,先到先得。【我要上车】
涉及函数 GetDistance(),返回公里(km),函数位置
ewei_shop/core/model/util.php
function GetDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2) { $pi = 3.1415926; $er = 6378.137; $radLat1 = ($lat1 * $pi) / 180; $radLat2 = ($lat2 * $pi) / 180; $a = $radLat1 - $radLat2; $b = ($lng1 * $pi) / 180 - ($lng2 * $pi) / 180; $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))); $s = $s * $er; $s = round($s * 1000); if( 1 < $len_type ) { $s /= 1000; } return round($s, $decimal); }
应用
$lng = $_GPC['lng']; $lat = $_GPC['lat']; $list = pdo_fetchall('select * from ' . tablename('ewei_shop_store') . ' where uniacid=:uniacid and status=1 order by displayorder desc,id desc', array(':uniacid' => $_W['uniacid'])); foreach ($list as $key => $value ) { $list[$key]['dast'] = m('util')->GetDistance($value['lat'], $value['lng'], $lat, $lng, 2) . 'km'; }