I am trying to incorporate the leaflet plugin leaflet-mapwithlabels into my angular project. However, the library does not provide an option for NPM installation. After following some guides, I attempted adding the JS file directly to the node-modules in its own folder and then importing it into my component like this:
import * as L from 'leaflet';
import {GeoJSON} from "leaflet";
import "node_modules/leaflet-mapwithlabels/dist/leaflet-mapwithlabels.js"
and including it in my angular.json file:
"styles": [
"@angular/material/prebuilt-themes/azure-blue.css",
"./node_modules/leaflet/dist/leaflet.css",
"./node_modules/leaflet-mapwithlabels/dist/leaflet-mapwithlabels.css",
"src/styles.scss"
],
"scripts": [
"./node_modules/leaflet/dist/leaflet.js",
"./node_modules/leaflet-mapwithlabels/dist/leaflet-mapwithlabels.js"
]
and referencing it here:
private map!: L.MapWithLabels;
Unfortunately, this approach did not work as expected. Any guidance on how I can successfully include it would be greatly appreciated.
Here is some relevant information:
package.json:
"devDependencies": {
"@angular-devkit/build-angular": "^18.2.1",
"@angular/cli": "^18.2.1",
"@angular/compiler-cli": "^18.2.1",
...
"dependencies": {
...
"@bluehalo/ngx-leaflet": "^18.0.2",
"@ngageoint/geopackage": "^4.2.6",
"@stijn98s/leaflet.tilelayer.pouchdbcached": "^1.0.0",
"@types/leaflet.fullscreen": "^3.0.2",
"leaflet": "^1.9.4",
"leaflet-geometryutil": "^0.10.3",
"leaflet.fullscreen": "^3.0.2",
Update: Here is my current attempt, but I am still encountering errors:
I moved the JS file and added this configuration to my angular.json:
"scripts": [
"node_modules/leaflet/dist/leaflet.js",
"./node_modules/leaflet.fullscreen/Control.FullScreen.js",
"./src/assets/scripts/leaflet-mapwithlabels.js"
]
I created a leaflet-extensions.d.ts file in my src/types directory:
import * as L from 'leaflet';
import * as geojson from "geojson";
import {Layer} from "leaflet";
declare module 'leaflet' {
interface LayerOptions {
// Specify label options here
}
// More definitions...
}
declare module "leaflet-map-with-labels" {
export = L;
}
And my usage looks like this:
import {
Injectable
} from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import * as L from 'leaflet';
import { Geometry } from 'geojson';
import { CountryProperties } from "../model/interfaces";
@Injectable({
providedIn: 'root'
})
export class MapService {
constructor(private http: HttpClient) {}
initMap(elementId: string): L.MapWithLabels {
return L.mapWithLabels(elementId, {
// Map initialization options
})
}
loadGeoJSON(url: string): Observable<GeoJSON.FeatureCollection<Geometry, CountryProperties>> {
return this.http.get<GeoJSON.FeatureCollection<Geometry, CountryProperties>>(url);
}
}
However, I am still encountering the following error:
ERROR TypeError: L.mapWithLabels is not a function
at _MapService.initMap (map.service.ts:16:14)
If anyone can provide insights on what may have gone wrong, I would greatly appreciate the help.