Thanks to your script example I found a strange anomaly, dgelessus!
It took my quite a while to figure this out and it is due to similar strange behavior I've seen wehen working in a PHP based project, where I found out that the contents of variables seemed to be updated in a different fashion when FIRST printing the values of these variables to stdout and THEN access the values of these variables to print them elsewhere - eg on a screen
In your code example at http://pastebin.com/ZAWiaGGm you've used this construction:
location.start_updates()
self.loc = location.get_location()
print(self.loc)
location.stop_updates()
I've modified your code so the draw() method displays the value of the self.loc variable on the canvas as well as in the interpreter. As you've noticed, the interpreter's values are correctly updated. The values within the scene are not.
After some experimentation I've noticed the "print(self.loc))" line in your code and I got this "PHP problem hunch". I've added the same print() line in my Scenes based script and lo and behold! The location values are now correctly updated within the Scene's draw loop!
With your original script with the three buttons you can see the same behavior. If you comment out the 'print(self.loc)' line, you'll notice the values printed on the screen canvas are not updated. As soon as you uncomment the print() line, the values are correctly updated.
I've added my modifications here:
import console
import location
from scene import *
running = True
testaddr = {'Street': 'Infinite Loop', 'City': 'Cupertino', 'Country': 'USA'}
testcoords = (())
def geocode():
testcoords = location.geocode(testaddr)
class MainScene (Scene):
def init(self):
self.btn = 0
self.loc = ''
def setup(self):
# This will be called before the first frame is drawn.
pass
def draw(self):
# This will be called for every frame (typically 60 times per second).
background(0, 0, 0)
# draw wannabe buttons
fill(0, 0, 1)
rect(0, 0, 200, 200)
# add continuous polling of location. Not so economic, but only for this testing case
location.start_updates()
self.loc = location.get_location()
# make sure to add this print statement: comment this line to see the update of the self.loc value fail.
print(self.loc)
location.stop_updates()
# if we've successfully obtained our location, display it in the scene in all it's floating number glory
if self.loc:
mylocation = '%.9f, %.9f, %d' % (self.loc['longitude'], self.loc['latitude'], self.loc['timestamp'])
stroke(1,1,1)
text(mylocation, 'Helvetica', 20, 100, 10, 6)
# Draw a red circle for every finger that touches the screen:
fill(1, 0, 0)
for touch in self.touches.values():
ellipse(touch.location.x - 50, touch.location.y - 50, 100, 100)
def touch_began(self, touch):
# determines touched "button"
if touch.location.x < 200:
if touch.location.y < 200:
self.btn = 1
elif touch.location.y < 400:
self.btn = 2
elif touch.location.y < 600:
self.btn = 3
def touch_moved(self, touch):
pass
def touch_ended(self, touch):
stroke(1,1,1)
# runs the tapped button's corresponding function
if self.btn == 1:
text('button 1', 'Helvetica', 20, 100, 200, 6)
self.dumploc()
elif self.btn == 2:
text('button 2', 'Helvetica', 20, 100, 300, 6)
self.dumpaddr()
elif self.btn == 3:
text('button 3', 'Helvetica', 20, 100, 400, 6)
self.geocode()
self.btn = 0
def dumploc(self):
location.start_updates()
self.loc = location.get_location()
print(self.loc)
location.stop_updates()
def dumpaddr(self):
self.addr = location.reverse_geocode(self.loc)
print(self.addr)
def geocode(self):
geocode()
print(testcoords)
run(MainScene())
Though this perhaps doesn't solve the reverse_geocode issue, it does solve continuous update issue I've ran into.
Not sure I understand why this happens, but I am happy that I found a workaround for the continuous location update issue. If I find a gap in my schedule I will dive into the reverse_geocode issue.
Thanks for your input!