Interacting with the Control

There are broadly four types of interaction with the Control from the Angular Component:

  1. Using Structural Directives to control the control,

  2. interactions between

Feed Data Back

Some controls are associated to data returned to the app. The word choice is deliberate, I give the example of location data that is "associated" with the location control but generated by the map.

My prefered methid to expose the data to the app is to output the data from the control component even thought the data is coming from the map. This improves readability and means that the listeners are created and destroyed correctly as the control is clawed up and clawed down in app DOM.

So:

  1. the listeners should be setup when the control is added to the map,

  2. the listeners should be removed when the component is destroyed,

  3. there should be an Output property for the value and new values should be .emit() 'ed on this property.

  4. IN the parent component, the directive should listen to the value and call a local method when it receives a new value.

e.g.

@Input() set map(map: Map){
    if (map) {
      this._map = map;
      this.control = new Control.Locate(this.options);
      this.control.addTo(map);
      let location$ = this.location$;
      map.on('locationfound', function(e) {
        const newLocation = new Location(latLng(e.latlng.lat, e.latlng.lng, Math.round(e.altitude || 0)),e.accuracy || 0);
        newLocation.speed = e.speed || 0;
        newLocation.timestamp = Date.now();
        location$.emit(newLocation);
      });
    }
  }
  get map(): Map {
    return this._map
  }

and

<leaflet-locate-control 
    [map]="map"
    [options]="locateOptions"
    (location$)="onNewLocation($event)"
    ></leaflet-locate-control>

Worked Examples

For some worked examples of these issues and some discussion, see:

The Demo code is included in this stackblitz

Last updated