calculate toll in map in python

To calculate toll in a map using Python, you can use an API that provides information about toll roads or toll plazas. One such API is the TollGuru API (https://tollguru.com/developers/docs/), which provides information on toll prices, per-vehicle service costs, and more.

Here is an example code snippet using the TollGuru API to calculate the toll between two locations:

main.py
import requests
import json

def get_toll_price(start_lat, start_lng, end_lat, end_lng, vehicle_type):
    url = 'https://dev.tollguru.com/v1/calc/route'
    headers = {
        'Content-Type': 'application/json',
        'x-api-key': 'your_api_key_here'
    }
    data = {
        'source': {
            'coords': [start_lat, start_lng]
        },
        'destination': {
            'coords': [end_lat, end_lng]
        },
        'vehicles': [
            {
                'class': vehicle_type
            }
        ],
        'departure_time': '2022-01-01T10:00:00Z'
        # You can change the date and time as per your requirement
    }
    response = requests.post(url, headers=headers, data=json.dumps(data))
    toll_data = response.json()

    # Extracting toll price from the response
    toll_price = toll_data['route']['costs']['tag']['total']
    return toll_price
884 chars
31 lines

In the above code, start_lat and start_lng represent the latitude and longitude of the starting location; end_lat and end_lng represent the latitude and longitude of the destination location; and vehicle_type represents the type of vehicle (e.g., Car, Truck, etc.).

You will need to replace your_api_key_here with your actual API key for the TollGuru API. You can sign up for a free API key at https://tollguru.com/signup.

The function get_toll_price() sends a POST request to the TollGuru API with the required parameters and headers, and returns the toll price for the given route.

related categories

gistlibby LogSnag