mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-13 11:17:22 +00:00
The DrawEvents.Created layer variable is not of type Layer. As noted by the in-code documentation, the Leaflet.Draw.DrawEvents.Created.layer variable is one of Leaflet's vector shapes or a markers. https://github.com/Leaflet/Leaflet.draw/blob/c6af99b761c5d9900bf5a3e2e1d8827bbde29542/src/Leaflet.Draw.Event.js#L31 The L.Draw.Event.CREATED event is only fired by Draw.Feature in the _fireCreatedEvent method. Draw.Feature bequeaths _fireCreatedEvent to it's subclasses Draw.Marker, Draw.Polyline, and Draw.SimpleShape, which in turn bequeath the method to Draw.Circle, Draw.Rectangle (both from Draw.SimpleShape), Draw.CircleMarker (from Draw.Marker), and Draw.Polygon (from Draw.Polyline). Draw.Circle, Draw.CircleMarker, Draw.Marker, Draw.Rectangle and Draw.Polyline override the _fireCreatedEvent to pass instances of their respective vector classes to _fireCreatedEvent, meaning that the layer variable is not of type Layer, but instead one of the vector or marker classes. For example, Draw.Circle passes L.Circle to L.Draw.Feature.prototype._fireCreatedEvent as the argument to the `layer` parameter. Note that Draw.Polygon inherits Draw.Polyline's behavior without changes, but changes the vector class via the Poly class variable.
124 lines
3.6 KiB
TypeScript
124 lines
3.6 KiB
TypeScript
import * as L from 'leaflet';
|
|
import 'leaflet-draw';
|
|
|
|
const osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
|
|
const osmAttrib = '© <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors';
|
|
const osm = L.tileLayer(osmUrl, {maxZoom: 18, attribution: osmAttrib});
|
|
const map = L.map('map', {layers: [osm], center: L.latLng(-37.7772, 175.2756), zoom: 15 });
|
|
|
|
const drawnItems = L.featureGroup();
|
|
map.addLayer(drawnItems);
|
|
|
|
const drawControl = new L.Control.Draw({
|
|
position: 'topleft' ,
|
|
draw: {
|
|
polygon: {
|
|
allowIntersection: false,
|
|
drawError: {
|
|
color: '#b00b00',
|
|
timeout: 1000
|
|
},
|
|
shapeOptions: {
|
|
color: '#bada55'
|
|
},
|
|
showArea: true
|
|
},
|
|
polyline: {
|
|
metric: false
|
|
},
|
|
circle: {
|
|
shapeOptions: {
|
|
color: '#662d91'
|
|
}
|
|
}
|
|
},
|
|
edit: {
|
|
featureGroup: drawnItems
|
|
}
|
|
});
|
|
map.addControl(drawControl);
|
|
|
|
map.on(L.Draw.Event.CREATED, (e: L.DrawEvents.Created) => {
|
|
const type = e.layerType;
|
|
const layer = e.layer;
|
|
const geojson = e.layer.toGeoJSON();
|
|
|
|
drawnItems.addLayer(layer);
|
|
});
|
|
|
|
const examplePolygon: L.LatLngLiteral[] = [{lng: 0, lat: 0}, {lng: 10, lat: 0}, {lng: 10, lat: 10}, {lng: 0, lat: 10}, {lng: 0, lat: 0}];
|
|
const examplePolygonArea: number = L.GeometryUtil.geodesicArea(examplePolygon);
|
|
L.GeometryUtil.readableArea(examplePolygonArea, true);
|
|
|
|
function testBooleanControlOptions() {
|
|
const drawControl = new L.Control.Draw({
|
|
position: 'topleft' ,
|
|
draw: {
|
|
polygon: {
|
|
allowIntersection: false,
|
|
drawError: {
|
|
color: '#b00b00',
|
|
timeout: 1000
|
|
},
|
|
shapeOptions: {
|
|
color: '#bada55'
|
|
},
|
|
showArea: true
|
|
},
|
|
polyline: {},
|
|
circle: false
|
|
},
|
|
edit: {
|
|
featureGroup: drawnItems
|
|
}
|
|
});
|
|
}
|
|
|
|
function testExampleControlOptions() {
|
|
const editableLayers = new L.FeatureGroup([]);
|
|
map.addLayer(editableLayers);
|
|
|
|
const MyCustomMarker = L.Icon.extend({
|
|
options: {
|
|
shadowUrl: null,
|
|
iconAnchor: new L.Point(12, 12),
|
|
iconSize: new L.Point(24, 24),
|
|
iconUrl: 'link/to/image.png'
|
|
}
|
|
});
|
|
const drawControl = new L.Control.Draw({
|
|
position: 'topright',
|
|
draw: {
|
|
polyline: {
|
|
shapeOptions: {
|
|
color: '#f357a1',
|
|
weight: 10
|
|
}
|
|
},
|
|
polygon: {
|
|
allowIntersection: false, // Restricts shapes to simple polygons
|
|
drawError: {
|
|
color: '#e1e100', // Color the shape will turn when intersects
|
|
message: '<strong>Oh snap!<strong> you can\'t draw that!' // Message that will show when intersect
|
|
},
|
|
shapeOptions: {
|
|
color: '#bada55'
|
|
}
|
|
},
|
|
circle: false, // Turns off this drawing tool
|
|
rectangle: {
|
|
shapeOptions: {
|
|
// clickable: false // clickabkle is not a polyline option according to leaflet docs
|
|
}
|
|
},
|
|
marker: {
|
|
icon: new MyCustomMarker()
|
|
}
|
|
},
|
|
edit: {
|
|
featureGroup: editableLayers, // REQUIRED!!
|
|
remove: false
|
|
}
|
|
});
|
|
}
|