Get GPS info in QGIS using Python

Warning: sizeof(): Parameter must be an array or an object that implements Countable in /home/ca0z7qw4wks9/public_html/ on line 205

As a future improvement for my QGIS Plugin Transectizer, I want to be able to get GPS information (coordinates and bearing, for instance) from QGIS’s GPS widget. I read in a post in stackexchange an answer from a QGIS developer where the main directions are given, but I miss some information about how to really get the info through python console in QGIS, so here’s the recipe. Just open the Python console in QGIS and try the following to get GPS info in QGIS using Python:

The trick here is to know that you have to call the instance() method:

To get the GPSConnectionRegistry for your running application. Bear in mind that you must have a GPS connection stablished. Soon I’ll write a post about a tiny application which makes possible to simulate GPS for testing purposes.