【问题标题】:How to calculate distance for multiple destination php如何计算多个目的地php的距离
【发布时间】:2017-01-07 07:26:44
【问题描述】:

如何从我的估算票价函数中一一获取多个目的地的距离..因为我正在获取以下格式的数据。

我需要一一获取距离,然后将所有距离相加。

请帮我实现我的预估票价功能。

Array
    (
        [pickupadd] => smartData Enterprises (I) Ltd., MIHAN, Nagpur, Maharashtra, India
        [delivery_add] => Array
            (
                [0] => TCS Bhosari, MIDC, Pimpri-Chinchwad, Maharashtra, India
                [1] => Reva University, Bengaluru, Karnataka, India
                [2] => GTR, Narayan Shasthri Road, Mysuru, Karnataka, India
            )

        [deliveryType] => 2
    )

下面是我的代码..

 public function estimateFare(){
        if (!empty($this->request->data)) { 
          $this->autoRender = false;            
          $baseFare = Configure::read('base_fare');     
          $pickup = $this->request->data['pickupadd'];          
          $deliveryType = $this->request->data['deliveryType'];
          $delivery = $this->request->data['delivery_add'];

            if(!empty($pickup) && !empty($delivery) && !empty($deliveryType)){  
                if($deliveryType == 1){
                    $distInKm = $this->GetDrivingDistance($pickup,$delivery);
                    print_r($distInKm); exit();
                    if(!empty($distInKm)){
                        foreach ($distInKm as $key => $value) {
                            $dist = $value;
                            $price = $dist * $baseFare;
                            print_r($price); exit();
                        }
                    }
                }                
            }          
        }
    }

    function GetDrivingDistance($origin, $destination){   
    print_r($destination) ; exit();  
        $url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".urlencode($origin)."&destinations=".urlencode($destination)."&mode=driving";
        $ch = curl_init($url);
       // curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $response = curl_exec($ch);
        curl_close($ch);
        $response_a = json_decode($response, true);
        print_r($response_a);exit();
        $dist = $response_a['rows'][0]['elements'][0]['distance']['text'];
        $time = $response_a['rows'][0]['elements'][0]['duration']['text'];
        $dist_val = $response_a['rows'][0]['elements'][0]['distance']['value'];
        $time_val = $response_a['rows'][0]['elements'][0]['duration']['value'];
        return array('distance' => $dist,'time' => $time,'distance_value' => $dist_val, 'time_value' => $time_val);
    }

【问题讨论】:

    标签: php google-maps google-distancematrix-api distance-matrix


    【解决方案1】:

    你可以使用距离矩阵

    假设您有以下航点

    1. 来源
    2. 路点1
    3. 路点2
    4. 目的地

    您可以如下计算距离

    $d1=GetDrivingDistance($source, $waypoint1);
    $d2=GetDrivingDistance($waypoint1, $waypoint2);
    $d3=GetDrivingDistance($waypoint2, $destination);
    $distance=$d1['distance']+$d2['distance']+$d3['distance'];
    
    
    
    
    function GetDrivingDistance($origin, $destination){
            $url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".urlencode($origin)."&destinations=".urlencode($destination)."&mode=driving&region=AU";
            $ch = curl_init($url);
           // curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            $response = curl_exec($ch);
            curl_close($ch);
            $response_a = json_decode($response, true);
            //print_r($response_a);
            $dist = $response_a['rows'][0]['elements'][0]['distance']['text'];
            $time = $response_a['rows'][0]['elements'][0]['duration']['text'];
            $dist_val = $response_a['rows'][0]['elements'][0]['distance']['value'];
            $time_val = $response_a['rows'][0]['elements'][0]['duration']['value'];
    
            return array('distance' => $dist, 
                        'time' => $time,
                        'distance_value' => $dist_val, 
                        'time_value' => $time_val,
    
                    );
        }
    

    编辑

    //assuming $waypoint as your array
    foreach($waypoint as $key=>$point) {
                            //calculate time and distance between each waypoint
                                          $distance=GetDrivingDistance($waypoint[$key-1],$point);
    }
    

    【讨论】:

    • Sumit you r right..但我问如何根据你的waypoint1,waypoint2 逻辑在 GetDrivingDistance 函数中一一传递送货地址……我更新我的问题..请帮帮我..
    • 为什么要在一个函数中这样做,请用例?
    • 我不计算航点的数量......因为送货地址来自克隆谷歌地址文本框,它可以是 n 个数字......所以请建议如何在估算票价功能中实现您的逻辑。 .
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-25
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    相关资源
    最近更新 更多