File

libs/map/src/lib/control/locate/locate.service.ts

Index

Methods

Constructor

constructor(mapCache: MapCache)
Parameters :
Name Type Optional
mapCache MapCache No

Methods

Private removeMarker
removeMarker(map: L.Map)
Parameters :
Name Type Optional
map L.Map No
Returns : void
Public startLocate
startLocate(id: string)
Parameters :
Name Type Optional
id string No
Returns : void
Public stopLocate
stopLocate(id: string)
Parameters :
Name Type Optional
id string No
Returns : void
import { Injectable } from '@angular/core';
import * as L from 'leaflet';

import { MapCache } from '../../base/map-cache.service';

const LOCATION_FOUND_EVENT = 'locationfound';
const LOCATION_ERROR = 'locationerror';
const LOCATED_MARKER_ID = 'located';

@Injectable()
export class LocateService {

  constructor(
    protected mapCache: MapCache
  ) { }

  public startLocate(id: string) {
    const map = this.mapCache.getMap(id);
    map.on(LOCATION_FOUND_EVENT, (evt: L.LocationEvent) => {
      this.removeMarker(map);
      const marker = L.marker(evt.latlng).addTo(map);
      marker.options.title = LOCATED_MARKER_ID;
    });
    map.on(LOCATION_ERROR, (error) => {
      console.error(error);
    });
    map.locate({
      watch: true,
      setView: true,
      timeout: 30000
    });
  }

  public stopLocate(id: string) {
    const map = this.mapCache.getMap(id);
    map.stopLocate();
    map.off(LOCATION_FOUND_EVENT);
    this.removeMarker(map);
  }

  private removeMarker(map: L.Map) {
    map.eachLayer((entry) => {
      if (entry instanceof L.Marker && entry.options.title === LOCATED_MARKER_ID) {
        map.removeLayer(entry);
      }
    });
  }

}

result-matching ""

    No results matching ""