I load markers from the database and then draw a polyline between the markers. I use a polyline to calculate the total distance instead of calculating the distance from marker-a to marker-b to marker-c, etc.
My distance, however, is inaccurate, because if there are two curves around a curved road, the polyline simply connects them, and does not draw along the road.
I know that this is possible in the Google Maps API, but the restrictions on use do not suit me, so I decided to use the booklet.
My tokens are not so far apart because my GPS device sends a location every 10 seconds.
I found the leaflet-routing-machine plugin and I was wondering if I can use this to make my polyline road-bound?
This is how I add markers to my map:
function getlocationsfromdb(){
group.clearLayers();
latlngArray.length=0;
var deviceid = $("#selectid").val();
$.ajax({
type: "POST",
url: "functionhandlers/getlocations.php",
data: {deviceid:deviceid,start:start,end:end},
dataType: 'json',
cache: false,
})
.success(function(response) {
$('input').removeClass('error').next('.errormessage').html('');
if(!response.errors && response.result) {
$.each(response.result, function( index, value) {
var latlng = L.latLng(value[7], value[8]);
var marker = L.circleMarker(latlng,{radius:2}).addTo(group);
latlngArray.push(latlng);
});
var polyline = L.polyline(latlngArray, {color: '#605ca8'}).addTo(group);
map.fitBounds(group.getBounds());
var distancetravelled=polyline.measuredDistance();
$("#distancetravelled").html(distancetravelled);
} else {
$.each(response.errors, function( index, value) {
$('input[name*='+index+']').addClass('error').after('<div class="errormessage">'+value+'</div>')
});
}
});
}
Can anyone point me in the right direction?
user5563910
source
share