From cca3180f30488aac08b50eda5f7620326050386b Mon Sep 17 00:00:00 2001 From: Vladimir Agafonkin Date: Mon, 15 Oct 2012 11:39:07 +0300 Subject: [PATCH] fix Circle getBounds, closes #1068 --- spec/runner.html | 1 + spec/suites/layer/vector/CircleSpec.js | 17 +++++++++++++++++ src/layer/vector/Circle.js | 23 ++++++++++++----------- 3 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 spec/suites/layer/vector/CircleSpec.js diff --git a/spec/runner.html b/spec/runner.html index 41f7c9ff..110f7c7c 100644 --- a/spec/runner.html +++ b/spec/runner.html @@ -47,6 +47,7 @@ + diff --git a/spec/suites/layer/vector/CircleSpec.js b/spec/suites/layer/vector/CircleSpec.js new file mode 100644 index 00000000..09d17a4d --- /dev/null +++ b/spec/suites/layer/vector/CircleSpec.js @@ -0,0 +1,17 @@ +describe('Circle', function () { + describe('#getBounds', function () { + + var circle; + + beforeEach(function () { + circle = L.circle([50, 30], 200); + }); + + it('should return correct bounds', function () { + var bounds = circle.getBounds(); + + expect(bounds.getSouthWest().equals([49.998203369, 29.997204939])).toBeTruthy(); + expect(bounds.getNorthEast().equals([50.001796631, 30.002795061])).toBeTruthy(); + }); + }); +}); diff --git a/src/layer/vector/Circle.js b/src/layer/vector/Circle.js index 2a84281c..19eb5956 100644 --- a/src/layer/vector/Circle.js +++ b/src/layer/vector/Circle.js @@ -34,13 +34,11 @@ L.Circle = L.Path.extend({ }, getBounds: function () { - var map = this._map, - delta = this._radius * Math.cos(Math.PI / 4), - point = map.project(this._latlng), - swPoint = new L.Point(point.x - delta, point.y + delta), - nePoint = new L.Point(point.x + delta, point.y - delta), - sw = map.unproject(swPoint), - ne = map.unproject(nePoint); + var lngRadius = this._getLngRadius(), + latRadius = (this._mRadius / 40075017) * 360, + latlng = this._latlng, + sw = new L.LatLng(latlng.lat - latRadius, latlng.lng - lngRadius), + ne = new L.LatLng(latlng.lat + latRadius, latlng.lng + lngRadius); return new L.LatLngBounds(sw, ne); }, @@ -72,11 +70,14 @@ L.Circle = L.Path.extend({ return this._mRadius; }, - _getLngRadius: function () { - var equatorLength = 40075017, - hLength = equatorLength * Math.cos(L.LatLng.DEG_TO_RAD * this._latlng.lat); + // TODO Earth hardcoded, move into projection code! - return (this._mRadius / hLength) * 360; + _getLatRadius: function () { + return (this._mRadius / 40075017) * 360; + }, + + _getLngRadius: function () { + return this._getLatRadius() / Math.cos(L.LatLng.DEG_TO_RAD * this._latlng.lat); }, _checkIfEmpty: function () {