commit ac7973c23c3c16c42d3b9e3e184a55215b5c66bf
parent 62fd4f690c96ea81bac3d4685b2e35caee59a033
Author: Sergej Orlov <wladimirych@gmail.com>
Date: Wed, 7 Nov 2018 09:14:50 +0100
properly setView to track bounds when track segment are on different sides of meridian 180 #155
Diffstat:
3 files changed, 39 insertions(+), 20 deletions(-)
diff --git a/src/lib/leaflet.control.track-list/track-list.js b/src/lib/leaflet.control.track-list/track-list.js
@@ -23,6 +23,7 @@ import {notify} from 'lib/notifications';
import {fetch} from 'lib/xhr-promise';
import config from 'config';
import md5 from './lib/md5';
+import {wrapLatLngToTarget, wrapLatLngBoundsToTarget} from 'lib/leaflet.fixes/fixWorldCopyJump';
const TrackSegment = L.MeasuredLine.extend({
includes: L.Polyline.EditMixin,
@@ -365,17 +366,19 @@ L.Control.TrackList = L.Control.extend({
var points = this.getTrackPoints(track);
if (lines.length || points.length) {
var bounds = L.latLngBounds([]);
- lines.forEach(function(l) {
+ lines.forEach((l) => {
if (l.getLatLngs().length > 1) {
- bounds.extend(l.getBounds());
+ bounds.extend(wrapLatLngBoundsToTarget(l.getBounds(), bounds));
}
}
);
- points.forEach(function(p) {
- bounds.extend([p.latlng.lat, p.latlng.lng]);
+ const boundsCenter = bounds.getCenter();
+ points.forEach((p) => {
+ bounds.extend(wrapLatLngToTarget(p.latlng, boundsCenter));
}
);
if (bounds.isValid()) {
+ bounds = wrapLatLngBoundsToTarget(bounds, this.map.getCenter());
if (L.Browser.mobile) {
this.map.fitBounds(bounds);
} else {
diff --git a/src/lib/leaflet.fixes/fixWorldCopyJump.js b/src/lib/leaflet.fixes/fixWorldCopyJump.js
@@ -1,17 +1,33 @@
import L from 'leaflet';
-function wrapLongitudeToTarget(latLng, targetLatLng) {
- const targetLng = targetLatLng.lng;
- const lng = latLng.lng;
- let newLng;
+function shiftLongitudeToTarget(lng, targetLng) {
+ if (targetLng instanceof L.LatLngBounds) {
+ if (!targetLng.isValid()) {
+ return 0;
+ }
+ targetLng = targetLng.getCenter().lng;
+ } else if (targetLng instanceof L.LatLng) {
+ targetLng = targetLng.lng;
+ }
+ let shift = 0;
if (Math.abs(lng + 360 - targetLng) < Math.abs(lng - targetLng)) {
- newLng = lng + 360;
+ shift = 360;
} else if (Math.abs(lng - 360 - targetLng) < Math.abs(lng - targetLng)) {
- newLng = lng - 360;
- } else {
- return latLng;
+ shift= - 360;
}
- return L.latLng(latLng.lat, newLng);
+ return shift;
+}
+
+function wrapLatLngToTarget(latLng, targetLng) {
+ const shift = shiftLongitudeToTarget(latLng.lng, targetLng);
+ return L.latLng(latLng.lat, latLng.lng + shift);
+}
+
+function wrapLatLngBoundsToTarget(latLngBounds, targetLng) {
+ const shift = shiftLongitudeToTarget(latLngBounds.getCenter().lng, targetLng);
+ const p1 = latLngBounds.getSouthEast();
+ const p2 = latLngBounds.getNorthWest();
+ return L.latLngBounds([[p1.lat, p1.lng + shift], [p2.lat, p2.lng + shift]]);
}
function fixVectorMarkerWorldJump() {
@@ -122,7 +138,7 @@ function fixVectorMarkerWorldJump() {
L.DomUtil.setPosition(shadow, iconPos);
}
- latlng = wrapLongitudeToTarget(latlng, marker._latlng);
+ latlng = wrapLatLngToTarget(latlng, marker._latlng);
marker._latlng = latlng;
e.latlng = latlng;
e.oldLatLng = this._oldLatLng;
@@ -135,4 +151,4 @@ function fixVectorMarkerWorldJump() {
}
}
-export {wrapLongitudeToTarget, fixVectorMarkerWorldJump}
-\ No newline at end of file
+export {wrapLatLngToTarget, fixVectorMarkerWorldJump, wrapLatLngBoundsToTarget}
+\ No newline at end of file
diff --git a/src/lib/leaflet.polyline-edit/index.js b/src/lib/leaflet.polyline-edit/index.js
@@ -1,6 +1,6 @@
import L from 'leaflet';
import './edit_line.css';
-import {wrapLongitudeToTarget} from 'lib/leaflet.fixes/fixWorldCopyJump';
+import {wrapLatLngToTarget} from 'lib/leaflet.fixes/fixWorldCopyJump';
L.Polyline.EditMixinOptions = {
className: 'leaflet-editable-line'
@@ -98,7 +98,7 @@ L.Polyline.EditMixin = {
newNodeIndex = this._latlngs.length - 1;
refNodeIndex = this._latlngs.length - 1;
}
- this.addNode(newNodeIndex, wrapLongitudeToTarget(e.latlng, this._latlngs[refNodeIndex]));
+ this.addNode(newNodeIndex, wrapLatLngToTarget(e.latlng, this._latlngs[refNodeIndex]));
} else {
if (!this.preventStopEdit) {
this.stopEdit(true);
@@ -192,7 +192,7 @@ L.Polyline.EditMixin = {
var nodeIndex = this._drawingDirection === -1 ? 0 : this.getLatLngs().length - 1;
let latlng = e.latlng;
if (this._latlngs.length > 0) {
- latlng = wrapLongitudeToTarget(latlng, this._latlngs[nodeIndex]);
+ latlng = wrapLatLngToTarget(latlng, this._latlngs[nodeIndex]);
}
this.spliceLatLngs(nodeIndex, 1, latlng);
this.fire('nodeschanged');
@@ -280,7 +280,7 @@ L.Polyline.EditMixin = {
latlngs = this.getLatLngs(),
nodeIndex = latlngs.indexOf(segmentOverlay._lineNode) + 1;
const midPoint = L.latLngBounds(latlngs[nodeIndex], latlngs[nodeIndex - 1]).getCenter();
- this.addNode(nodeIndex, wrapLongitudeToTarget(e.latlng, midPoint));
+ this.addNode(nodeIndex, wrapLatLngToTarget(e.latlng, midPoint));
if (L.Draggable._dragging) {
L.Draggable._dragging.finishDrag()
}