Turning this into a function of its own, this should do the trick.
WorldMapButton is a child of
WorldMapFrame that the map and all objects are placed on. TomTom used to use the AstroLab library for real coordinates of the map edges, but they are now provided through
GetCurrentMapZone(). There's some additional complexity to using these if you want to include distances, but as you aren't including that in your design, this is a more simple approach.
Lua Code:
function GetDirection(x,y)
-- Only reset map if not shown
if not WorldMapFrame:IsShown() then SetMapToCurrentZone(); end
local px,py=GetPlayerMapPosition("player");
if px==0 and py==0 then return nil; end-- Return nil if not on map
local w,h=WorldMapButton:GetSize();
-- Converts radians to degrees and normalizes range to -180 thru 180 (Player X and Y are multiplied by 100)
return ((GetPlayerFacing()-math.atan2((px*100-x)*w,(py*100-y)*h))*180/math.pi+180)%360-180;
end