Search code examples
pythonwindows-runtimewindows-11

Python WinRT Geolocation RuntimeError


I am trying to read the geocoordinates from my windows device using the WinRT API with the solution suggested in this post:

import winrt.windows.devices.geolocation as wdg, asyncio

async def getCoords():
    locator = wdg.Geolocator()
    pos = await locator.get_geoposition_async()
    print(pos)
    return [pos.coordinate.latitude, pos.coordinate.longitude]
    
asyncio.run(getCoords())

Unfortunately, I run into the following error...not much to go on:

Traceback (most recent call last):
  File "C:\path\to\locator.py", line 9, in <module>
    asyncio.run(getCoords())
  File "C:\path\to\anaconda3\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\path\to\anaconda3\lib\asyncio\base_events.py", line 642, in run_until_complete
    return future.result()
  File "C:\path\to\locator.py", line 5, in getCoords
    pos = await locator.get_geoposition_async()
RuntimeError

I've also tried the exact solution in the WinRT Documentation without much luck.

This solution worked for me about a year ago. I have since switched from Windows 10 to Windows 11. Is it possible this is a Windows 11 issue?


Solution

  • winrt is no longer maintained, you can use the community-supported winsdk fork.