Skip to content
This repository has been archived by the owner on Mar 28, 2022. It is now read-only.

Add static label support to Paths #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

icedtoast
Copy link

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.

Static labels for paths use the calculated centriod as the
label coordinate.
@jacobtoye
Copy link
Member

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.

polygoncenter

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

@dr-itz
Copy link

dr-itz commented Jul 31, 2015

I implemented something similar in dr-itz:polyline-static
https://github.com/dr-itz/Leaflet.label/tree/polyline-static
It basically follows the polyline and places the label in the middle on the line. Works well enough for Polylines but might place the label at an unexpected position in a Polygon.

@@ -1,4 +1,20 @@
L.Path.include({
showLabel: function () {
Copy link
Member

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.

@dr-itz
Copy link

dr-itz commented Aug 2, 2015

@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.

@a1an77
Copy link

a1an77 commented Nov 25, 2015

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

@jmaister
Copy link

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".

@arminus
Copy link

arminus commented Jun 1, 2016

@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});
wayLine.bindLabel("My Label", { noHide: true });
wayLine.addTo(map).showLabel();

But the labels doesn't show up? (I'm on Leaflet 0.7.3)

@arminus
Copy link

arminus commented Jun 1, 2016

Ok, just figured out how to build this with Jake - same effect, no labels appear.

@dr-itz
Copy link

dr-itz commented Jun 1, 2016

@arminus My polyline-static-v2 branch only works with Leaflet 1.0 beta or -rc, not 0.7.3...

@yohanboniface
Copy link
Member

For Leaflet 1.0 support, follow Leaflet/Leaflet#3952, and check https://github.com/umap-project/Leaflet.Label in the meantime.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

showLabel() does not work with a polygon
7 participants