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

Polyline fix #661

Merged
merged 2 commits into from
Jan 2, 2017
Merged
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
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "leaflet-draw",
"description": "Vector drawing plugin for Leaflet",
"version": "0.4.7",
"version": "0.4.8",
"main": [
"dist/leaflet.draw.js",
"dist/leaflet.draw.css",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "leaflet-draw",
"version": "0.4.7",
"version": "0.4.8",
"description": "Vector drawing plugin for Leaflet",
"devDependencies": {
"eslint": "^3.5.0 <3.6.0",
Expand Down
87 changes: 74 additions & 13 deletions src/draw/handler/Draw.Polyline.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ L.Draw.Polyline = L.Draw.Feature.extend({
}

this._mouseMarker
.on('mousedown', this._onMouseDown, this)
.on('mouseout', this._onMouseOut, this)
.on('mouseup', this._onMouseUp, this) // Necessary for 0.8 compatibility
.on('mousemove', this._onMouseMove, this) // Necessary to prevent 0.8 stutter
.on('mousedown', this._onMouseDown, this)
.on('mouseup', this._onMouseUp, this) // Necessary for 0.8 compatibility
.addTo(this._map);

this._map
Expand All @@ -107,6 +107,7 @@ L.Draw.Polyline = L.Draw.Feature.extend({
.on('zoomlevelschange', this._onZoomEnd, this)
.on('touchstart', this._onTouch, this)
.on('zoomend', this._onZoomEnd, this);

}
},

Expand Down Expand Up @@ -174,8 +175,8 @@ L.Draw.Polyline = L.Draw.Feature.extend({
// Add a vertex to the end of the polyline
addVertex: function (latlng) {
var markersLength = this._markers.length;

if (markersLength > 0 && !this.options.allowIntersection && this._poly.newLatLngIntersects(latlng)) {
// markersLength must be greater than or equal to 2 before intersections can occur
if (markersLength >= 2 && !this.options.allowIntersection && this._poly.newLatLngIntersects(latlng)) {
this._showErrorTooltip();
return;
}
Expand Down Expand Up @@ -268,12 +269,17 @@ L.Draw.Polyline = L.Draw.Feature.extend({
},

_onMouseDown: function (e) {
var originalEvent = e.originalEvent;
var clientX = originalEvent.clientX;
var clientY = originalEvent.clientY;
this._startPoint.call(this, clientX, clientY);

if (!this._clickHandled && !this._touchHandled && !this._disableMarkers) {
this._onMouseMove(e);
this._clickHandled = true;
this._disableNewMarkers();
var originalEvent = e.originalEvent;
var clientX = originalEvent.clientX;
var clientY = originalEvent.clientY;
this._startPoint.call(this, clientX, clientY);
}
},

_startPoint: function (clientX, clientY) {
this._mouseDownOrigin = L.point(clientX, clientY);
},
Expand All @@ -283,28 +289,40 @@ L.Draw.Polyline = L.Draw.Feature.extend({
var clientX = originalEvent.clientX;
var clientY = originalEvent.clientY;
this._endPoint.call(this, clientX, clientY, e);
this._clickHandled = null;
},

_endPoint: function (clientX, clientY, e) {
if (this._mouseDownOrigin) {
var distance = L.point(clientX, clientY)
var dragCheckDistance = L.point(clientX, clientY)
.distanceTo(this._mouseDownOrigin);
if (Math.abs(distance) < 9 * (window.devicePixelRatio || 1)) {
var lastPtDistance = this._calculateFinishDistance(e.latlng);
if (lastPtDistance < 10 && L.Browser.touch) {
this._finishShape();
} else if (Math.abs(dragCheckDistance) < 9 * (window.devicePixelRatio || 1)) {
this.addVertex(e.latlng);
}
this._enableNewMarkers(); // after a short pause, enable new markers
}
this._mouseDownOrigin = null;
},

// ontouch prevented by clickHandled flag because some browsers fire both click/touch events,
// causing unwanted behavior
_onTouch: function (e) {
var originalEvent = e.originalEvent;
var clientX;
var clientY;
if (originalEvent.touches && originalEvent.touches[0]) {
if (originalEvent.touches && originalEvent.touches[0] && !this._clickHandled && !this._touchHandled && !this._disableMarkers) {
clientX = originalEvent.touches[0].clientX;
clientY = originalEvent.touches[0].clientY;
this._disableNewMarkers();
this._touchHandled = true;
this._startPoint.call(this, clientX, clientY);
this._endPoint.call(this, clientX, clientY, e);
this._touchHandled = null;
}
this._clickHandled = null;
},

_onMouseOut: function () {
Expand All @@ -313,6 +331,34 @@ L.Draw.Polyline = L.Draw.Feature.extend({
}
},

// calculate if we are currently within close enough distance
// of the closing point (first point for shapes, last point for lines)
// this is semi-ugly code but the only reliable way i found to get the job done
// note: calculating point.distanceTo between mouseDownOrigin and last marker did NOT work
_calculateFinishDistance: function (potentialLatLng) {
var lastPtDistance
if (this._markers.length > 0) {
var finishMarker;
if (this.type === L.Draw.Polyline.TYPE) {
finishMarker = this._markers[this._markers.length - 1];
} else if (this.type === L.Draw.Polygon.TYPE) {
finishMarker = this._markers[0];
} else {
return Infinity;
}
var lastMarkerPoint = this._map.latLngToContainerPoint(finishMarker.getLatLng()),
potentialMarker = new L.Marker(potentialLatLng, {
icon: this.options.icon,
zIndexOffset: this.options.zIndexOffset * 2
});
var potentialMarkerPint = this._map.latLngToContainerPoint(potentialMarker.getLatLng());
lastPtDistance = lastMarkerPoint.distanceTo(potentialMarkerPint);
} else {
lastPtDistance = Infinity;
}
return lastPtDistance;
},

_updateFinishHandler: function () {
var markerCount = this._markers.length;
// The last marker should have a click handler to close the polyline
Expand Down Expand Up @@ -419,7 +465,9 @@ L.Draw.Polyline = L.Draw.Feature.extend({
_getTooltipText: function () {
var showLength = this.options.showLength,
labelText, distanceStr;

if (L.Browser.touch) {
showLength = false; // if there's a better place to put this, feel free to move it
}
if (this._markers.length === 0) {
labelText = {
text: L.drawLocal.draw.handlers.polyline.tooltip.start
Expand Down Expand Up @@ -506,6 +554,19 @@ L.Draw.Polyline = L.Draw.Feature.extend({
}
},

// disable new markers temporarily;
// this is to prevent duplicated touch/click events in some browsers
_disableNewMarkers: function () {
this._disableMarkers = true;
},

// see _disableNewMarkers
_enableNewMarkers: function () {
setTimeout(function() {
this._disableMarkers = false;
}.bind(this), 50);
},

_cleanUpShape: function () {
if (this._markers.length > 1) {
this._markers[this._markers.length - 1].off('click', this._finishShape, this);
Expand Down