-
Notifications
You must be signed in to change notification settings - Fork 225
Add static label support to Paths #97
base: master
Are you sure you want to change the base?
Conversation
Static labels for paths use the calculated centriod as the label coordinate.
This works fine when you have a polygon that has a good shape. However if you have an unorthodox polygon the center can be the incorrect place for the label E.g. The main reason for not including static labels for polygons/polylines is that the user can zoom the map so that only a piece of the shape is showing. I'll leave this pull open and if people want this functionality they can use your fork. Cheers |
I implemented something similar in dr-itz:polyline-static |
@@ -1,4 +1,20 @@ | |||
L.Path.include({ | |||
showLabel: function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Polygon and Polyline now have a getCenter
method (which does not cover all cases, but works in the common situations).
Plus, I suggest to add a latlng
parameter to showLabel
, with a fallback to getCenter
, so it's easier for users to give the position they want when using a static label on a path, and it still works for basic cases.
@yohanboniface I cleaned up my polyline-static branch based on your suggestions, created a polyline-static-v2 branch and pull request #123 for it. This should make it easier to discuss this feature. |
It would indeed be useful to have static labels on paths. If I where to use it I'd need the label to be shown along the line (with a given offset) and the best spot for it would be in the middle of the visible part of the line. I might well be going to implement something similar with labelled markers paired with polylines. What would be the best way to include that implementation into the plugin? PS: Of course I'll check out @dr-itz 's implementation first |
As @a1an77 says, the best spot would be the middle of the path of the line. This is an example of using the centre of the polyline. The label points to "nowhere". |
@dr-itz I was just trying to get static labels to show up by basically copy&pasting the changes from your polyline-static-v2 branch into a leaflet.label-src.js (I'm not familiar with how to build the dist files from the source and the dist files seem not to be up-to-date in that branch). My code basically does this var wayLine = new L.Polyline(latLngs, {color: col}); But the labels doesn't show up? (I'm on Leaflet 0.7.3) |
Ok, just figured out how to build this with Jake - same effect, no labels appear. |
@arminus My polyline-static-v2 branch only works with Leaflet 1.0 beta or -rc, not 0.7.3... |
For Leaflet 1.0 support, follow Leaflet/Leaflet#3952, and check https://github.com/umap-project/Leaflet.Label in the meantime. |
Static labels for paths use the calculated centriod as the label coordinate.remote
This will fix #36 and does work for polylines etc, as they all descend from path.