maps · caching · ecowell · performance · database
Real Road Distances and ETAs: Google Routes, Geocoding, and Caching Derived Data
The URL approach from the previous post is free but gives us no numbers. For ETAs (“technician arrives in about 18 minutes”) and distance reports we need an actual route calculation. We use two Google services: Geocoding (address to coordinates) and the Routes API (road distance and duration).

Calling the Routes API carefully
Three habits keep the integration small and safe:
- Ask for only the fields you need. A field mask of
routes.distanceMeters,routes.durationkeeps responses small and billing in the cheapest tier. - Always set a timeout. Ours is 12 seconds. A map provider having a bad day must not hold our request open.
- Validate what comes back. We accept the result only if distance is a sane integer (1 m to 2,000 km) and duration is a positive whole number of seconds. Otherwise we return
422 ROUTE_NOT_FOUND. Never pass a provider’snullorNaNinto a customer’s screen.
Failure modes get honest, distinct codes: no API key configured is 503 GOOGLE_ROUTES_NOT_CONFIGURED, a provider outage is 503 ROUTE_PROVIDER_UNAVAILABLE, an unusable address is 422.
The provider is an interface, not a dependency
The route code calls a small routerProvider and geocoder, and Google is one implementation. Tests inject a fake fetcher, so the whole path is tested without network or keys. If we change provider later, one class changes.
Geocode once, store it
Customers’ addresses do not move. Geocoding the same address on every request wastes money and time, so we cache the result on the customer record:
alter table crm_customers
add column address_latitude numeric(10,7),
add column address_longitude numeric(10,7),
add column address_geocoded_at timestamptz;
Three rules make that cache safe:
- The address text stays the source of truth. Coordinates are derived data. If geocoding fails or the cache is empty, route generation falls back to the address text.
- Database constraints guard the values. Latitude and longitude must both be present or both be null, latitude within -90 to 90, longitude within -180 to 180. A bug in the app cannot store half a coordinate.
- A timestamp records when we geocoded, so stale entries can be refreshed when an address changes.
The ETA honesty rule
Customer live tracking shows “your technician is on the way” with an ETA. Our rule: the ETA comes from the route provider or it is empty. If the provider is unconfigured, the address is missing, or the call fails, the field is null and the app simply shows no ETA. We do not fall back to “distance divided by an assumed speed”, because a wrong number that looks confident is worse than no number.
Privacy limits on live location
The same endpoint is deliberately narrow:
- Only the customer who owns the job, in the same branch, sees anything.
- Only while the technician is on the way. It stops on arrival, cancel, reassign or completion.
- Stale or mock GPS fixes are withheld.
- Only the current position is returned. There is no history to scrape.
Takeaways
- Field masks, timeouts and response validation are three cheap lines that prevent most map-integration incidents.
- Cache derived data (coordinates), keep the source (address), and put constraints in the database.
- Hide behind an interface so tests and future providers are easy.
- If you cannot compute the number, show nothing. Do not invent one.