Skip to content

Commit

Permalink
Update leaflet.textpath.js
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkau committed Apr 13, 2015
1 parent eeb0ef0 commit 46d7df2
Showing 1 changed file with 17 additions and 43 deletions.
60 changes: 17 additions & 43 deletions leaflet.textpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,26 @@ var PolylineTextPath = {
textNode.setAttribute('dx', ((pathWidth / 2) - (textWidth / 2)));
}

if (options.flipvertical) {
/* Change label rotation (if required) */
if (options.hasOwnProperty(orientation)) {
var rotateAngle = 0;
switch (options.orientation)
{
case "flip":
rotateAngle = 180;
break;
case "perpendicular"
rotateAngle = 90;
break;
default:
rotateAngle = options.orientation;
}

var rotatecenterX = (textNode.getBBox().x + textNode.getBBox().width / 2);
var rotatecenterY = (textNode.getBBox().y + textNode.getBBox().height / 2);
textNode.setAttribute('transform', 'rotate(180 ' + rotatecenterX + ' ' + rotatecenterY + ')');
textNode.setAttribute('transform', 'rotate(' + rotateAngle + ' ' + rotatecenterX + ' ' + rotatecenterY + ')');
}

/* Initialize mouse events for the additional nodes */
if (this.options.clickable) {
if (L.Browser.svg || !L.Browser.vml) {
Expand Down Expand Up @@ -159,46 +173,6 @@ L.LayerGroup.include({
}
});

/* Functions to compute line direction to correct text orientation
* Note: only generic, in future could check each line segment.
*/

/* Calculate bearing between start and end of line */
function getBearing(Lat1, Lon1, Lat2, Lon2) {
var dLon = Lon2.toRadians() - Lon1.toRadians();
var dPhi = Math.log(Math.tan(Lat2.toRadians() / 2 + Math.PI/4) / Math.tan(Lat1.toRadians()/2 + Math.PI/4));
if (Math.abs(dLon) > Math.PI){
if (dLon > 0.0) {
dLon = -(2.0 * Math.PI - dLon);
} else {
dLon = (2.0 * Math.PI + dLon);
}
}

return (Math.atan2(dLon, dPhi)) + 360).toDegrees() % 360;
}

/* Convert bearing into a generic direction. Lines heading North or East should have their labels flipped */
function correctLabelOrientation (b) {
if (b >= 45 && b =< 135) {
return true; /* North */
} else if (b >= 136 && b =< 225) {
return true; /* East */
} else if (b >= 226 && b =< 315) {
return false; /* South */
} else {
return false; /* West */
}
}

/* Convert Degrees to Radians */
Number.prototype.toRadians = function() {
return (this.valueOf() * Math.PI) / 180;
}

/* Convert Radians to Degrees */
Number.prototype.toDegrees = function() {
return this.valueOf() * (180 * Math.PI);
}

})();

0 comments on commit 46d7df2

Please sign in to comment.