I am wanting to allow the user to be able to drag a map marker, in OpenLayers 3, but constrain the dragging to the lineString that represents the route. Based on the code example on Stack Exchange I am able to move the marker and have an onChange handler, as follows:
geoMarker.on('change',function() {
var point = scope.getGeometry().getCoordinates();
if ( scope.map.getLayer('routeLayer').getSource().getFeaturesAtCoordinate(point) == 0 ) {
// TODO if point is not in lineString, then prevent drag
} else {
// TODO get index of nearest point in routeLine and display
// location information
}
});
I looked at the OpenLayers 3 examples, but nothing jumped out as how to go about doing this.