Skip to content

Commit

Permalink
Switching map types preserves location.
Browse files Browse the repository at this point in the history
  • Loading branch information
simondlevy committed Jun 28, 2015
1 parent e0bd8e6 commit 3b9fc26
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ GooMPy.egg-info
build
dist
mapscache
*.swp

16 changes: 8 additions & 8 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@

from goompy import GooMPy

LATITUDE = 37.7913838
LONGITUDE = -79.44398934

WIDTH = 800
HEIGHT = 500

LATITUDE = 37.7913838
LONGITUDE = -79.44398934
ZOOM = 15
MAPTYPE = 'roadmap'

class UI(Tk):

Expand Down Expand Up @@ -62,9 +63,8 @@ def __init__(self):

maptype_index = 0
self.radiovar.set(maptype_index)
self.maptype = self.maptypes[maptype_index]

self.zoomlevel = 15
self.goompy = GooMPy(WIDTH, HEIGHT, LATITUDE, LONGITUDE, ZOOM, MAPTYPE)

self.restart()

Expand All @@ -82,7 +82,6 @@ def zoom(self, sign):

def reload(self):

self.goompy = GooMPy(WIDTH, HEIGHT, LATITUDE, LONGITUDE, self.zoomlevel, self.maptype)
self.coords = None
self.redraw()

Expand All @@ -98,7 +97,8 @@ def restart(self):
def add_radio_button(self, text, index):

maptype = self.maptypes[index]
Radiobutton(self.radiogroup, text=maptype, variable=self.radiovar, value=index, command=lambda:self.usemap(maptype)).grid(row=0, column=index)
Radiobutton(self.radiogroup, text=maptype, variable=self.radiovar, value=index,
command=lambda:self.usemap(maptype)).grid(row=0, column=index)

def click(self, event):

Expand Down Expand Up @@ -129,7 +129,7 @@ def redraw(self):

def usemap(self, maptype):

self.maptype = maptype
self.goompy.useMaptype(maptype)
self.restart()

def check_quit(self, event):
Expand Down
1 change: 1 addition & 0 deletions goompy/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.pyc
*.swp
46 changes: 32 additions & 14 deletions goompy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,36 +140,24 @@ def __init__(self, width, height, latitude, longitude, zoom, maptype, radius_met

self.zoom = zoom
self.maptype = maptype
self.radius_meters = radius_meters

self.winimage = _new_image(self.width, self.height)

self.bigimage, self.northwest, self.southeast = fetchTiles(latitude, longitude, zoom, maptype, radius_meters)
self._fetch()

self.leftx = self._center(self.width)
self.uppery = self._center(self.height)

self._update()

def _center(self, dim):

return (self.bigimage.size[0] - dim) / 2

def _update(self):

self.winimage.paste(self.bigimage, (-self.leftx, -self.uppery))

def getImage(self):
'''
Returns the current image as a PIL.Image object.
'''

return self.winimage

def _constrain(self, oldval, diff, dimsize):

newval = oldval + diff
return newval if newval > 0 and newval < self.bigimage.size[0]-dimsize else oldval

def move(self, dx, dy):
'''
Moves the view by the specified pixels dx, dy.
Expand All @@ -179,3 +167,33 @@ def move(self, dx, dy):
self.uppery = self._constrain(self.uppery, dy, self.height)
self._update()

def useMaptype(self, maptype):

self.maptype = maptype

self._fetch()

self._update()

def useZoom(self, zoom):

None

def _fetch(self):

self.bigimage, self.northwest, self.southeast = fetchTiles(self.lat, self.lon, self.zoom, self.maptype, self.radius_meters)

def _center(self, dim):

return (self.bigimage.size[0] - dim) / 2

def _update(self):

self.winimage.paste(self.bigimage, (-self.leftx, -self.uppery))

def _constrain(self, oldval, diff, dimsize):

newval = oldval + diff
return newval if newval > 0 and newval < self.bigimage.size[0]-dimsize else oldval


0 comments on commit 3b9fc26

Please sign in to comment.