You should be able to solve the first part of the question by yourself using the same approach that you used for the player but now based on the player's vehicle.
To implement the second part you need to know about safe coordinates for vehicle placement that are not inside water and connect that to the first part of the question using the setElementPosition function. Since MTA by default does not provide you with a function to retrieve recommended land positions I suggest you to try taking the traffic path nodes from the MTA community traffic resource. The path node definitions are located in the definitions folder. The most interesting function inside of the resource is the following (traffic_common.lua):
function pathsNodeFindClosest ( x, y, z )
local areaID = getAreaFromPos ( x, y, z )
local minDist, minNode
local nodeX, nodeY, dist
-- for id,node in pairs( AREA_PATHS[areaID] ) do
for id,node in pairs( AREA_PATHS_ALL[areaID].veh ) do
nodeX, nodeY = node.x, node.y
dist = (x - nodeX)*(x - nodeX) + (y - nodeY)*(y - nodeY)
if not minDist or dist < minDist then
minDist = dist
minNode = node
end
end
return minNode
end
It is a pretty icky mathematical solution to your problem because this function does not return a point on the path lines but just the points that are stored in the database as start and end points of lines, but it is better than having nothing! Use this function by passing the vehicle position inside of the water to it and it will return a node object with fields x, y, and z.
You will have to find a ground position based on the point above ground (for ex. the node x, y and z). I suggest you use the getGroundPosition function. A popular candidate for calculating the ground-offset of vehicles is the getElementDistanceFromCentreOfMassToBaseOfModel function. Just add this distance to the ground z coordinate and you should obtain a good vehicle placement position based on the 0, 0, 0 euler rotation.
You will have to slice parts of the resource away and put them into your own because there are no exported functions for the traffic resource.
Have fun!