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

Fix problem of narrow angles locateOnLine #79

Merged
merged 2 commits into from
Feb 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions spec/test.locateOnLine.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
describe('Locate on line', function() {
var line = L.polyline([[0,0], [1, 1], [2, 2]]);
var line_narrow_angle = L.polyline([[0,0], [0, 40], [9, 0]])

it('It should return 0 if start', function(done) {
assert.equal(0, L.GeometryUtil.locateOnLine(map, line, L.latLng([0, 0])));
Expand All @@ -17,4 +18,10 @@ describe('Locate on line', function() {
assert.almostEqual(0.85, L.GeometryUtil.locateOnLine(map, line, L.latLng([1.7, 1.7])), 4);
done();
});

it('It should return ratio of point', function(done) {
assert.almostEqual(0.433, L.GeometryUtil.locateOnLine(map, line_narrow_angle, L.latLng([0, 35])), 4);
assert.almostEqual(0.559, L.GeometryUtil.locateOnLine(map, line_narrow_angle, L.latLng([1.5, 35])), 3);
done();
});
});
2 changes: 1 addition & 1 deletion src/leaflet.geometryutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ L.GeometryUtil = L.extend(L.GeometryUtil || {}, {
var l1 = latlngs[i],
l2 = latlngs[i+1];
portion = lengths[i];
if (L.GeometryUtil.belongsSegment(point, l1, l2)) {
if (L.GeometryUtil.belongsSegment(point, l1, l2, 0.0001)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This value seems pretty arbitrary :) Where does it come from?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's arbitrary a little bit like 0.2 :) i can't put 0 because it's not possible to find the point on the line with this value, it should be because the point is on the line but i think that it's not perfectly on it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Gotcha!

portion += l1.distanceTo(point);
found = true;
break;
Expand Down