View Single Post

Thread: Google Map on pys60

#7 Old 2005-07-31, 10:41

Join Date: Jan 2005
Posts: 148
Location: Bangkok, Thailand
Send a message via MSN to korakotc
korakotc's Avatar
korakotc
Offline
Regular Contributor
Here's a 0.4 version that display satellite map using 'hash' key to
toggle between 'map' and 'sat' mode
Code:
# s60_gmap version 0.4
from appuifw import *
from key_codes import *
from graphics import Image
from os.path import exists
from urllib import urlretrieve
import e32

app.screen = 'full'
app.body = c= Canvas()

fmt_file = u'E:\\system\\data\\%s-%s-%s' # memcard has more space
x, y, z = 10*256, 24*256, 11
mode = 'map'
cache = {'map': {}, 'sat': {} }
ext = {'map':'.png', 'sat':'.jpg'}

def draw():
    gx, ox = divmod(x, 256)
    gy, oy = divmod(y, 256)
    tile(gx, gy, -ox, -oy)  # top-left tile and 3 optional tiles
    if ox > 80:
        tile(gx+1, gy, 256-ox, -oy)
    if oy > 48:
        tile(gx, gy+1, -ox, 256-oy)
    if ox > 80 and oy > 48:
        tile(gx+1, gy+1, 256-ox, 256-oy)

def tile(tx, ty, left, top):
    t = (tx, ty, z)
    rect = (left, top, left+256, top+256)
    # here we use 2 level cache
    if t in cache[mode]:
        im = cache[mode][t]
    else:
        f = fmt_file % t + ext[mode]
        if not exists(f):
            c.rectangle(rect, fill=0xffffff)
            c.text((left+5, top+20), u'Loading')
            urlretrieve(tile_url(t), f)   # url cache
        cache[mode][t] = im = Image.open(f)   # object cache
    c.blit(im, target=rect)

def tile_url(t):
    if mode=='map':
        return 'http://mt.google.com/mt?v=w2.5&x=%s&y=%s&zoom=%s' % t
    quadtree = []  # calculate satellite quadtree url
    m = {(0,0):'q', (0,1):'t', (1,0):'r', (1,1):'s'}
    _x, _y, z = t
    for i in range(17-z):
        _x, rx = divmod(_x, 2)
        _y, ry = divmod(_y, 2)
        quadtree.insert(0, m[(rx,ry)])
    return 'http://kh.google.com/kh?v=3&t=t' + ''.join(quadtree)

def move(dx,dy):
    global x, y
    x += dx * 50
    y += dy * 50
    draw()

def zoomin():
    global x,y,z
    x = x*2 + 88
    y = y*2 + 104
    z = z-1
    draw()

def zoomout():
    global x,y,z
    x = x/2 - 44
    y = y/2 - 52
    z = z+1
    draw()

def toggle_mode():
    global mode
    if mode == 'map':
        mode = 'sat'
    else:
        mode = 'map'
    draw()

c.bind(EKeyRightArrow,lambda:move(1, 0))
c.bind(EKeyLeftArrow,lambda:move(-1, 0))
c.bind(EKeyUpArrow,lambda:move(0, -1))
c.bind(EKeyDownArrow,lambda:move(0, 1))
c.bind(EKeySelect, zoomin)
c.bind(EKeyStar, zoomout)
c.bind(EKeyHash, toggle_mode)

running = 1
def quit():
    global running
    running = 0
app.exit_key_handler= quit

draw()
while running:
    e32.ao_sleep(0.1)
Now the code become exactly 100 lines.
Reply With Quote
 
 
 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2010 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomE2fforumE2fshowpostE2ephpE3fpE3d242953E26postcountE3d4X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfnTypeZCommunityContentQ qdcZtypeQUqfnTypeZE44iscussionContentQ qdcZtypeQUqfnTypeZE44iscussionE45ntryQ qdcZtypeQUqfnTypeZE52esourceQ qdcZtypeQUqfnTypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZpythonQ qfnZtypeQUqfnTypeZCommunityContentQ qfnZtypeQUqfnTypeZE44iscussionContentQ qfnZtypeQUqfnTypeZE44iscussionE45ntryQ qfnZtypeQUqfnTypeZE52esourceQ qfnZtypeQUqfnTypeZWebpageQ qfnZuserE5ftagQSxpythonX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfnTypeZCommunityContentQ qrdfZtypeQUqfnTypeZE44iscussionContentQ qrdfZtypeQUqfnTypeZE44iscussionE45ntryQ qrdfZtypeQUqfnTypeZE52esourceQ qrdfZtypeQUqfnTypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ