Directions widget persistance in Angular Component

698
2
Jump to solution
03-08-2022 12:45 PM
lawsonkendall
New Contributor II

I have a Directions widget (@arcgis/core/widgets/Directions) within an Angular Component.  

The component gets deleted whenever a user navigates away from the component (as is typical for Angular components).  If a user enters addresses on the Directions form, resulting in successful calculation of a route + rendering of the route line on the map; closes the component, then later re-opens the component, I would like for their previously entered data to (still) be on the Directions form.

If I remove the call to this.directionsWidget.destroy() inside ngOnDetroy() the route line remains on the map when the component is closed.  So far, this is good and the desired behavior. 

 

When I reselect the component containing the Directions widget, causing a new component to be created, it has no memory of the previously entered stops.  I can use a service to save the Directions widget, but any attempts to re-connect the old Directions widget with the new component div simply results in a blank space where the Directions form is normally displayed.  Likewise, I can retrieve/restore "lastRoute", or individual stops from the "old" widget.  But it doesn't appear as if there's any means to add an already existing Route, or set of stops, to a newly constructed Directions widget programmatically.

 

If I can't connect an existing Directions widget (containing already-retrieved route info) to a new view div (preferred solution), is it at least possible to:

  • construct a new Directions widget using previously cached Route info? or
  • add stops, or Route data to a Directions widget programmatically (e.g. from within Angular's ngAfterViewInit())?

From looking at the Directions API documentation it doesn't seem as if any of these things is possible, but I'm hoping someone can point out something I've overlooked.

Software Versions:

  • ArgGIS: 4.22.2
  • Angular: 13.1.3 (TypeScript 4.5.4)

Thank you.

 

(For what it's worth, my first attempt to solve this problem was to implement Angular's RouteReuseStrategy interface.  This doesn't work because the component is constructed dynamically within another component, without any url/path changes.  I suppose it might be hypothetically possible to make this approach work by restructuring the parent component so all child components have their own sub-path, but this looks like a major refactoring I would prefer to avoid at this time.)

0 Kudos
1 Solution

Accepted Solutions
AndyGup
Esri Regular Contributor

Hi @lawsonkendall can you provide a github repo example? I'm not 100% certain I understand the architecture. On the surface the implementation sounds like its unsupported because its intermixing Angular component life-cycle with the ArcGIS widget life-cycle.

> If I can't connect an existing Directions widget (containing already-retrieved route info) to a new view div 

This isn't possible. All of the APIs out-of-the-box widgets have private undocumented dependencies on the MapView (or SceneView), so a widget can't be disconnected from a View and then reattached.

I'm tagging @Noah-Sager for any input he may have related to caching route info and programmatic access.

View solution in original post

2 Replies
AndyGup
Esri Regular Contributor

Hi @lawsonkendall can you provide a github repo example? I'm not 100% certain I understand the architecture. On the surface the implementation sounds like its unsupported because its intermixing Angular component life-cycle with the ArcGIS widget life-cycle.

> If I can't connect an existing Directions widget (containing already-retrieved route info) to a new view div 

This isn't possible. All of the APIs out-of-the-box widgets have private undocumented dependencies on the MapView (or SceneView), so a widget can't be disconnected from a View and then reattached.

I'm tagging @Noah-Sager for any input he may have related to caching route info and programmatic access.

lawsonkendall
New Contributor II

I kind of knew the answer as I posted the question.  I've read and re-read the API for Directions and it doesn't support it.  Your response just confirms what I already suspected.  Thanks for taking the time to look at this.

0 Kudos