How to draw an arrow on every polyline segment on Google Maps V3 -


i looking solution problem on stackoverflow since couldn't find accurate solution ended solving myself , post here, hope help.

google maps provides polyline feature, based on list of coordinates can draw series of lines joining of them.

you can draw polyline single arrow following code:

     var allcoordinates = [         new google.maps.latlng(26.291, 148.027),         new google.maps.latlng(26.291, 150.027),         new google.maps.latlng(22.291, 153.027),         new google.maps.latlng(18.291, 153.027)     ];       var polyline = new google.maps.polyline({             path: allcoordinates,             strokecolor: color,             strokeopacity: 1.0,             strokeweight: 2,             geodesic: true,             icons: [{                 icon: {path: google.maps.symbolpath.forward_closed_arrow},                 offset: '100%'             }]         }); 

the problem here arrow drawn in last segment shown in next picture, route not straightforward , need add arrow on every segment.

the attribute 'repeat' inside icon definition option allows define measure in pixels , definelty won't match every change of direction on polyline.

picture1

so, 1 way found achive make several polylines, 1 per segment allowing in case arrow drawn on each one. code:

     var allcoordinates = [         new google.maps.latlng(26.291, 148.027),         new google.maps.latlng(26.291, 150.027),         new google.maps.latlng(22.291, 153.027),         new google.maps.latlng(18.291, 153.027)     ];      (var = 0, n = allcoordinates.length; < n; i++) {          var coordinates = new array();         (var j = i; j < i+2 && j < n; j++) {             coordinates[j-i] = allcoordinates[j];         }          var polyline = new google.maps.polyline({             path: coordinates,             strokecolor: color,             strokeopacity: 1.0,             strokeweight: 2,             geodesic: true,             icons: [{                 icon: {path: google.maps.symbolpath.forward_closed_arrow},                 offset: '100%'             }]         });         polyline.setmap(map);         polylines.push(polyline);      } 

and picture:

picture2

i hope works looking this!

there repeat property icon options object. example of dashed lines google maps js api shows way repeating symbols on line instead of creating new polylines. in context of example, this:

var allcoordinates = [     new google.maps.latlng(26.291, 148.027),     new google.maps.latlng(26.291, 150.027),     new google.maps.latlng(22.291, 153.027),     new google.maps.latlng(18.291, 153.027) ];  var polyline = new google.maps.polyline({     path: allcoordinates,     strokecolor: color,     strokeopacity: 1.0,     strokeweight: 2,     geodesic: true,     icons: [{         icon: {path: google.maps.symbolpath.forward_closed_arrow},         offset: '100%',         repeat: '20px'     }] }); polyline.setmap(map); polylines.push(polyline); 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -