src/utility/eposLeaflet/components/controls/searchControl/searchControl.ts
Properties |
|
Methods |
|
constructor(showSearchResultsLayer)
|
||||
Parameters :
|
Protected eposLeaflet |
Type : EposLeafletComponent
|
Inherited from
AbstractControl
|
Defined in
AbstractControl:38
|
The line |
Protected leafletMapObj |
Type : L.Map
|
Inherited from
AbstractControl
|
Defined in
AbstractControl:31
|
The line |
Public addTo | ||||||
addTo(leafletMapObj: L.Map)
|
||||||
Inherited from
AbstractControl
|
||||||
Defined in
AbstractControl:37
|
||||||
Parameters :
|
Protected closeExpanders | ||||||||
closeExpanders(clickedElement: HTMLElement)
|
||||||||
Inherited from
AbstractControl
|
||||||||
Defined in
AbstractControl:161
|
||||||||
The function closes all expanded elements within a specific container when a specific element is clicked. represents the element that was clicked.
Parameters :
Returns :
void
|
Protected createExpander | ||||||||||||
createExpander(faIconClasses: string, title: string)
|
||||||||||||
Inherited from
AbstractControl
|
||||||||||||
Defined in
AbstractControl:113
|
||||||||||||
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 :
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 :
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 :
|
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;
}
}