Workaround for flyTo bug

* test for flyTo start latlng == end latlng condition
* workaround for flyTo infinite loop
* expect for zoomend values

Close #4226
This commit is contained in:
Zsolt Ero
2016-04-02 21:12:40 +02:00
committed by Per Liedman
parent a4a72ccb17
commit 928100f961
3 changed files with 49 additions and 6 deletions

View File

@ -56,7 +56,7 @@
var map = L.map('map', {
zoomSnap: 0.25
}).setView(dc, 10);
}).setView(dc, 14);
var positron = L.tileLayer('http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, &copy; <a href="http://cartodb.com/attributions">CartoDB</a>'
@ -73,7 +73,7 @@
var nullIslandKitten = L.imageOverlay('http://placekitten.com/300/400?image=6', [[-0.2,-0.15], [0.2, 0.15]]).addTo(map);
document.getElementById('dc').onclick = function () { map.flyTo(dc, 10); };
document.getElementById('dc').onclick = function () { map.flyTo(dc, 4); };
document.getElementById('sf').onclick = function () { map.setView(sf, 10, {duration: 5, animate: true}); };
document.getElementById('trd').onclick = function () { map.flyTo(trd, 10, {duration: 20}); };
document.getElementById('lnd').onclick = function () { map.flyTo(lnd, 9.25); };

View File

@ -609,8 +609,26 @@ describe("Map", function () {
});
describe('#flyTo', function () {
var div;
beforeEach(function () {
div = document.createElement('div');
div.style.width = '800px';
div.style.height = '600px';
div.style.visibility = 'hidden';
document.body.appendChild(div);
map = L.map(div);
});
afterEach(function () {
document.body.removeChild(div);
});
it.skipInPhantom('move to requested center and zoom, and call zoomend once', function (done) {
this.timeout(10000); // This test takes longer than usual due to frames
it('move to requested center and zoom, and call zoomend once', function (done) {
var spy = sinon.spy(),
newCenter = new L.LatLng(10, 11),
newZoom = 12;
@ -622,7 +640,22 @@ describe("Map", function () {
done();
};
map.setView([0, 0], 0);
map.once('zoomend', callback).flyTo(newCenter, newZoom);
map.on('zoomend', callback).flyTo(newCenter, newZoom);
});
it.skipInPhantom('flyTo start latlng == end latlng', function (done) {
this.timeout(10000); // This test takes longer than usual due to frames
var dc = new L.LatLng(38.91, -77.04);
map.setView(dc, 14);
map.on('zoomend', function () {
expect(map.getCenter()).to.eql(dc);
expect(map.getZoom()).to.eql(4);
done();
});
map.flyTo(dc, 4);
});
});

View File

@ -29,8 +29,18 @@ L.Map.include({
rho2 = rho * rho;
function r(i) {
var b = (w1 * w1 - w0 * w0 + (i ? -1 : 1) * rho2 * rho2 * u1 * u1) / (2 * (i ? w1 : w0) * rho2 * u1);
return Math.log(Math.sqrt(b * b + 1) - b);
var s1 = i ? -1 : 1,
s2 = i ? w1 : w0,
t1 = w1 * w1 - w0 * w0 + s1 * rho2 * rho2 * u1 * u1,
b1 = 2 * s2 * rho2 * u1,
b = t1 / b1,
sq = Math.sqrt(b * b + 1) - b;
// workaround for floating point precision bug when sq = 0, log = -Infinite,
// thus triggering an infinite loop in flyTo
var log = sq < 0.000000001 ? -18 : Math.log(sq);
return log;
}
function sinh(n) { return (Math.exp(n) - Math.exp(-n)) / 2; }