使用距离矩阵逐点计算票价

时间:2018-08-08 14:31:19

标签: php

我正在开发一种出租车应用程序,其中所有乘客都前往同一目的地。

不仅仅是获得一个起点和目的地,还有一种方法来获取旅客与最终目的地之间所有距离的总和吗?

出租车费用计算

//Fees to driver
feesToDriver = (DistFromAToB + DistFromBToC + DistFromCToDest) * RM1.81

RatioOfPassenger = DistFromDest + DistFromFirstPassengerToCurrentPassenger / ( DistFromAToDest + DistFromBToDest + DistFromCToDest + DistFromAToB + DistFromAToB + DistFromBToC)

FeesPaidByPassenger = feesToDriver * RatioOfPassenger

计算PHP

function get_coordinates($address)
    {
        $address = urlencode($address);
        $url = "http://maps.google.com/maps/api/geocode/json?address=$address";
        $ch = curl_init();
        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);
        $status = $response_a->status;

        if ( $status == 'ZERO_RESULTS' )
        {
            return FALSE;
        }
        else
        {
            $return = array('lat' => $response_a->results[0]->geometry->location->lat, 'long' => $long = $response_a->results[0]->geometry->location->lng);
            return $return;
        }
    }

    function GetDrivingDistance($lat1, $lat2, $long1, $long2)
    {
        $url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$lat1.",".$long1."&destinations=".$lat2.",".$long2."&mode=driving";
        $ch = curl_init();
        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);
        $dist = $response_a['rows'][0]['elements'][0]['distance']['text'];
        $time = $response_a['rows'][0]['elements'][0]['duration']['text'];

        return array('distance' => $dist, 'time' => $time);
    }

    $coordinatesFrom = get_coordinates($data['from']);
    $coordinatesTo = get_coordinates($data['to']);
    if ( !$coordinatesFrom || !$coordinatesTo )
    {
        echo 'Bad address.';
    }
    else
    {
        $dist = GetDrivingDistance($coordinatesFrom['lat'], $coordinatesTo['lat'], $coordinatesFrom['long'], $coordinatesTo['long']);
        $return['distance'] = $dist['distance'];
        $return['time'] = $dist['time'];
    }
    echo(json_encode($return));

    $fee = $dist['distance'] * 2.15;

0 个答案:

没有答案