-1

How to get distance of multiple destination one by one from my estimate fare function..as i m getting data in below format.

I need to get distance one by one and then add all distance.

please help me to implement in my estimate fare function.

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
    )

below is my code..

 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);
    }
ndm
  • 59,784
  • 9
  • 71
  • 110
parcel pik
  • 145
  • 1
  • 8

1 Answers1

0

You can use distance matrix

Assuming you have following waypoints

  1. Source
  2. Way point 1
  3. Way point 2
  4. Destination

You can calculation distance as below

$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,

                );
    }

EDIT

//assuming $waypoint as your array
foreach($waypoint as $key=>$point) {
                        //calculate time and distance between each waypoint
                                      $distance=GetDrivingDistance($waypoint[$key-1],$point);
}
sumit
  • 15,003
  • 12
  • 69
  • 110
  • Sumit you r right..but i am asking how to pass delivery address one by one in GetDrivingDistance function as per your waypoint1,waypoint2 logic...i update my question..plz help me.. – parcel pik Jan 07 '17 at 10:01
  • why you want to do that in one function , use case please? – sumit Jan 07 '17 at 10:03
  • I dont no the count of waypoints...as delivery address is coming from clone google address textbox and it can be of n numbers..so plz suggest how to implement your logic in estimate fare function.. – parcel pik Jan 07 '17 at 10:19