📦 @bespunky/angular-google-maps/directions
🧩 GoogleMapsDirectionsModule
The directions module allows you to both calculate and render directions on your map (see original docs).
The fastest way to add directions to your map is using the <bs-google-maps-directions>
directive:
<bs-google-map *bsSafe ...>
<bs-google-maps-directions [from]="'Jerusalem'" [to]="Tel Aviv"></bs-google-maps-directions>
</bs-google-map>
<bs-google-map *bsSafe ...>
<bs-google-maps-directions [through]="['Jerusalem', [31.998041, 34.731823], 'Tel Aviv']"></bs-google-maps-directions>
</bs-google-map>
The first item will be considered as origin, last one as destination and everything in between will be considered a waypoint.
❕ Note that any change to the provided places will trigger a new directions request to Google's servers.
💡 To use the directive import
GoogleMapsDirectionsModule
.
💡 Make sure you Enabled Directions API on Google Cloud and read the original docs on Limits and Restrictions for Waypoints.
Just like with geometry types and overlays, the directions module defines flexible types for places and waypoints.
In essence, it allows you to provide almost any type of data you can think of as a waypoint (even your overlays) and do something cool like the following:
<bs-google-map *bsSafe ...>
<bs-google-maps-marker [position]="someCoord" #marker="marker"></bs-google-maps-marker>
<bs-google-maps-polygon [path]="somePath" #polygon="polygon"></bs-google-maps-polygon>
<bs-google-maps-directions [through]="[marker, polygon, 'Jerusalem', [31.9, 34.7], { lat: 31.99, lng: 35 }, 'Tel Aviv']"></bs-google-maps-directions>
</bs-google-map>
Note that for
BoundsLike
places, the coordinate will be calculated as the center of the shape.
Places are expressed using the DirectionsPlace
type and can be specified as:
string | LatLng | LatLngLiteral | Place
).BoundsLike
(see Interchangeability).DirectionsWaypoint
.Waypoints are expressed using the DirectionsWaypoint
type and can be specified as:
google.maps.DirectionsWaypoint
interface.google.maps.DirectionsWaypoint
and has a BoundsLike
(see Interchangeability) assigned to the location
property.You can further customize your direction requests by binding the [config]
property and providing a google.maps.DirectionsRequest
object.
Any
origin
,destination
orwaypoints
properties you provide within the config object will be ignored as they are set by the directive.
You can further customize renderization of the directions by binding the [options]
property and providing a google.maps.DirectionsRendererOptions
object.
To set individual options, use the quick option properties (e.g. draggable
, hideRouteList
, etc.).
The module is comprised of 3 players:
Player | Job Description |
---|---|
GoogleMapsDirectionsService |
Make requests to Google for directions. |
DirectionsTransformService |
Detecting and transforming flexible directions objects to native ones. |
GoogleMapsDirectionsDirective |
Requesting and rendering directions on the map (uses GoogleMapsDirectionsService internally). |
If you're not planning on rendering the directions on the map, or you have your own mechanism for rendering them, it is preferred that you inject
GoogleMapsDirectionsService
and use it directly without importingGoogleMapsDirectionsModule
. This will allow the compiler to tree-shake the module.
Topic | Description |
---|---|
Geometry Types | Flexibility for geometries. |
Feature Maps | Best practices for maps scalability. |
Feature Components | Best practices for centralizing map features. |