Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Korakot Chaovavanich http://korakot.stumbleupon.com

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Google map pin

   1  
   2  def pin(x,y,char=None):
   3      # top-left is x-5, y-17
   4      points = [ (0,0), (-2,-6), (-5,-10), (-5,-14), (-2,-17), \
   5                        (2,-17), (5,-14), (5,-10), (2,-6)]
   6      c.polygon([(x+dx,y+dy) for dx,dy in points], 0, (255,119,107), 1)
   7      if char:
   8          c.text((x-2, y-7), unicode(char))
   9      else:
  10          c.point((x,y-12), 0, width=5)

Instead of adding a pin image to the program, this function
will draw a google map pin at point (x,y) instead.
If you give it a char, it will put that char in the middle of
the pin as well
   1  
   2  pin(20,20,'A') # A in the pin
   3  pin(40,40)  # just bull-eye

Converting between 2 google map tile types

Google maps is a marvelous app.
I try to program a prototype of it on pys60
http://bigbold.com/snippets/posts/show/458

There I only show the default map, not the satellite images.
Retrieving a different mode isn't that difficult.
I read the info from here
http://intepid.com/2005-07-17/21.50/
Then I begin comparing the 2 tile types of the same area
(around California)
http://mt.google.com/mt?v=w2.5&x=20&y=49&zoom=10 (map)
http://kh.google.com/kh?v=3&t=tqtsqrqt (satellite)

Here's the conversion routine between x,y,zoom and quadtree
   1  
   2  def quadtree(x,y, zoom):
   3  	out = []
   4  	m = {(0,0):'q', (0,1):'t', (1,0):'r', (1,1):'s'}
   5  	for i in range(17-zoom):
   6  		x, rx = divmod(x, 2)
   7  		y, ry = divmod(y, 2)
   8  		out.insert(0, m[(rx,ry)])
   9  	return 't' + ''.join(out)

Then to convert back
   1  
   2  def xyzoom(quad):
   3  	x, y, z = 0, 0, 17
   4  	m = {'q':(0,0), 't':(0,1), 'r':(1,0), 's':(1,1)}
   5   	for c in quad[1:]:
   6  		x = x*2 + m[c][0]
   7  		y = y*2 + m[c][1]
   8  		z -= 1
   9  	return x, y, z

Using them is simple
   1  
   2  >>> quadtree(20,49,10)
   3  'tqtsqrqt'
   4  >>> xyzoom('tqtsqrqt')
   5  (20, 49, 10)
   6  >>> sat_url = 'http://kh.google.com/kh?v=3&t=' + quadtree(20,49,10)

Using google map on mobile phone

Download the tile images for testing
   1  
   2  from urllib import urlretrieve
   3  turl =  'http://mt.google.com/mt?v=w2.4&x=%s&y=%s&zoom=%s'
   4  tfile = '%s-%s-%s.gif'
   5  z = 12
   6  for x in range(5,10):
   7    for y in range(10,14):
   8      urlretrieve(turl % (x,y,z), tfile % (z,y,x))

Then put the images in the phone. Then use the following code
to browse around the images (and zoom in/out)
   1  
   2  from appuifw import *
   3  from key_codes import *
   4  from graphics import Image
   5  import e32
   6  
   7  app.screen = 'full'
   8  app.body = c= Canvas()
   9  
  10  x, y, z = 10*256, 24*256, 11
  11  dirname = u'C:\\system\\data\\gmap\\'  # where I save the tile images
  12  
  13  def draw():
  14      gx, ox = divmod(x, 256)
  15      gy, oy = divmod(y, 256)
  16      f = dirname + '%s-%s-%s.gif' % (z,gy,gx)
  17      c.blit(Image.open(f), target=(-ox,-oy))
  18  
  19      if ox > 80:
  20          f = dirname + '%s-%s-%s.gif' % (z,gy,gx+1)
  21          c.blit(Image.open(f), target=(256-ox,-oy))
  22      if oy > 48:
  23          f = dirname + '%s-%s-%s.gif' % (z,gy+1,gx)
  24          c.blit(Image.open(f), target=(-ox,256-oy))
  25      if ox > 80 and oy > 48:
  26          f = dirname + '%s-%s-%s.gif' % (z,gy+1,gx+1)
  27          c.blit(Image.open(f), target=(256-ox,256-oy))
  28  
  29  def move(dx,dy):
  30      global x, y
  31      x += dx * 50
  32      y += dy * 50
  33      draw()
  34  
  35  def zoomin():
  36      global x,y,z
  37      x = x*2 + 88
  38      y = y*2 + 104
  39      z = z-1
  40      draw()
  41  
  42  def zoomout():
  43      global x,y,z
  44      x = x/2 - 44
  45      y = y/2 - 52
  46      z = z+1
  47      draw()
  48  
  49  c.bind(EKeyRightArrow,lambda:move(1, 0))
  50  c.bind(EKeyLeftArrow,lambda:move(-1, 0))
  51  c.bind(EKeyUpArrow,lambda:move(0, -1))
  52  c.bind(EKeyDownArrow,lambda:move(0, 1))
  53  c.bind(EKeySelect, zoomin)
  54  c.bind(EKeyStar, zoomout)
  55  
  56  running = 1
  57  def quit():
  58      global running
  59      running = 0
  60  app.exit_key_handler= quit
  61  
  62  draw()
  63  while running:
  64      e32.ao_sleep(0.1)

I put a bit more explanation in Nokia's forum here. (Newer version is available)
http://discussion.forum.nokia.com/forum/showthread.php?s=&postid=153609
Also see the screenshot here
http://flickr.com/photos/korakot/30189624/
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS