[GIS] OpenLayers snap to road

javascriptopenlayers-2

i have some marker on my map, and i am trying to draw line between the marker but i want it follow the road ,when i used Google map i follow this example…

http://people.missouristate.edu/chadkillingsworth/mapsexamples/snaptoroad.htm

and got this…

if (shiftPressed || path.getLength() === 0) {
          path.push(lineCoordinates[x]);
          if(path.getLength() === 1) {
            poly.setPath(path);
          }
        } else {
          service.route({ origin: path.getAt(path.getLength() - 1), destination: lineCoordinates[x], travelMode: google.maps.DirectionsTravelMode.DRIVING }, function(result, status) {
            if (status == google.maps.DirectionsStatus.OK) {
              for(var i = 0, len = result.routes[0].overview_path.length; i < len; i++) {
                path.push(result.routes[0].overview_path[i]);
              }
            }
          });
        }
      x++;

is there anyway to do the same in OpenLayers?

Best Answer

OpenLayers has a SnappingControl, with an example here.

I guess this is a good starting point, but you will to somehow load your road geometries and set a reasonable snapping threshold.