Forum Archive

Extract only latitude and longitude from location

secmast

With this simple script I can get the complete location information from the unit GPS.

import location, time

a=""
label1 = ""

def getlocation(sender):
location.start_updates()
time.sleep(1)
label1 = str(location.get_location())
location.stop_updates()

getlocation(a)
print(a)

And the result is the following
{'latitude': 49.305450439453125, 'longitude': 2.5930605239319453, 'altitude': 28.290002822875977, 'timestamp': 1542108670.452639, 'horizontal_accuracy': 140.45985579622024, 'vertical_accuracy': 10.941082954406738, 'speed': -1.0, 'course': -1.0}

But, and I’m sure this is simple but my knowledge of python and programming in general is really poor.
How can I get only latitude and longitude information out of a ?
More generally how can I convert something that seems to be a mix of string and number. As the A variable have quotes and special character like ā€œ:ā€.

cvp

@secmast try this
```
import location, time

a=""
label1 = ""

def getlocation():
location.start_updates()
time.sleep(1)
label1 = location.get_location() # returns a dictionnary
location.stop_updates()
return label1

a = getlocation()
print(a)

get two elements from dictinnary

lat = a['latitude']
lon = a['longitude']
print(lat,lon)```

secmast

Thanks...
In fact writing the post I discover that I just need to understand a bit better the Variable type under Python.
In this case this is a dictionary.
Now I need to read a bit about dict type...
But thanks...