Skip to content

Instantly share code, notes, and snippets.

@ItachiSan
Created March 13, 2024 15:54
Show Gist options
  • Save ItachiSan/c7e9d470ed1ba53673cdc1b48155d8b7 to your computer and use it in GitHub Desktop.
Save ItachiSan/c7e9d470ed1ba53673cdc1b48155d8b7 to your computer and use it in GitHub Desktop.
Python script fetching the current position
import sys
import time
from PySide6.QtCore import QCoreApplication, QObject, QTimer
from PySide6.QtPositioning import QGeoPositionInfoSource, QGeoPositionInfo
class PositionPoller(QObject):
def __init__(self, parent: QObject):
super().__init__(parent)
source = QGeoPositionInfoSource.createDefaultSource(self)
source.startUpdates()
print("-- Latest location:")
last_known_location = source.lastKnownPosition()
self.positionUpdated(last_known_location)
print("-- Manal update:")
source.requestUpdate(10)
time.sleep(10)
last_known_location = source.lastKnownPosition()
self.positionUpdated(last_known_location)
print("-- Background location:")
# C++ call:
#self.connect(source, source.positionUpdated, self.positionUpdated)
source.positionUpdated.connect(self.positionUpdated)
@staticmethod
def positionUpdated(info: QGeoPositionInfo):
timestamp_string = info.timestamp().toString()
coordinate_string = info.coordinate().toString()
print(f"[{timestamp_string}] {coordinate_string}")
def main():
app = QCoreApplication()
_location_updater = PositionPoller(app)
# Shutdown after a while
def shutdown():
print("Closing after 30 seconds")
app.exit()
QTimer.singleShot(30_000, shutdown)
sys.exit(app.exec())
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment