CO2

Example 3 – Integration with the Toll for Maps API

The CO2 API can be integrated with the Toll for Maps API, so in addition to the return of toll fares, the calculation of carbon emissions will also be provided.

The process consists of adding the CO2 API parameters to the body of the request to the Toll for Maps APIs.

You don’t need to include the parameter totalDistance, as the distance traveled will already be in the Directions API response.

The following example shows a request for a route between São Paulo – SP and Campinas – SP, to be traveled by a vehicle using gasoline as fuel and with an average consumption of 3.7 Km/l. We will also inform you of the price of fuel, which is $4.90 per liter.

Endpoint used:https://api.maplink.global/toll/v1/directions?vehicleType={VEHICLE_TYPE} (Método: POST)

The body of the request must contain the response from the Directions API (Google), added with the parameters of the CO2 API, as shown in the example below:

{
    //Resposta da Directions API 
    "maplink": {
        "emission": {
            "source": "GHG_PROTOCOL",
            "fuelType": "BR_GASOLINE",
            "autonomy": 3.7,
            "fuelPrice": 4.9
        }
    }
}

In the response below, the information regarding CO2 emissions can be found in the object emission, where:

  • fuelType – Confirmation of the fuel type selected;
  • source – Confirmation of the source used for the emission calculation;
  • fuelConsumed – Amount of fuel consumed on the route (in liters);
  • totalFuelPrice – Total amount of fuel consumed on the route;
  • totalEmission – Amount of CO2 emitted on the route (in Kg).

The full response can be found below:

{
    "results": [
        {
            "legs": [
                {
                    "tolls": [
                        {
                            "id": "13",
                            "name": "Pedágio - Campo Limpo",
                            "address": "SP 348 - Rod. dos Bandeirantes",
                            "city": "Campo Limpo",
                            "state": {
                                "name": "São Paulo",
                                "code": "SP"
                            },
                            "country": "Brasil",
                            "concession": "Autoban",
                            "direction": "NORTH",
                            "coordinates": {
                                "latitude": -23.3260346,
                                "longitude": -46.8231453
                            },
                            "serviceTypes": [
                                {
                                    "serviceId": "48",
                                    "name": "Via Facil"
                                },
                                {
                                    "serviceId": "35353480390470101",
                                    "name": "ANTTCODE"
                                }
                            ],
                            "price": 31.80
                        },
                        {
                            "id": "73",
                            "name": "Pedágio - Valinhos Norte",
                            "address": "SP 330 - Rod. Anhanguera Km 82",
                            "city": "Valinhos",
                            "state": {
                                "name": "São Paulo",
                                "code": "SP"
                            },
                            "country": "Brasil",
                            "concession": "Autoban",
                            "direction": "NORTHWEST",
                            "coordinates": {
                                "latitude": -23.01295484,
                                "longitude": -47.0230555
                            },
                            "serviceTypes": [
                                {
                                    "serviceId": "50",
                                    "name": "Via Facil"
                                },
                                {
                                    "serviceId": "35353300820000101",
                                    "name": "ANTTCODE"
                                }
                            ],
                            "price": 31.50
                        }
                    ],
                    "legTotalCost": 63.30
                }
            ],
            "emission": {
                "fuelType": "BR_GASOLINE",
                "source": "GHG_PROTOCOL",
                "fuelConsumed": 25.06,
                "totalFuelPrice": 122.78,
                "totalEmission": 33.724
            },
            "totalCost": 63.30
        }
    ]
}