php 根据两地经纬度计算距离,单位可调节。

public function distance($lat1, $lon1, $lat2, $lon2, $unit) {

  $theta = $lon1 - $lon2;
  $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
  $dist = acos($dist);
  $dist = rad2deg($dist);
  $miles = $dist * 60 * 1.1515;
  $unit = strtoupper($unit);

  if ($unit == "K") {
    return ($miles * 1.609344);
  } else if ($unit == "N") {
      return ($miles * 0.8684);
    } else {
        return $miles;
      }
}

 public function customerlist(){

       $serviceid=$_POST['service_id'];

        $lat=$_POST['lat'];
        $lng=$_POST['lng'];

        // $lat=39.73838; // 纬度
        // $lng=116.339128; // 经度

        if(empty($lat)||empty($lng)){
            Error('定位失败',-2001);
        }
        // 三元桥 116.464052,39.967359
        // 国贸 116.467524,39.914319
        // $serviceid=367;
       $where['o.serviceid']=$serviceid;
       $where['u.type']='1';
       // 找到这服务人员下所有的订单

      $data =D('Order')->field('o.userid as clientid,sc.scorelevel as starts,u.photo as clientphoto,u.realname as clientname,u.lat,u.lng')->table('t_order as o')->join('t_order_evaluation_s_c as sc on sc.memberid=o.serviceid')->join('t_user as u on o.userid =u.id')->where($where)->group('u.id')->select();

      // echo $m->getlastsql();

      foreach ($data as $key => &$value) {
          if($value['lng']&&$value['lat']){
           $data[$key]['distance']=(string)round($this->distance($lat,$lng,$value['lat'],$value['lng'],"K"),2)."Km";
           unset($data[$key]['lng']);
           unset($data[$key]['lat']);
          }else{
            unset($data[$key]);
          }
      }

      $count=COUNT($data);

            Success(array('list'=>$data,'count'=>$count),1001);

    }

如何根据地点知道经纬度。答案是百度拾取器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值