Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:

i am building a software for Taxi company and i want to Calcuate distance from pickup point to drop off and the will provide the fare. how can i acheive this using PHP

What I have tried:

i already tried using distance api but it is not working for me
M-Badger 28-Jan-24 5:55am    
It would be much more helpful if you included the code that you have tried
PIEBALDconsult 28-Jan-24 11:33am    
I suspect a spam set-up.
PIEBALDconsult 28-Jan-24 10:17am    
As someone who has written code for a taxi company (twenty years ago), I don't see why you would be doing this rather than buying an existing taxi routing/scheduling package -- there are (or were) many.
Furthermore, it seems that in many areas, ride sharing services have killed traditional taxi services, so this does not seem like a reasonable business to be getting into now. The taxi company I worked for is no longer in business, maybe the software companies have also died.
Similarly, if you are actually trying to start a ride sharing service, you are way behind the established leaders.

1 solution

Presuming that you are using the Distance Matrix API (and not the Directions API) then the form of a request should be :

And your code should look something like this :
Obviously you need to replace "YOUR_API_KEY" with your API key from Google for which you need a billing account set up and Maps Javascript API enabled. The full set of optional parameters are described below. You will receive a response in JSON format (or XML if you replace json? with xml? that you need to parse. An example of some PHP code to parse a Distance Matrix API request is shown below. All the information you need can be found at Distance Matrix API request and response  |  Google for Developers[^].

// We need to get the JSON response into a string variable $jsonResponse
// First assign the URL of the Distance Matrix API request to a string
$url = 'href="';

// Use file_get_contents() to send a GET request to the URL
$jsonResponse = file_get_contents($url);

// Decode the JSON response
$data = json_decode($jsonResponse, true);

// Check if the status is OK
if ($data['status'] == 'OK') {
    // Loop through each row (origin-destination pair)
    foreach ($data['rows'] as $row) {
        // Loop through each element (information about the origin-destination pair)
        foreach ($row['elements'] as $element) {
            // Check if the status is OK
            if ($element['status'] == 'OK') {
                // Get the distance and duration
                $distance = $element['distance']['text'];
                $duration = $element['duration']['text'];

                // Print the distance and duration
                echo "Distance: $distance\n";
                echo "Duration: $duration\n";
            } else {
                echo "Error: " . $element['status'] . "\n";
} else {
    echo "Error: " . $data['status'] . "\n";

Distance Matrix API Request :

Where outputFormat may be either json or xml.

And parameters include :

The starting point for calculating travel distance and time. You can supply one or more locations in the form of an address, latitude/longitude coordinates, or a place ID.
One or more locations to which to calculate travel distance and time.
mode (optional)
Specifies the mode of transport. Options include driving, walking, bicycling, and transit.
units (optional)
Specifies the unit system to use when expressing distance as text. Options include metric and imperial.
departure_time (optional)
Specifies the desired time of departure as an integer in seconds since midnight, January 1, 1970 UTC.
traffic_model (optional)
Specifies the assumptions to use when calculating time in traffic. Options include best_guess, pessimistic, and optimistic.

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900