DistanceMatrixService component calculates travel distance and duration between multiple origins and destinations using the Google Maps Distance Matrix API.
Import
Usage
Props
The distance matrix request configuration.Required fields:
origins: Array of one or more origin locations (can be string addresses, LatLng, or Place objects)destinations: Array of one or more destination locationstravelMode: Mode of transportation (DRIVING, WALKING, BICYCLING, or TRANSIT)
avoidHighways: Whether to avoid highwaysavoidTolls: Whether to avoid tollsavoidFerries: Whether to avoid ferriesunitSystem: Unit system for distances (METRIC or IMPERIAL)region: The region code for biasing the resultsdrivingOptions: Additional options for driving mode (departure time, traffic model)transitOptions: Options for transit mode
callback
(response: google.maps.DistanceMatrixResponse | null, status: google.maps.DistanceMatrixStatus) => void
required
Callback function invoked when the distance matrix request completes.Parameters:
response: The distance matrix response object if successful, ornullif the request failedstatus: The status of the request (OK, INVALID_REQUEST, MAX_ELEMENTS_EXCEEDED, etc.)
rows: Array of distance/duration results for each originoriginAddresses: Formatted addresses of the originsdestinationAddresses: Formatted addresses of the destinations
Callback invoked when the distance matrix service instance has been created.Parameters:
distanceMatrixService: The distance matrix service instance
Callback invoked when the component unmounts.Parameters:
distanceMatrixService: The distance matrix service instance