Overview
TheMapViewRoute component provides several callback props that are invoked at different points in the route lifecycle. Use these callbacks to respond to route data, handle errors, and implement custom navigation features.
Callbacks
onReady
Called when the route has been successfully fetched from the Google Routes API and decoded into coordinates.Callback invoked when the route is ready to render.Parameters:
coordinates- Array of decoded LatLng points that make up the route polyline
- After successful API response
- After polyline decoding
- Before the polyline is rendered on the map
- Called again whenever route parameters change (origin, destination, mode, etc.)
onError
Called when an error occurs during route fetching or processing.Callback invoked when an error occurs.Parameters:
error- Standard JavaScript Error object with message and stack trace
- Network errors (no internet, timeout, etc.)
- API errors (invalid API key, quota exceeded, invalid parameters)
- Response parsing errors
- Any other errors during route fetching
- Invalid or missing API key
- Routes API not enabled in Google Cloud Console
- API quota exceeded
- Invalid coordinates
- Network connectivity issues
- Server errors from Google Routes API
onEstimatedTime
Called with the estimated travel time for the route.Callback invoked with estimated travel duration.Parameters:
time- Estimated travel time in milliseconds
- Must set
enableEstimatedTime={true}prop
- After successful route calculation
- Whenever the route changes
- Time is provided in milliseconds
- Includes traffic conditions if available
- Based on Google Routes API duration field
- Must enable with
enableEstimatedTime={true}prop
onDistance
Called with the total distance of the route.Callback invoked with total route distance.Parameters:
distance- Total route distance in meters
- Must set
enableDistance={true}prop
- After successful route calculation
- Whenever the route changes
- Distance is provided in meters
- Represents actual route distance (not straight-line distance)
- Based on Google Routes API distanceMeters field
- Must enable with
enableDistance={true}prop
onLegs
Called with detailed information about route segments (legs) and turn-by-turn steps.Callback invoked with detailed route leg and step data.Parameters:
legs- Array of GoogleRouteLeg objects containing segment information
- Must specify
legFieldsand/orlegStepFieldsprops
- After successful route calculation
- Only if legFields or legStepFields are specified
- Whenever the route changes
- Legs represent segments between waypoints
- Steps provide turn-by-turn navigation instructions
- Fields included depend on
legFieldsandlegStepFieldsprops - Duration strings are in format like “3600s” (seconds)
- Only requested fields will be present in the response
Complete Example with All Callbacks
See Also
- MapViewRoute Component - Component overview
- Props Reference - Complete props documentation
- Types Reference - TypeScript type definitions including GoogleRouteLeg and GoogleRouteStep