I solved this problem by indeed adding a temporary node on the clicked edge and adding 2 temporary edges to this temporary node. By manually joining these temporary node and edges before calling shortest_path_astar they are being used for calculating the correct path but they dont clutter your database with extra records that are only interesting for 1 particular user (at 1 particular moment in time).
Here the SQL query i used (in Python):
"SELECT
row_number() over (range unbounded preceding) as rownumber,
astar.vertex_id,
astar.edge_id,
astar.cost
FROM
shortest_path_astar('SELECT
gid as id,
source::integer as source,
target::integer as target,
length::double precision as cost,
ST_X(ST_Startpoint(the_geom)) as x1,
ST_Y(ST_Startpoint(the_geom)) as y1,
ST_X(ST_Endpoint(the_geom)) as x2,
ST_Y(ST_Endpoint(the_geom)) as y2
FROM ways
%s',
%s,
%s,
false,
false) astar" % (extra_edges, route_origin, route_destination)
where route_origin or route_destination can be the new temporary (negative) id of the virtual node and extra_edges looks like:
" UNION SELECT %d, %s, %d, ww.length * %f, ST_X(ST_Startpoint(ww.the_geom)), ST_Y(ST_Startpoint(ww.the_geom)), ST_X(ST_Line_Interpolate_Point(ww.the_geom, %f)), ST_Y(ST_Line_Interpolate_Point(ww.the_geom, %f)) FROM ways ww WHERE ww.source = %s AND ww.target = %s" % (edgeid, edge_fromnode, nodeid, perc, perc, perc, edge_fromnode, edge_tonode)
+ " UNION SELECT %d, %d, %s, ww.length * (1-%f), ST_X(ST_Line_Interpolate_Point(ww.the_geom, %f)), ST_Y(ST_Line_Interpolate_Point(ww.the_geom, %f)), ST_X(ST_Endpoint(ww.the_geom)), ST_Y(ST_Endpoint(ww.the_geom)) FROM ways ww WHERE ww.source = %s AND ww.target = %s" % (edgeid-1, nodeid, edge_tonode, perc, perc, perc, edge_fromnode, edge_tonode);
Where edgeid is a unique (negative) id for this temp edge, nodeid a unique (negative) id for this virtual node, edge_fromnode the from node of the original edge, edge_tonode the to node of the original edge, perc is the percentage on the original edge (edge_fromnode, edge_tonode) of the new virtual node.
A week ago (9-1-2012) the pgrouting newsletter also touched this subject. See: Pgrouting-users Digest, Vol 40, Issue 2 -> http://lists.osgeo.org/pipermail/pgrouting-users/2012-January/000927.html
Best Answer
I found this link: http://www.sqlexamples.info/SPAT/postgis_nearest_point.htm
based on which, I created the following function:
This function gives me the node id & the distance