Skip to content
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

Fix error if using shapeAt within line boundaries but outside of tolerance #174

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions example/delete-shape.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
<div id='map'></div>

<script type="text/javascript">
var startPoint = [43.1249, 1.254];
var map = L.map('map', {editable: true}).setView(startPoint, 16),
var startPoint = [43.1249, 1.254];
var svgRenderer = L.svg({tolerance: 10}); // Set tolerance to allow fuzzy hit detection for a line.
var map = L.map('map', {
editable: true,
renderer: svgRenderer
}).setView(startPoint, 16),
tilelayer = L.tileLayer('http://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png', {maxZoom: 20, attribution: 'Data \u00a9 <a href="http://www.openstreetmap.org/copyright"> OpenStreetMap Contributors </a> Tiles \u00a9 HOT'}).addTo(map);

L.NewLineControl = L.Control.extend({
Expand Down Expand Up @@ -71,10 +75,25 @@
var deleteShape = function (e) {
if ((e.originalEvent.ctrlKey || e.originalEvent.metaKey) && this.editEnabled()) this.editor.deleteShapeAt(e.latlng);
};
// Attach listeners for direct click on a shape.
map.on('layeradd', function (e) {
if (e.layer instanceof L.Path) e.layer.on('click', L.DomEvent.stop).on('click', deleteShape, e.layer);
if (e.layer instanceof L.Path) e.layer.on('dblclick', L.DomEvent.stop).on('dblclick', e.layer.toggleEdit);
});
// If we click elsewhere on the map, look for a nearby shape (given renderer tolerance).
map.on('click', function (e) {
if (e.originalEvent.ctrlKey || e.originalEvent.metaKey) {
map.eachLayer(function (layer) {
// Look for layer that's an editable feature.
if (typeof layer.shapeAt === 'function') {
var foundShape = layer.shapeAt(e.latlng);
if (foundShape !== null && layer.editEnabled()) {
layer.editor.deleteShape(layer.getLatLngs());
}
}
});
}
});

var line = L.polyline([
[43.1292, 1.256],
Expand All @@ -83,6 +102,12 @@
]).addTo(map);
line.enableEdit();

var line2 = L.polyline([
[43.1291, 1.252],
[43.1308, 1.255]
]).addTo(map);
line2.enableEdit();

var multi = L.polygon([
[
[
Expand Down
1 change: 1 addition & 0 deletions src/Leaflet.Editable.js
Original file line number Diff line number Diff line change
Expand Up @@ -1816,6 +1816,7 @@
if (!latlngs) return false;
var i, k, len, part = [], p,
w = this._clickTolerance();
if (latlngs instanceof L.LatLng) latlngs = [latlngs]
this._projectLatlngs(latlngs, part, this._pxBounds);
part = part[0];
p = this._map.latLngToLayerPoint(l);
Expand Down