There are a couple of issues:
- The draw options aren't quite right. This isn't actually causing the error, though.
- There's a bug leaflet-draw that causes the exception you're seeing.
Leaflet Draw Options
square
is not a draw option. The correct option is rectangle
. Additionally, all of the handlers are enabled by default. So, you only need to turn off the ones you don't want.
So, I think what you want is in your app.component.ts
file:
public drawOptions = {
position: 'topright',
draw: {
marker: {
icon: L.icon({
iconSize: [25, 41],
iconAnchor: [13, 41],
iconUrl: 'assets/marker-icon.png',
shadowUrl: 'assets/marker-shadow.png'
})
},
polygon: false,
circlemarker: false
}
};
The above will make sure that marker, circle, rectangle, and polyline are enabled and the others are disabled. You want to make sure to add the leaflet assets png files to the list of assets being exported by Angular CLI in your angular.json
file.
Identifying and Fixing The Error
There's something weird about leaflet-draw's build that's causing the source maps to not work. To get them working, I had to directly import the leaflet.draw-src.js
file.
At the top of app.component.ts
, I added:
import * as L from 'leaflet';
import '../../node_modules/leaflet-draw/dist/leaflet.draw-src.js'; // add this
That allows you to put a breakpoint in the leaflet-draw code to figure out what's going on. Upon doing that, it looks like there's a variable called type
that isn't declared before it's assigned to. The code is being run in strict mode, so this will throw an exception. This looks to be a bug in leaflet-draw.
Solution 1: Disable ShowArea
First, you can disable showArea
, which will prevent the problem code from running. To do this, modify the drawOptions
:
public drawOptions = {
position: 'topright',
draw: {
marker: {
icon: L.icon({
iconSize: [25, 41],
iconAnchor: [13, 41],
iconUrl: 'assets/marker-icon.png',
shadowUrl: 'assets/marker-shadow.png'
})
},
rectangle: { showArea: false }, // disable showArea
polyline: true,
polygon: false,
circlemarker: false
}
};
This isn't a great solution, cause you lose the showArea functionality.
Solution 2: Disable Strict Mode
The other solution is to disable strict mode for the Typescript compiler.
To do this, edit your tsconfig.json
and tsconfig.app.json
files, adding "noImplicitUseStrict": true
under the compilerOptions
property.
This solves the problem, but now you're not running your code in strict mode, which can lead to other issues.