leaflet routing machine - handle errors

294 views Asked by At

How can I handle not existing route in leaflet routing-machine? In case of error I do not want to return <RoutingMachine />

const Routing = ({ pointA, pointB }) => {

    const dispatch = useDispatch()

    const createLayer = () => {
        const instance = L.Routing.control({
            waypoints: [
                L.latLng(pointA.position.lat, pointA.position.lng),
                L.latLng(pointB.position.lat, pointB.position.lng)
            ],
            lineOptions: {
                styles: [{ color: "#965de9", weight: 5 }]
            },
        });

        instance.on('routesfound', (e) => {
            let routes = e.routes;
            let summary = routes[0].summary;
            let totalTime = moment.utc(1000 * summary.totalDistance).format("H[h] mm[m]")
            console.log('total time: ', totalTime);
            let totalDistance = Math.floor(summary.totalDistance / 1000)
            dispatch(setTime(totalTime))
            dispatch(setDistance(totalDistance))
        });

        return instance;
    };

    const RoutingMachine = createControlComponent(createLayer);

    return <RoutingMachine />
};
0

There are 0 answers