working code
import console, ui, motion, time
import location
start = time.time()
motion.start_updates()
location.start_updates()
console.set_idle_timer_disabled(True)
def dict_print(dic):
l = 0
for k in dic:
l = max(l,len(k))
for k,v in dic.items():
print(f'{k}:'.ljust(l+1),v)
for _ in range(1):
my_location = location.get_location()
print('location:')
dict_print(my_location)
#address_dict = {'Street': 'Infinite Loop', 'City': 'Cupertino', 'Country': 'USA'}
my_address = {
k:v for k, v in my_location.items()}
# if k=='latitude' or k=='longitude'}
print ('my address1:')
dict_print(my_address)
my_address = location.geocode(my_location)
print ('\nmy address2:')
dict_print(dict(my_address))
results = location.reverse_geocode(my_location)
print ('reverse geocode:')
if results:
dict_print(results[0])
print('authorised:',location.is_authorized())
while False:
a, b, c = motion.get_attitude()
# a tilt left right
# b roll forwards and backwards
# c twist about horizontal axis
print('{: .2f}, {: .2f}, {: .2f},'.format(a,b,c))
time.sleep(.5)
motion.stop_updates()
location.stop_updates()```
# The non working code:
*you will need to create a pyui file that contains three labels:*
['latlong', 'speed', 'address']
``` import console, ui, time
import location, motion
#console.set_idle_timer_disabled(True)
def dict_print(dic):
l = 0
for k in dic:
l = max(l,len(k))
s = ''
for k,v in dic.items():
s += f'{k.ljust(l+1)}: {v}\n'
return s
class myview (ui.View):
pass
def __init__(self, *arg, **args):
start = time.time()
motion.start_updates()
location.start_updates()
def update(self):
if not location.is_authorized():
self['address'].text = 'This app needs access to your location data'
return
my_location = location.get_location()
self['latlong'].text = ', '.join([str(my_location['latitude']), str(my_location['longitude'])])
self['speed'].text = ' / '.join(['{: .2} m/s'.format(my_location['speed']),'{: 0.2} km/h'.format(my_location['speed']*60*60/1000)])
results = location.geocode(my_location)
print(results)
results = location.reverse_geocode(my_location)
print ('reverse geocode:')
s=''
if results:
self['address'].text = dict_print(results[0])
#self['address'].text = str(results)
print(results)
location.start_updates()
motion.start_updates()
v = ui.load_view()
v.update_interval=1
print('authorised:',location.is_authorized())
v.present('sheet')
motion.stop_updates()
location.stop_updates()