I am trying to display a GPX file on leaflet into my Ionic Angular application by using GPX plugin for Leaflet. In VS Code, everything looks fine but when I run Ionic Serve, I get the following error : Property 'GPX' does not exist on type 'typeof import("TestLeaflet/node_modules/@types/leaflet/index")'.
Here is how to reproduce the issue :
ionic start TestLeaflet blank
(select Angular and Standalone)npm install leaflet
npm install @types/leaflet --save-dev
npm install leaflet-gpx
npm install @types/leaflet-gpx --save-dev
Here is the content of the file app.component.ts
:
import { Component, OnInit } from '@angular/core';
import { IonApp, IonRouterOutlet } from '@ionic/angular/standalone';
import * as L from 'leaflet';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
standalone: true,
imports: [IonApp, IonRouterOutlet],
})
export class AppComponent implements OnInit {
constructor() {}
ngOnInit() {
this.initMap();
}
private initMap(): void{
const options = {
async: true,
marker_options: {
startIconUrl: 'https://cdn.jsdelivr.net/npm/leaflet-gpx@1.7.0/pin-icon-start.png',
endIconUrl: 'https://cdn.jsdelivr.net/npm/leaflet-gpx@1.7.0/pin-icon-end.png',
shadowUrl: 'https://cdn.jsdelivr.net/npm/leaflet-gpx@1.7.0/pin-shadow.png'
}
}
const url = '../assets/gpx/rando.gpx';
const map = L.map('map');
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'Map data © <a href="http://www.osm.org">OpenStreetMap</a>'
}).addTo(map);
new L.GPX(url, options).on('loaded', (e) => {
map.fitBounds(e.target.getBounds());
}).addTo(map);
}
}
So far everything is good in VS Code, but when I run Ionic serve, I get the following error :
Property 'GPX' does not exist on type 'typeof import("Projects/TestLeaflet/node_modules/@types/leaflet/index")'. [ng] 35 new L.GPX(url, options).on('loaded', (e) => {
I already tried without success :
- Replace
import * as L from 'leaflet';
byimport * as L from 'leaflet-gpx';
- Add
import * as LGPX from 'leaflet-gpx';
and usenew LGPX.GPX(url, options...
which givesProperty 'GPX' does not exist on type 'typeof import("TestLeaflet/node_modules/@types/leaflet-gpx/index")'
What am I missing ?
Thanks for your help !