6

I need to allow user to draw a route (using polyline) with specified road radius (visually it was done with "weight" parameter).

Visually it looks like that:

enter image description here

So I'm wondering how can build a polygon around this polyline with some offset? Like this:

enter image description here

2 Answers2

3

Finally I made it with JSTS library (https://www.npmjs.com/package/jsts).

It's easy:

//pathCoords should be an array of jsts.geom.Coordinate
var pathCoords = [];
var geometryFactory = new jsts.geom.GeometryFactory();

// on what distance new polygon should be built
var distance = (meters * 0.0001) / 111.12;
var shell = geometryFactory.createLineString(pathCoords);

// building a new polygon
var polygon = shell.buffer(distance);

// finally get your new polygon coordinates
var polygonCoords = polygon.getCoordinates();
  • I tried JSTS library on my web application and it's working perfectly, I need to do the same on Android but I couldn't find a similar library for Android or Java – AymanKun Jun 17 '16 at 01:50
2

Like you said, you can use the getLatLngs method of L.Polyline to access the coordinates and use them to initialize a L.Polygon. If you need to access the weight that's been set on the polyline you can do so by using it's options object:

var polyline = new L.Polyline([[25, -25], [25, 25], [-25, 25], [-25, -25]], {
    weight: 10,
}).addTo(map);

var polygon = new L.Polygon(polyline.getLatLngs(), {
    weight: polyline.options.weight
}).addTo(map);

You could even use the entire options object if you've got more things you'll need to duplicate:

var polygon = new L.Polygon(polyline.getLatLngs(), polyline.options).addTo(map);

Since L.Polygon is extended from L.Polyline that won't be a problem since it has the same options.

iH8
  • 27,722
  • 4
  • 67
  • 76
  • 2
    Maybe I wasn't clear about question, I don't need to apply a polyline's style to polygon, I need to build one around polyline. I attached a new picture for better understanding. – Ruslan Nigmatulin Mar 22 '16 at 12:55