commit 637b97c79643af5aee46175fc146648e8e972515
parent 49d7b9dffa2285e507762d990ed0ea4ff151decb
Author: Sergej Orlov <wladimirych@gmail.com>
Date: Wed, 31 Oct 2018 08:23:23 +0100
adjust longitudes from mouse events for interactions with lines and markers
Diffstat:
2 files changed, 57 insertions(+), 5 deletions(-)
diff --git a/src/lib/leaflet.fixes/index.js b/src/lib/leaflet.fixes/index.js
@@ -9,6 +9,7 @@ function fixAll() {
fixVectorWorldJump();
fixMarkerWorldJump();
fixViewResetOnWorldJump();
+ fixMarkerDragWorldJump();
}
// https://github.com/Leaflet/Leaflet/issues/3575
@@ -159,5 +160,42 @@ function fixViewResetOnWorldJump() {
});
}
+function wrapLongitudeToTarget(latLng, targetLatLng) {
+ const targetLng = targetLatLng.lng;
+ const lng = latLng.lng;
+ let newLng;
+ if (Math.abs(lng + 360 - targetLng) < Math.abs(lng - targetLng)) {
+ newLng = lng + 360;
+ } else if (Math.abs(lng - 360 - targetLng) < Math.abs(lng - targetLng)) {
+ newLng = lng - 360;
+ } else {
+ return latLng;
+ }
+ return L.latLng(latLng.lat, newLng);
+}
+
+function fixMarkerDragWorldJump() {
+ L.Handler.MarkerDrag.prototype._onDrag = function(e) {
+ var marker = this._marker,
+ shadow = marker._shadow,
+ iconPos = L.DomUtil.getPosition(marker._icon),
+ latlng = marker._map.layerPointToLatLng(iconPos);
+ // update shadow position
+ if (shadow) {
+ L.DomUtil.setPosition(shadow, iconPos);
+ }
+
+ latlng = wrapLongitudeToTarget(latlng, marker._latlng);
+ marker._latlng = latlng;
+ e.latlng = latlng;
+ e.oldLatLng = this._oldLatLng;
+
+ // @event drag: Event
+ // Fired repeatedly while the user drags the marker.
+ marker
+ .fire('move', e)
+ .fire('drag', e);
+ }
+}
-export {fixAll}
+export {fixAll, wrapLongitudeToTarget}
diff --git a/src/lib/leaflet.polyline-edit/index.js b/src/lib/leaflet.polyline-edit/index.js
@@ -1,5 +1,6 @@
import L from 'leaflet';
import './edit_line.css';
+import {wrapLongitudeToTarget} from 'lib/leaflet.fixes';
L.Polyline.EditMixinOptions = {
className: 'leaflet-editable-line'
@@ -85,8 +86,16 @@ L.Polyline.EditMixin = {
onMapClick: function(e) {
if (this._drawingDirection) {
- var newNodeIndex = this._drawingDirection === -1 ? 1 : this.getLatLngs().length - 1;
- this.addNode(newNodeIndex, e.latlng);
+ let newNodeIndex,
+ refNodeIndex;
+ if (this._drawingDirection === -1) {
+ newNodeIndex = 1;
+ refNodeIndex = 0;
+ } else {
+ newNodeIndex = this._latlngs.length - 1;
+ refNodeIndex = this._latlngs.length - 1;
+ }
+ this.addNode(newNodeIndex, wrapLongitudeToTarget(e.latlng, this._latlngs[refNodeIndex]));
} else {
if (!this.preventStopEdit) {
this.stopEdit(true);
@@ -177,7 +186,11 @@ L.Polyline.EditMixin = {
onMouseMoveFollowEndNode: function(e) {
var nodeIndex = this._drawingDirection === -1 ? 0 : this.getLatLngs().length - 1;
- this.spliceLatLngs(nodeIndex, 1, e.latlng);
+ let latlng = e.latlng;
+ if (this._latlngs.length > 0) {
+ latlng = wrapLongitudeToTarget(latlng, this._latlngs[nodeIndex]);
+ }
+ this.spliceLatLngs(nodeIndex, 1, latlng);
this.fire('nodeschanged');
},
@@ -259,7 +272,8 @@ L.Polyline.EditMixin = {
var segmentOverlay = e.target,
latlngs = this.getLatLngs(),
nodeIndex = latlngs.indexOf(segmentOverlay._lineNode) + 1;
- this.addNode(nodeIndex, e.latlng);
+ const midPoint = L.latLngBounds(latlngs[nodeIndex], latlngs[nodeIndex - 1]).getCenter();
+ this.addNode(nodeIndex, wrapLongitudeToTarget(e.latlng, midPoint));
if (L.Draggable._dragging) {
L.Draggable._dragging.finishDrag()
}