From 16f9c9a3024c95e78add1ad735adae604d707bbf Mon Sep 17 00:00:00 2001 From: Seb Emonet Date: Fri, 13 Sep 2013 00:22:19 +0200 Subject: [PATCH] Accept simple objects in L.latLngBounds(). Fixes #1915. --- spec/suites/geo/LatLngBoundsSpec.js | 6 +++++- src/geo/LatLng.js | 9 ++++++++- src/geo/LatLngBounds.js | 5 +++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/spec/suites/geo/LatLngBoundsSpec.js b/spec/suites/geo/LatLngBoundsSpec.js index ebfceab3..2e81aad9 100644 --- a/spec/suites/geo/LatLngBoundsSpec.js +++ b/spec/suites/geo/LatLngBoundsSpec.js @@ -27,13 +27,17 @@ describe('LatLngBounds', function() { it('extends the bounds by given bounds', function () { a.extend([[20, 50], [8, 40]]); - expect(a.getSouthEast()).to.eql(new L.LatLng(8, 50)); }); it('extends the bounds by undefined', function () { expect(a.extend()).to.eql(a); }); + + it('extends the bounds by raw object', function () { + a.extend({lat: 20, lng: 50}); + expect(a.getNorthEast()).to.eql(new L.LatLng(30, 50)); + }); }); describe('#getCenter', function () { diff --git a/src/geo/LatLng.js b/src/geo/LatLng.js index 9e2a453c..a7e3336e 100644 --- a/src/geo/LatLng.js +++ b/src/geo/LatLng.js @@ -75,7 +75,11 @@ L.latLng = function (a, b) { // (LatLng) or ([Number, Number]) or (Number, Numbe return a; } if (L.Util.isArray(a)) { - return new L.LatLng(a[0], a[1]); + if (typeof a[0] === 'number' || typeof a[0] === 'string') { + return new L.LatLng(a[0], a[1]); + } else { + return null; + } } if (a === undefined || a === null) { return a; @@ -83,6 +87,9 @@ L.latLng = function (a, b) { // (LatLng) or ([Number, Number]) or (Number, Numbe if (typeof a === 'object' && 'lat' in a) { return new L.LatLng(a.lat, 'lng' in a ? a.lng : a.lon); } + if (b === undefined) { + return null; + } return new L.LatLng(a, b); }; diff --git a/src/geo/LatLngBounds.js b/src/geo/LatLngBounds.js index 166af803..6fda3a88 100644 --- a/src/geo/LatLngBounds.js +++ b/src/geo/LatLngBounds.js @@ -17,8 +17,9 @@ L.LatLngBounds.prototype = { extend: function (obj) { // (LatLng) or (LatLngBounds) if (!obj) { return this; } - if (typeof obj[0] === 'number' || typeof obj[0] === 'string' || obj instanceof L.LatLng) { - obj = L.latLng(obj); + var latLng = L.latLng(obj); + if (latLng !== null) { + obj = latLng; } else { obj = L.latLngBounds(obj); }