Ahead On The Route¶
Quick reference¶
Anchoring the search at the driver’s current position answers what is around me. Planning a pause asks what will be around me in half an hour, so that a break — and the detour it costs — can be chosen upfront rather than at the next exit. This notebook moves the search anchor n minutes forward along the route, out of the route spans and without any extra API call: reading the spans, measuring the elapsed time at the driver’s position, interpolating the point one horizon further, and drawing the horizons on a map. Most-used elements:
RoutingAPI.routes(..., spans=("duration", "dynamicSpeedInfo", "length"))→RouteSection.spanselapsed_sec_at_position(spans, waypoints, lat, lon)position_at_x_sec_ahead(spans, waypoints, elapsed, x_sec)RouteEngine.set_mins_from_pos(mins)/RouteEngine.search_at_positionRouteController.build_mins_from_pos_text_w()/RouteController.draw_future_position()API.browse(..., polyline=..., width=...)anchored at the future position
dynamicSpeedInfo is what splits the route into spans: spans=("duration", "length") alone
returns a single span covering the whole route.
For the complete reference, see the API reference.
Methods used in this notebook¶
- here_search_demo.widgets.route_geometry.elapsed_sec_at_position(spans, waypoints, lat, lon)
Return the elapsed seconds from the route start to the waypoint closest to
(lat, lon).Each span dict must contain
offset(int) andduration(float, seconds). Following the HERE Routing API convention,spans[i]['offset']is the index of the first waypoint of span i, so span i covers the waypoints up tospans[i + 1]['offset'](the last waypoint for the last span).- Return type:
float
- here_search_demo.widgets.route_geometry.position_at_x_sec_ahead(spans, waypoints, elapsed, x_sec)
Return the
(lat, lon)that isx_secseconds ahead ofelapsedalong the route.If the target is beyond the end of the route the final waypoint is returned.
- Return type:
tuple[float,float]
- here_search_demo.widgets.route_geometry.haversine_m(lat1, lon1, lat2, lon2)
Great-circle distance in metres between two
(lat, lon)points.- Return type:
float
- class here_search_demo.routing_api.RoutingAPI(credentials=None, cache=None, raise_for_status=True, options=None, log_fn=None, on_request_sent=None)
https://www.here.com/docs/bundle/routing-api-v8-api-reference/page/index.html
- async routes(session, origin, destination, via=None, transport_mode=None, return_fields=None, spans=None, x_headers=None, **kwargs)
Calls the HERE Routing
/v8/routesendpoint.- Parameters:
session (
ClientSession) – instance of HTTPSessionorigin – route origin, a
Waypointor a(lat, lon[, name_hint])tupledestination – route destination, in the same formats as origin
via – optional intermediate waypoint(s), in the same formats as origin
transport_mode (
str|None) – overrides the instancetransportModeoptionreturn_fields (
Sequence[str] |None) – overrides the instancereturnoptionspans (
Sequence[str] |None) – overrides the instancespansoptionx_headers (
dict|None) – Optional X-* headers (X-Request-Id, …)kwargs – additional request URL parameters
- Return type:
Response- Returns:
a Response object whose
datais the routing payload
- static sections(response, route_index=0)
Return the sections of one route of a routing response.
The Routing API starts a new section at each via waypoint and wherever the journey changes transport mode – a Berlin/London route is split by the channel ferry, for instance. Callers that need the
at → resultandresult → stoplegs must therefore group these sections withsplit_at()andmerge_sections()rather than assume one section per leg.- Parameters:
response (
Response) – aResponsereturned byroutes()route_index (
int) – index in theroutes[]array
- Return type:
list[RouteSection]- Returns:
list of
RouteSection
- class here_search_demo.route_engine.RouteEngine(credentials, *, routing_api=None, on_routing_request=None, route_cache=None, width=100, retrieve_route_fn=None)
Head-agnostic route state and routing retrieval service.
This class intentionally has no widget dependencies. It stores route/ranking state, computes derived search context, manages route cache, and retrieves route geometry through
RoutingAPI.
- class here_search_demo.api.API(credentials=None, cache=None, raise_for_status=True, options=None, log_fn=None, on_request_sent=None, testing_header=False)
https://docs.here.com/geocoding-and-search/reference
- async browse(session, latitude, longitude, categories=None, food_types=None, chains=None, polyline=None, width=None, all_along=None, x_headers=None, **kwargs)
Calls HERE Search Browse endpoint
- Parameters:
session (
ClientSession) – instance of HTTPSessionlatitude (
float) – search center latitudelongitude (
float) – search center longitudecategories (
Sequence[str] |None) – Places category ids for filteringfood_types (
Sequence[str] |None) – Places cuisine ids for filteringchains (
Sequence[str] |None) – Places chain ids for filteringx_headers (
dict|None) – Optional X-* headers (X-Request-Id, X-AS-Session-ID, …)
- Param:
kwargs: additional request URL parameters
- Return type:
Response- Returns:
a Response object