From 9cb3e7d03ad0aed5d999e601396ad51fd668496b Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Thu, 15 Feb 2018 13:40:32 -0500 Subject: [PATCH] Adjust calculation for turn indicator placement This helps move the indicator a bit further away in cases where turn.to.node is right up very close to the turning vertex --- modules/svg/turns.js | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/modules/svg/turns.js b/modules/svg/turns.js index 257d16a94..baae52635 100644 --- a/modules/svg/turns.js +++ b/modules/svg/turns.js @@ -1,4 +1,4 @@ -import { geoAngle, geoVecLength } from '../geo'; +import { geoAngle, geoPathLength } from '../geo'; export function svgTurns(projection) { @@ -63,19 +63,23 @@ export function svgTurns(projection) { groups .attr('transform', function (turn) { - var pxOffset = 50; - var way = graph.entity(turn.to.way); - var t = graph.entity(turn.to.node); - var v = graph.entity(turn.to.vertex); - var a = geoAngle(v, t, projection); - var p = projection(v.loc); - var q = projection(t.loc); - var mid = geoVecLength(p, q) / 2; // midpoint of destination way - var r = turn.u ? 0 - : !way.__via ? pxOffset // leaf way: put marker at pxOffset - : Math.min(mid, pxOffset); // via way: prefer pxOffset, fallback to midpoint + var pxRadius = 50; + var toWay = graph.entity(turn.to.way); + var toPoints = graph.childNodes(toWay) + .map(function (n) { return n.loc; }) + .map(projection); + var toLength = geoPathLength(toPoints); + var mid = toLength / 2; // midpoint of destination way - return 'translate(' + (r * Math.cos(a) + p[0]) + ',' + (r * Math.sin(a) + p[1]) + ') ' + + var toNode = graph.entity(turn.to.node); + var toVertex = graph.entity(turn.to.vertex); + var a = geoAngle(toVertex, toNode, projection); + var o = projection(toVertex.loc); + var r = turn.u ? 0 // u-turn: no radius + : !toWay.__via ? pxRadius // leaf way: put marker at pxRadius + : Math.min(mid, pxRadius); // via way: prefer pxRadius, fallback to mid for very short ways + + return 'translate(' + (r * Math.cos(a) + o[0]) + ',' + (r * Math.sin(a) + o[1]) + ') ' + 'rotate(' + a * 180 / Math.PI + ')'; });