Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply css on polylines : leaflet

I am working with the application which uses leaflet api.

Introduction

I needed to draw different types of fences, using decorators i can somewhat apply good visuals to the polylines but not much.

Problem

I was willing to show twisted wires instead of dashes, dots or plain lines and I know the twisted wire line will be an image but can't find help about applying custom css to polylines.

Script Example

         var fence2Icon = L.icon({
                            iconUrl: 'xxxx.png',
                            iconSize: [5, 20]
                            iconAnchor: [5, 18]
                        });

                        // Add coordinate to the polyline
                        var polylineFence2 = new L.Polyline([], { color: 'red' });
                    function fencePlace2(e) {
                        // New marker on coordinate, add it to the map
                new L.Marker(e.latlng, { icon: fence2Icon, draggable: false }).addTo(curr);
                        // Add coordinate to the polyline
               polylineFence2.addLatLng(e.latlng).addTo(curr);
            var decorator = L.polylineDecorator(polylineFence2, {
            patterns:[{offset:5,repeat:'20px',symbol:new L.Symbol.Dash({pixelSize:5})
                         }]
                    }).addTo(curr);
                    }    

                L.easyButton('fa-flash', function () {
                            $('.leaflet-container').css('cursor', 'crosshair');
                            map.on('click', fencePlace2);
                            polylineFence2 = new L.Polyline([], { color: 'red' });
                        }).addTo(map);

If someone know anything about polyline or another way please do help. Thanks for your time:-)

like image 395
Suhail Mumtaz Awan Avatar asked Oct 01 '15 08:10

Suhail Mumtaz Awan


People also ask

How to draw a polyline overlay on a map using Leaflet JavaScript?

Create a polyline using the L.polyline (). To draw the polyline, pass the locations as variable and an option to specify the color of the lines. Add the polyline to the map using the addTo () method of the Polyline class. To draw a polygon overlay on a map using Leaflet JavaScript library, follow the steps given below −

Are there style options for polylines in leaflet?

The Leaflet documentation seems to have been updated since the original answer was provided (v1.03 vs v1.40 at the time of this posting), but even then the documentation on polylines hasn't ever identified any style options outside of the 3 used in the example (that the OP mentions).

How to add a class to a polyline?

You can add a class in the options of your polyline ... and add your own settings in the CSS ... You can also access some options directly ... Show activity on this post. If you create a polyline you're in fact adding an element to the SVG element which Leaflet uses to draw it's overlays.

Is it possible to style a polyline?

The available options for styling L.Polyline s are clearly documented in the Leaflet API reference. Please note that styling options are common with circles, polygons and any other paths, and so appear as "Options inherited from L.Path ". Is it possible to add a border around the line?


2 Answers

You can add a class in the options of your polyline ...

var polyline = L.polyline(latlngs, { className: 'my_polyline' }).addTo(map);

and add your own settings in the CSS ...

.my_polyline { 
  stroke: green;
  fill: none;
  stroke-dasharray: 10,10; 
  stroke-width: 5;  
}

Here is an example: http://jsfiddle.net/FranceImage/9dggfhnc/

You can also access some options directly ...

var polyline = L.polyline(latlngs, { dashArray: '10,10' }).addTo(map);

See Path Options

like image 153
YaFred Avatar answered Oct 12 '22 00:10

YaFred


If you create a polyline you're in fact adding an element to the SVG element which Leaflet uses to draw it's overlays. Styling SVG path elements is different from styling regular HTML elements. There's no such thing as border and background-color etc. It has different properties, if you're interested here's a nice read on the matter:

https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Fills_and_Strokes

You can style Leaflet's path elements when you instanciate them via options or via CSS using the properties (related to styling) described in the documentation here:

http://leafletjs.com/reference.html#path

Via options:

new L.Polyline([...], {
    weight: 3,
    color: 'red',
    opacity: 0.5
}).addTo(map);

Via CSS:

new L.Polyline([...], {
    className: 'polyline'
}).addTo(map);

.polyline {
    weight: 3,
    color: red,
    opacity: 0.5
}

However, what you want, using an image simply isn't possible. You can use images as fill for SVG paths, but it would be impossible for your usecase. You'de need to add a pattern definition to the SVG Leaflet is using and then you could use that id as the fill property as outlined in this answer: https://stackoverflow.com/a/3798797/2019281 but will always fill/tile the image horizontally which won't work if your polyline is vertical.

like image 31
iH8 Avatar answered Oct 12 '22 00:10

iH8