leaflet-streets: Improve orientation detection for street names

This commit is contained in:
Markus Koch 2020-04-19 22:00:35 +02:00
parent be65b093a1
commit ba466fe474

View File

@ -126,8 +126,7 @@ L.StreetLabels = L.LabelTextCollision
var stopCoords = layer.getLatLngs()[layer.getLatLngs().length - 1]; var stopCoords = layer.getLatLngs()[layer.getLatLngs().length - 1];
//Flip lineString if bearing is negative //Flip lineString if bearing is negative
if (this._getBearing(startCoords, stopCoords) < 0) flip = (this._getBearing(startCoords, stopCoords) < 0);
layer = this._getLineStringReverse(layer);
if (layer._parts) { if (layer._parts) {
ctx.textAlign = "center"; ctx.textAlign = "center";
@ -137,7 +136,7 @@ L.StreetLabels = L.LabelTextCollision
//Build the points list for the first part //Build the points list for the first part
var pathPoints = []; var pathPoints = [];
for (var i = 0; i < part.length; i++) { for (var i = 0; i < part.length; i++) {
var linePart = part[i]; var linePart = (flip ? part[part.length - 1 - i] : part[i]);
pathPoints.push(linePart.x); pathPoints.push(linePart.x);
pathPoints.push(linePart.y); pathPoints.push(linePart.y);
} }
@ -158,16 +157,20 @@ L.StreetLabels = L.LabelTextCollision
Source: https://makinacorpus.github.io/Leaflet.GeometryUtil/leaflet.geometryutil.js.html Source: https://makinacorpus.github.io/Leaflet.GeometryUtil/leaflet.geometryutil.js.html
*/ */
_getBearing: function (startCoords, stopCoords) { _getBearing: function (startCoords, stopCoords) {
var rad = Math.PI / 180, if (startCoords.lat == stopCoords.lat) {
lat1 = startCoords.lat * rad, if (startCoords.lng > stopCoords.lng)
lat2 = stopCoords.lat * rad, return -180;
lon1 = startCoords.lng * rad, else
lon2 = stopCoords.lng * rad, return 180;
y = Math.sin(lon2 - lon1) * Math.cos(lat2), }
x = Math.cos(lat1) * Math.sin(lat2) - if (startCoords.lng == stopCoords.lng) {
Math.sin(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1); if (startCoords.lat > stopCoords.lat)
var bearing = ((Math.atan2(y, x) * 180 / Math.PI) + 360) % 360; return -90;
return bearing >= 180 ? bearing - 360 : bearing; else
return 90;
}
var angle = Math.atan(stopCoords.lng - startCoords.lng, stopCoords.lat - startCoords.lat) * 180 / Math.Pi;
return angle;
}, },
/** /**