In recent years many works focusing on improved vehicle fuel efficiency through advanced control have been carried out, reflecting the high interest in ecodriving of vehicles. Although many studies have shown the potential that optimal control based ecodriving can offer, these solution are often difficult to be translated into online control strategies, one of the reasons being the complexity of the optimal control problem and therefore the computational burden. To cope with this a novel online approach, based on switching Nonlinear Model Predictive Control (NMPC), is proposed. The NMPC strategy is developed for the case of conventional vehicles, where gear shifting and longitudinal dynamics are controlled. It is shown that our proposal can operate in real time, while recovering most of the performance achievable by an offline optimal solution. The development of the method is described in detail and its performance is analyzed. The results show that the proposed NMPC can successfully solve the ecodriving task and seems a good compromise between computational burden and performance suitable for field implementation.