building from this answer, you can extend L.Polyline to include any number of variables as options
customPolyline = L.Polyline.extend({
options: {
// default values, you can override these when constructing a new customPolyline
speed: '25',
bearing: '140'
}
});
now create your polylines, and add to the map:
latlngs = [<?php for ($i=0;$i<count($latts);$i++){echo '['. $latts[$i] .', '. $longs[$i];
var polyline = new customPolyline(latlngs,{
color: 'red',
speed: '143',
bearing: '38'
});
polyline.on('click', function() {
alert(this.options.speed)
});
polyline.addTo(map);
See this fiddle for a complete example with polyline
UPDATE
Following up on your comment, you can also attach custom data to L.markers, as shown below and in this fiddle:
customMarker = L.Marker.extend({
options: {
speed: '111',
bearing: '111'
}
});
for (var i = 0, len = latlngs.length; i < len; i++) {
var m = new customMarker(latlngs[i], {
// get the actual speed for this lat-long
// if speeds are in an array in the same exact order as the lat-longs
// it could be as easy as speed: speed[i]
speed: '123'
});
m.on('click', function() {
alert(this.options.speed)
})
m.addTo(map);
}
var polyline = L.polyline(latlngs,{});
polyline.addTo(map);
I don't know how your speed data are associated with each point. But assuming you can get a javascript array or object of lat-longs and speeds, you just need to edit the marker loop, above, to add the speed to each marker when it is constructed.
Best Answer
The below code uses Leaflet.GeometryUtil to check whether the clicked
LatLng
is on a segment and then populatesstart
andend
variables with the respectiveLatLng
s of that segment.