8000 TypeScript example for leaflet-locatecontrol 0.83.1 · Issue #362 · domoritz/leaflet-locatecontrol · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

TypeScript example for leaflet-locatecontrol 0.83.1 #362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
RupertBarrow opened this issue Mar 6, 2025 · 0 comments
Open

TypeScript example for leaflet-locatecontrol 0.83.1 #362

RupertBarrow opened this issue Mar 6, 2025 · 0 comments

Comments

@RupertBarrow
Copy link

It would be nice if you could provide a TypeScript example (with Leaflet) : I could not get v0.83.1 to work with Leaflet 1.9.4 and Ionic 8 + Angular 19 + TypeScript 5.8.2

FYI, I use leaflet-locatecontrol 0.81.1 with :

// src/types/leaflet.locatecontrol.d.ts

import * as L from 'leaflet';

declare module 'leaflet' {
  interface LocateControlOptions {
    initialZoomLevel: boolean | number;
    position?: L.ControlPosition;
    layer?: L.LayerGroup;
    setView?: boolean | 'untilPan' | 'always';
    keepCurrentZoomLevel?: boolean;
    flyTo?: boolean;
    clickBehavior?: {
      inView?: 'stop' | 'setView';
      outOfView?: 'stop' | 'setView';
      inViewNotFollowing?: 'stop' | 'setView';
    };
    returnToPrevBounds?: boolean;
    cacheLocation?: boolean;
    drawCircle?: boolean;
    drawMarker?: boolean;
    markerClass?: L.Class;
    circleStyle?: L.PathOptions;
    markerStyle?: L.PathOptions;
    followCircleStyle?: L.PathOptions;
    followMarkerStyle?: L.PathOptions;
    icon?: string;
    iconLoading?: string;
    iconElementTag?: string;
    circlePadding?: number[];
    metric?: boolean;
    onLocationError?: (err: any) => void;
    onLocationOutsideMapBounds?: (control: L.Control.Locate) => void;
    showPopup?: boolean;
    strings?: {
      title?: string;
      metersUnit?: string;
      feetUnit?: string;
      popup?: string;
      outsideMapBoundsMsg?: string;
    };
    locateOptions?: L.LocateOptions;
    showCompass: boolean;
  }

  namespace Control {
    class Locate extends L.Control {
      constructor(options?: LocateControlOptions & L.ControlOptions);

      start(): void;
      stop(): void;
      setView(): void;
      stopFollowing(): void;
      getLocation(): L.LatLng | undefined;

      options: LocateControlOptions & L.ControlOptions;
    }
  }

  namespace control {
    function locate(options?: LocateControlOptions & L.ControlOptions): Control.Locate;
  }
}

and

// src/app/components/map/leaflet/controls.ts

import * as L from 'leaflet';
import 'leaflet.locatecontrol';
...
export const ZOOM_LEVEL = 12;

export const LOCATE_CONTROL_OPTIONS: L.LocateControlOptions = {
  initialZoomLevel: ZOOM_LEVEL,
  position: 'topright',
  drawMarker: true,
  drawCircle: true,
  showPopup: true,
  showCompass: true,
  strings: {
    title: 'Show my current location',
  },
};

and

// src/app/components/map/map.component.ts

...
import { LOCATE_CONTROL_OPTIONS } from './leaflet/controls';
...
export class MapComponent implements OnInit {
   ...
  async ngOnInit() {
    const osm = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution: '© OpenStreetMap contributors' });

    this.modalsManager.presentMapModal();

    const map = L.map('map', {
      zoom: ZOOM_LEVEL,
      zoomControl: false,
      zoomSnap: 0.1,
      layers: [osm],
    }).on('load', () => {
      // Set the map and display markers the first time the map is displayed
      this.mapDataService.init(map);

      // Initialize custom controls and display them at the same time as the map
      this._initCustomControls(map);
    });
  }

   ...
  private _initCustomControls(map: L.Map) {
    const locateControl = L.control.locate(LOCATE_CONTROL_OPTIONS);
    this.customControlsContainer.appendChild(locateControl.onAdd(map));
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant
0