File

src/utility/eposLeaflet/components/controls/searchControl/searchControl.ts

Extends

AbstractControl

Index

Properties
Methods

Constructor

constructor(showSearchResultsLayer)
Parameters :
Name Optional
showSearchResultsLayer No

Properties

Protected eposLeaflet
Type : EposLeafletComponent
Inherited from AbstractControl
Defined in AbstractControl:38

The line protected eposLeaflet: EposLeafletComponent; is declaring a protected property named eposLeaflet of type EposLeafletComponent. This property is used to store an instance of the EposLeafletComponent class, which represents a Leaflet map component. It is used within the setMapComponent method to set the map component for the control, and it can be accessed within the class to perform operations related to the map component.

Protected leafletMapObj
Type : L.Map
Inherited from AbstractControl
Defined in AbstractControl:31

The line protected leafletMapObj: L.Map; is declaring a protected property named leafletMapObj of type L.Map. This property is used to store an instance of the L.Map class from the Leaflet library, representing a Leaflet map object. It is used within the addTo method to set the map object to which the control will be added, and it can be accessed within the class to perform operations related to the map object.

Methods

Public addTo
addTo(leafletMapObj: L.Map)
Inherited from AbstractControl
Defined in AbstractControl:37
Parameters :
Name Type Optional
leafletMapObj L.Map No
Protected closeExpanders
closeExpanders(clickedElement: HTMLElement)
Inherited from AbstractControl

The function closes all expanded elements within a specific container when a specific element is clicked. represents the element that was clicked.

Parameters :
Name Type Optional Description
clickedElement HTMLElement No
  • The clickedElement parameter is an HTMLElement that represents the element that was clicked.
Returns : void
Protected createExpander
createExpander(faIconClasses: string, title: string)
Inherited from AbstractControl

The function creates an expander element with a FontAwesome icon and a title. classes for the Font Awesome icon that will be displayed in the expander. These classes should be separated by spaces. for the expander icon.

Parameters :
Name Type Optional Description
faIconClasses string No
  • The faIconClasses parameter is a string that represents the classes for the Font Awesome icon that will be displayed in the expander. These classes should be separated by spaces.
title string No
  • The title parameter is a string that represents the title or tooltip text for the expander icon.
Returns : HTMLElement

an HTMLElement, specifically the expanderWrapper element.

Protected getControlContainer
getControlContainer(controlId: string, faIconClasses: string, title: string, content: HTMLElement)
Inherited from AbstractControl
Defined in AbstractControl:78

The function creates a control container element with a specified ID, icon classes, title, and content. to uniquely identify the container element in the HTML document. to the expander element. These classes determine the appearance of the icon. container. content to be displayed within the control container. It can be any valid HTML element such as a div, span, or paragraph.

Parameters :
Name Type Optional Description
controlId string No
  • A string representing the ID of the control container. This ID is used to uniquely identify the container element in the HTML document.
faIconClasses string No
  • A string representing the Font Awesome icon classes to be applied to the expander element. These classes determine the appearance of the icon.
title string No
  • The title parameter is a string that represents the title of the control container.
content HTMLElement No
  • The content parameter is an HTMLElement that represents the content to be displayed within the control container. It can be any valid HTML element such as a div, span, or paragraph.
Returns : HTMLElement

an HTMLElement, specifically the wrapperDiv element.

Public setMapComponent
setMapComponent(eposLeaflet: EposLeafletComponent)
Inherited from AbstractControl
Defined in AbstractControl:47

The function sets the EposLeafletComponent for the map component and returns the instance of the class. EposLeafletComponent.

Parameters :
Name Type Optional Description
eposLeaflet EposLeafletComponent No
  • The eposLeaflet parameter is of type EposLeafletComponent.
import * as L from 'leaflet';

// types in the geocoder seem very messed up at the moment.
// this might be becasue of our angular 12 switch
import * as ELG from 'esri-leaflet-geocoder';
import { FaMarker } from '../../marker/faMarker/faMarker';
import { AbstractControl } from '../abstractControl/abstractControl';
import { MarkerLayer } from '../../layers/markerLayer';
import { environment } from 'environments/environment';


export class SearchControl extends AbstractControl {

  constructor(protected showSearchResultsLayer = true) {
    super();
  }

  public addTo(
    leafletMapObj: L.Map,
  ): this {
    // add the clear button
    const control = document.createElement('div');
    control.classList.add('search-clear');
    control.classList.add('leaflet-control');
    control.classList.add('hidden');
    control.insertAdjacentHTML('beforeend', '<i class="fas fa-times"></i>');
    control.addEventListener('click', () => {
      this.eposLeaflet.removeLayerById('geosearch');
      control.classList.add('hidden');
    });
    setTimeout(() => {
      this.eposLeaflet.getElement()
        .getElementsByClassName('geocoder-control')[0]
        .insertAdjacentElement('afterend', control);
    }, 500);

    const searchControl: L.esri.Geocoding.Geosearch = ELG['geosearch']({
      providers: [
        ELG['arcgisOnlineProvider']({
          // ELG['GeocodeServiceProvider']({
          url: 'https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/',
          // API Key to be passed to the ArcGIS Online Geocoding Service
          token: environment.esriApiKey,
        })]
    } as L.esri.Geocoding.GeosearchObject);

    const searchResultsMarkers = new MarkerLayer('geosearch', 'Search Results');
    L.layerGroup().addTo(leafletMapObj);
    const markerIcon = new FaMarker()
      .configure(null, '#00a1ff')
      .configureIcon(['fas', 'fa-search'], '#ffffff');

    // have to cast due to dodgy esri leaflet types
    searchControl.on('results', ((data: L.esri.Geocoding.Results) => {
      if (data.results != null) {
        if (this.showSearchResultsLayer) {
          searchResultsMarkers.clearMarkers();
          data['results'].forEach((itemData: L.esri.Geocoding.ResultObject, index: number) => {
            searchResultsMarkers.addMarker(
              index.toString(),
              (itemData.latlng as L.LatLng).lat,
              (itemData.latlng as L.LatLng).lng,
              markerIcon,
              // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
              itemData.properties['LongLabel'] as string | HTMLElement,
            );
          });
          this.eposLeaflet.addLayer(searchResultsMarkers);
          // show clear icon
          this.eposLeaflet.getElement()
            .getElementsByClassName('search-clear')[0]
            .classList.remove('hidden');
        } else {
          // just go to the first
          if (data['results'].length > 0) {
            // needs a delay to avoid default functionality of fit bounds all results
            setTimeout(() => {
              // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
              const bounds = data.results[0].bounds!;
              this.eposLeaflet.fitBounds(bounds);
            }, 500);
          }
        }
      }
    }) as unknown as L.LeafletEventHandlerFn);
    searchControl.setPosition(this.getPosition());
    searchControl.addTo(leafletMapObj);
    return this;
  }
}

results matching ""

    No results matching ""