Skip to content
This repository has been archived by the owner on Mar 28, 2022. It is now read-only.

Commit

Permalink
Support centering the position of a label
Browse files Browse the repository at this point in the history
  • Loading branch information
crrobinson14 committed Dec 16, 2013
1 parent ec317c7 commit 721a063
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Label.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,24 @@ L.Label = L.Class.extend({
labelWidth = this._labelWidth,
offset = L.point(this.options.offset);

// position to the right (right or auto & needs to)
if (direction === 'right' || direction === 'auto' && labelPoint.x < centerPoint.x) {
// Left, Center, Right positioning. 'Auto' chooses left or right as
// necessary to fit the label on the map.
if (direction === 'center') {
L.DomUtil.addClass(container, 'leaflet-label-center');
L.DomUtil.removeClass(container, 'leaflet-label-left');
L.DomUtil.removeClass(container, 'leaflet-label-right');

pos = pos.add(L.point(-offset.x - (labelWidth / 2), offset.y));
} else if (direction === 'right' || direction === 'auto' && labelPoint.x < centerPoint.x) {
L.DomUtil.addClass(container, 'leaflet-label-right');
L.DomUtil.removeClass(container, 'leaflet-label-left');
L.DomUtil.removeClass(container, 'leaflet-label-center');

pos = pos.add(offset);
} else { // position to the left
L.DomUtil.addClass(container, 'leaflet-label-left');
L.DomUtil.removeClass(container, 'leaflet-label-right');
L.DomUtil.removeClass(container, 'leaflet-label-center');

pos = pos.add(L.point(-offset.x - labelWidth, offset.y));
}
Expand Down

1 comment on commit 721a063

@nag5000
Copy link

@nag5000 nag5000 commented on 721a063 Mar 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pos.add(L.point(-offset.x - (labelWidth / 2), offset.y)).round()
otherwise fractional transformation leads to vague labels (if labelWidth/2 fractional).

Please sign in to comment.