I modified it ever so slightly to this:
Lua Code:
local GetBestMapForUnit = C_Map.GetBestMapForUnit
local MapRects = {}
local mapID
local UnitPosition = UnitPosition
local GetWorldPosFromMapPos = C_Map.GetWorldPosFromMapPos
local CreateVector2D = CreateVector2D
local function GetPlayerMapPosition(mapid, token, rects, _, x, y)
x, y = UnitPosition(token)
if not x then return end
if mapid then
rects = MapRects[mapid]
if not rects then
rects = {}
_, topleft = GetWorldPosFromMapPos(mapid, CreateVector2D(0,0))
_, bottomright = GetWorldPosFromMapPos(mapid, CreateVector2D(1,1))
bottomright:Subtract(topleft)
rects = { topleft.x, topleft.y, bottomright.x, bottomright.y }
MapRects[mapid] = rects
end
x, y = x - rects[1], y - rects[2]
return y / rects[4], x / rects[3]
end
end
local function OnUpdate(self, elapsed, x, y)
self.elapsed = self.elapsed + elapsed
while self.elapsed > 0.5 do --only update the coords 2/second
if mapID then
x, y = GetPlayerMapPosition(mapID, 'player')
if x then
self.text:SetFormattedText('%.1f, %.1f', x * 100, y * 100)
else
self.text:SetText('')
end
end
self.elapsed = self.elapsed - 0.5
end
end
tMinimapCoordsFrame:SetScript('OnEvent', function(self, event, ...)
if event == 'PLAYER_STOPPED_MOVING' then
mapID = nil
tMinimapCoordsFrame:SetScript('OnUpdate', nil)
elseif event == 'PLAYER_STARTED_MOVING' then
mapID = GetBestMapForUnit('player')
tMinimapCoordsFrame:SetScript('OnUpdate', OnUpdate)
elseif event == 'ZONE_CHANGED_NEW_AREA' then
if IsInInstance() then
self:UnregisterEvent('PLAYER_STARTED_MOVING')
self:UnregisterEvent('PLAYER_STOPPED_MOVING')
self:UnregisterEvent('ZONE_CHANGED')
self:UnregisterEvent('ZONE_CHANGED_INDOORS')
else
self:RegisterEvent('PLAYER_STOPPED_MOVING')
self:RegisterEvent('PLAYER_STARTED_MOVING')
self:RegisterEvent('ZONE_CHANGED')
self:RegisterEvent('ZONE_CHANGED_INDOORS')
end
else
mapID = GetBestMapForUnit('player')
OnUpdate(self, 1) --force it to update the coords exactly once as we log in
self:UnregisterEvent('PLAYER_ENTERING_WORLD')
if IsInInstance() then
self:UnregisterEvent('PLAYER_STARTED_MOVING')
self:UnregisterEvent('PLAYER_STOPPED_MOVING')
self:UnregisterEvent('ZONE_CHANGED')
self:UnregisterEvent('ZONE_CHANGED_INDOORS')
end
end
end)
I reworked the OnEvent handler to unregister and register the movement and zone changes if the player is in an instance so it won't be running in there where there are no coordinates.