Location Classes

What if we want to find the distance and travel times between locations when we know where we’re starting and going, but also where we need the locations to match in some way? In this case, we’ll make use of the location_class parameter.

Let’s consider the exaggerated real-world example started in the Sources and Destinations example. In that example, we had the current locations for a fleet of three (3) vehicles and locations of three (3) available fueling stations. In this example, we’ll add the twist that each vehicle takes a particular type of fuel, and only certain fueling location offers this kind of fuel. We start by assigning an arbitrary class number to each fuel type:

Fuel TypeClass
Gas0
Diesel1
Electric2

To frame the problem further, below is our fleet and fueling locations each with their designated location class:

Fleet (location class)Fueling Locations (location class)
vehicle-gas (0)fueling-gas (0)
vehicle-diesel (1)fueling-gas+diesel (0 and 1)
vehicle-electric (2)fueling-electric (2)

Let’s imagine this problem as a distance matrix. Obviously, we’re only interested in the distances from vehicles to fueling locations where the fuel types match, and not interested in the distances for:

  • A vehicle or fueling location to itself — where would it get fuel?
  • Vehicles to other vehicles — other vehicles don’t have fuel to share
  • Fueling locations to other fueling locations — other stations don’t have fuel to share
  • Fueling locations to vehicles — the station cannot go to the vehicle (although that would be cool)
  • Vehicles to fueling locations without the correct fuel — we’d be stranded at a gas station if we needed diesel

In the distance matrix template below, we’ve added a helpful zero-based "index" as a visual guide which is not part of the actual distance matrix inputs. There is also a new location_class which indicates the arbitrary class designation based on the type of fuel needed. Note that as a result of using an index-based approach, ORDER MATTERS. This is not an ideal design and is being updated in the next major revision of the API.

index012345
location class0120[0, 1]2
destinationsvehicle-gasvehicle-dieselvehicle-electricfueling-gasfueling-gas+dieselfueling-electric
sources
00vehicle-gasYESYES
11vehicle-dieselYES
22vehicle-electricYES
30fueling-gas
4[0, 1]fueling-gas+diesel
52fueling-electric

In order to limit our distance matrix request so that it mirrors our template above and only returns relevant distances, we specify the sources, destinations, and location_class parameters using the indices and classes of each entry.

Sample Request

The complete request we will send to the Distance Matrix endpoint is given below.

{
    "sources": [
        0,
        1,
        2
    ],
    "destinations": [
        3,
        4,
        5
    ],
    "vehicle": {
        "type": "car"
    },
    "locations": [
        {
            "location_id": "vehicle-gas",
            "latitude": 33.937244,
            "longitude": -84.36947,
            "location_class": [
                0
            ]
        },
        {
            "location_id": "vehicle-diesel",
            "latitude": 33.8875767,
            "longitude": -84.2613857,
            "location_class": [
                1
            ]
        },
        {
            "location_id": "vehicle-electric",
            "latitude": 33.8875767,
            "longitude": -84.2613857,
            "location_class": [
                2
            ]
        },
        {
            "location_id": "fueling-gas",
            "latitude": 33.8968108,
            "longitude": -84.3276536,
            "location_class": [
                0
            ]
        },
        {
            "location_id": "fueling-gas+diesel",
            "latitude": 33.8730743,
            "longitude": -84.3951042,
            "location_class": [
                0,
                1
            ]
        },
        {
            "location_id": "fueling-electric",
            "latitude": 33.8940743,
            "longitude": -84.2151042,
            "location_class": [
                2
            ]
        }
    ]
}

Sample Response

The complete response we receive from the Distance Matrix endpoint is given below. As expected, we only have travel_costs which show going from sources to destinations where one of the location_class is included:

  • vehicle-gas to fueling-gas and fueling-gas+diesel
  • vehicle-diesel to fueling-gas+diesel
  • vehicle-electric to fueling-electric
{
    "locations": [
        {
            "location_id": "vehicle-gas",
            "time_zone": "America/New_York"
        },
        {
            "location_id": "vehicle-diesel",
            "time_zone": "America/New_York"
        },
        {
            "location_id": "vehicle-electric",
            "time_zone": "America/New_York"
        },
        {
            "location_id": "fueling-gas",
            "time_zone": "America/New_York"
        },
        {
            "location_id": "fueling-gas+diesel",
            "time_zone": "America/New_York"
        },
        {
            "location_id": "fueling-electric",
            "time_zone": "America/New_York"
        }
    ],
    "status": "Ok",
    "traffic_windows": [
        {
            "traffic_window_end_times": [
                "06:30:00",
                "00:00:00"
            ],
            "traffic_window_index": 0,
            "traffic_window_start_times": [
                "00:00:00",
                "19:00:00"
            ]
        },
        {
            "traffic_window_end_times": [
                "07:30:00"
            ],
            "traffic_window_index": 1,
            "traffic_window_start_times": [
                "06:30:00"
            ]
        },
        {
            "traffic_window_end_times": [
                "08:30:00"
            ],
            "traffic_window_index": 2,
            "traffic_window_start_times": [
                "07:30:00"
            ]
        },
        {
            "traffic_window_end_times": [
                "09:30:00"
            ],
            "traffic_window_index": 3,
            "traffic_window_start_times": [
                "08:30:00"
            ]
        },
        {
            "traffic_window_end_times": [
                "16:00:00"
            ],
            "traffic_window_index": 4,
            "traffic_window_start_times": [
                "09:30:00"
            ]
        },
        {
            "traffic_window_end_times": [
                "17:00:00"
            ],
            "traffic_window_index": 5,
            "traffic_window_start_times": [
                "16:00:00"
            ]
        },
        {
            "traffic_window_end_times": [
                "18:00:00"
            ],
            "traffic_window_index": 6,
            "traffic_window_start_times": [
                "17:00:00"
            ]
        },
        {
            "traffic_window_end_times": [
                "19:00:00"
            ],
            "traffic_window_index": 7,
            "traffic_window_start_times": [
                "18:00:00"
            ]
        }
    ],
    "travel_costs": {
        "vehicle-diesel": {
            "fueling-gas+diesel": [
                20019.3,
                1187.1,
                1349.8,
                1638,
                1579,
                1383.8,
                1473.5,
                1484.9,
                1427.1
            ]
        },
        "vehicle-electric": {
            "fueling-electric": [
                7212.6,
                658.3,
                623.5,
                670.7,
                697.3,
                739.3,
                807.3,
                828.1,
                835.1
            ]
        },
        "vehicle-gas": {
            "fueling-gas": [
                9132.2,
                620.8,
                712.2,
                845.3,
                805.1,
                788.6,
                1046.4,
                1075.7,
                912
            ],
            "fueling-gas+diesel": [
                9002.9,
                760.8,
                746,
                831.9,
                887.3,
                922.6,
                1006.7,
                1008.5,
                949.5
            ]
        }
    }
}