Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to draw a polygon around a polyline in JavaScript?
    primarykey
    data
    text
    <p>I want to draw a polygon around a polyline. The polyline in my case is a Google Maps direction and I need to show a polygon around it within the Google Maps canvas.</p> <p><strong>First:</strong></p> <p>For offsetting I use the JavaScript Clipper Library. I have the following polyline (route): I make an offset polygon below using Clipper:</p> <p>I have a working <a href="http://jsbin.com/oVIcowo/1" rel="noreferrer">JS Bin example</a>.</p> <p>The code is:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Javascript Clipper Library / Offset polyline&lt;/title&gt; &lt;script src="clipper.js"&gt;&lt;/script&gt; &lt;script&gt; function draw() { var polygons = [[{"X":72,"Y":59.45},{"X":136,"Y":66},{"X":170,"Y":99},{"X":171,"Y":114},{"X":183,"Y":125},{"X":218,"Y":144},{"X":218,"Y":165},{"X":226,"Y":193},{"X":254,"Y":195},{"X":283,"Y":195},{"X":292,"Y":202},{"X":325,"Y":213},{"X":341,"Y":234},{"X":397,"Y":245},{"X":417,"Y":248}]]; var scale = 100; reverse_copy(polygons); polygons = scaleup(polygons, scale); var cpr = new ClipperLib.Clipper(); var delta = 25; var joinType = ClipperLib.JoinType.jtRound; var miterLimit = 2; var AutoFix = true; var svg, offsetted_polygon, cont = document.getElementById('svgcontainer'); offsetted_polygon = cpr.OffsetPolygons(polygons, delta * scale, joinType, miterLimit, AutoFix); //console.log(JSON.stringify(offsetted_polygon)); // Draw red offset polygon svg = '&lt;svg style="margin-top:10px;margin-right:10px;margin-bottom:10px;background-color:#dddddd" width="540" height="340"&gt;'; svg += '&lt;path stroke="red" fill="red" stroke-width="2" stroke-opacity="0.6" fill-opacity="0.2" d="' + polys2path(offsetted_polygon, scale) + '"/&gt;'; //Draw blue polyline svg += '&lt;path stroke="blue" stroke-width="3" d="' + polys2path(polygons, scale) + '"/&gt;'; svg += '&lt;/svg&gt;'; cont.innerHTML += svg; } // helper function to scale up polygon coordinates function scaleup(poly, scale) { var i, j; if (!scale) scale = 1; for(i = 0; i &lt; poly.length; i++) { for(j = 0; j &lt; poly[i].length; j++) { poly[i][j].X *= scale; poly[i][j].Y *= scale; } } return poly; } // converts polygons to SVG path string function polys2path (poly, scale) { var path = "", i, j; if (!scale) scale = 1; for(i = 0; i &lt; poly.length; i++) { for(j = 0; j &lt; poly[i].length; j++) { if (!j) path += "M"; else path += "L"; path += (poly[i][j].X / scale) + ", " + (poly[i][j].Y / scale); } path += "Z"; } return path; } function reverse_copy(poly) { // Make reverse copy of polygons = convert polyline to a 'flat' polygon ... var k, klen = poly.length, len, j; for (k = 0; k &lt; klen; k++) { len = poly[k].length; poly[k].length = len * 2 - 2; for (j = 1; j &lt;= len - 2; j++) { poly[k][len - 1 + j] = { X: poly[k][len - 1 - j].X, Y: poly[k][len - 1 - j].Y } } } } &lt;/script&gt; &lt;/head&gt; &lt;body onload="draw()"&gt; &lt;h2&gt;Javascript Clipper Library / Offset polyline&lt;/h2&gt; This page shows an example of offsetting polyline and drawing it using SVG. &lt;div id="svgcontainer"&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>And all this is good but now I must replace the polygon variables with points from Google Maps directions, so I do this change:</p> <pre><code>directionsService.route(request, function(response, status) { if (status == google.maps.DirectionsStatus.OK) { directionsDisplay.setDirections(response); function draw() { var polygons = response.routes[0].overview_path; //REST OF CODE } } } </code></pre> <p>I have a <a href="http://jsbin.com/uTATePe/9" rel="noreferrer">JS Bin example</a> with this code for offsetting the polygon around the polyline.</p> <p>But there is some problem, which I can't regonize and I can't get a polygon around directions.</p> <p>Is there any way to solve this problem?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload