Get GPS coordinates via terminal or python?



  • What is the (easiest) way to get current location or information about last known location using cli-command or python script? I am trying to write my first app for phones, so help is needed 🙂



  • https://github.com/ernesst/Utrack/

    This should give you the needed hints I think.



  • There is no store for the "last known location" - also location is an asynchronous thing, you have to request it, and then continue doing other stuff, and after some time (shorter or longer depending on GPS coverage) you will receive a callnack with the current location (if location is turned on, and your App is not blocked by the user).

    So its more a thing of responding to the actual location.

    That said, command line is not a good place to start an app with. You can create a simple QML App that requests location. Also remember you need to put location access into the app´s manifest so that the system knows your app is depending on location.



  • Thank you both! This was more like a pre-app planning and testing ideas thing 🙂

    I didn't know if there is a whoami type of command to location. So I checked the Utrack and quickly writed my own whereami-script.

    Here is the code if someone needs it.

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    import subprocess
    
    
    def whereami():
        print('Searching...')
        latitude = ''
        longitude = ''
        for line in get_data():
            if 'latitude' in line.decode("utf-8").lower():
                latitude = line.decode("utf-8")
            if 'longtide' in line.decode("utf-8").lower():
                longitude = line.decode("utf-8") 
            if latitude and longitude:
                print('Location:\n {}\n {}'.format(latitude, longitude))
                break
                
    def get_data():
        cmd = "sudo test_gps"
        gps_data = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
        
        while True:
            line = gps_data.stdout.readline().rstrip()
            if not line:
                break
            yield line
                           
    if __name__=='__main__':
        whereami()
    


  • @Jarsa nice idea! I would like to use this idea as an onlinetracking system for myself. As i do outdoorsports, sending my last location to my own server is a good idea.
    In Android (9) it was very hard because of power limitations. As soon as the screen is off, everything goes to powersaving. And gps eats the battery as the cpu stays always on...
    This could be the limiting problem here too: the battery will be drained like hell...
    Is there a way to reduce power consumption?


Log in to reply