Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better geodesy handling #2345

Merged
merged 16 commits into from
Jan 3, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
approximate Circle with ellipse
  • Loading branch information
mourner committed Jan 2, 2014
commit 7ff0fa9c7296f111903980219e53bc21f6441989
9 changes: 5 additions & 4 deletions spec/suites/layer/vector/CircleSpec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
describe('Circle', function () {
describe('#getBounds', function () {

var circle;
var map, circle;

beforeEach(function () {
circle = L.circle([50, 30], 200);
map = L.map(document.createElement('div')).setView([0, 0], 4);
circle = L.circle([50, 30], 200).addTo(map);
});

it('returns bounds', function () {
var bounds = circle.getBounds();

expect(bounds.getSouthWest().equals([49.998203369, 29.997204939])).to.be.ok();
expect(bounds.getNorthEast().equals([50.001796631, 30.002795061])).to.be.ok();
expect(bounds.getSouthWest()).nearLatLng(new L.LatLng(49.95122, 29.88281));
expect(bounds.getNorthEast()).nearLatLng(new L.LatLng(50.06419, 30.05859));
});
});
});
20 changes: 13 additions & 7 deletions src/layer/vector/Circle.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ L.Circle = L.CircleMarker.extend({
},

getBounds: function () {
var half = [this._radius, this._radius];
var half = [this._radius, this._radiusY];

return new L.LatLngBounds(
this._map.layerPointToLatLng(this._point.subtract(half)),
Expand All @@ -31,14 +31,20 @@ L.Circle = L.CircleMarker.extend({
setStyle: L.Path.prototype.setStyle,

_project: function () {
var crs = this._map.options.crs,
p1 = crs.project(this._latlng),
latlng2 = crs.unproject(p1.subtract([this._mRadius, 0]));

var pointLeft = this._map.latLngToLayerPoint(latlng2);
var lng = this._latlng.lng,
map = this._map,

this._point = this._map.latLngToLayerPoint(this._latlng);
this._radius = Math.max(this._point.x - pointLeft.x, 1);
latR = 360 * this._mRadius / (2 * L.CRS.Earth.R * Math.PI),
top = map.latLngToLayerPoint([this._latlng.lat + latR, lng]),
bottom = map.latLngToLayerPoint([this._latlng.lat - latR, lng]),
p = this._point = top.add(bottom).divideBy(2),
newLat = map.layerPointToLatLng(p).lat,
lngR = latR / Math.cos(newLat * Math.PI / 180),
left = map.latLngToLayerPoint([newLat, lng - lngR]);

this._radius = Math.max(p.x - left.x, 1);
this._radiusY = Math.max(p.y - top.y, 1);

this._updateBounds();
}
Expand Down