Guest User

Greg

a guest
May 20th, 2009
569
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from pyjamas.ui.RootPanel import RootPanelCls
  2. from pyjamas.ui.VerticalPanel import VerticalPanel
  3. from pyjamas.ui.SimplePanel import SimplePanel
  4. import pyjamas.DOM
  5. from pyjamas.JSONService import JSONProxy
  6. from pyjamas import Window
  7.  
  8. ### GOOGLE MAPS WRAPPERS ###
  9. def GMap2(el, options):
  10.     if options:
  11.         JS("""return new $wnd.GMap2(el,options);""")
  12.     else:
  13.         JS("""return new $wnd.GMap2(el);""")
  14.  
  15. def GLatLng(lat,long):
  16.     JS("return new $wnd.GLatLng(lat,long);")
  17.  
  18. def GPolyline(points,color,width):
  19.     JS("return new $wnd.GPolyline(points,color,width);")
  20.  
  21. def jsList():
  22.     JS("return [];")
  23.  
  24. ### JQUERY WRAPPERS/HELPERS ###
  25. def jQuery():
  26.     JS("return $wnd.jQuery;")
  27.  
  28. def jsOpts(options):
  29.     JS("return {};")
  30.  
  31. def getStart(ui):
  32.     JS("return ui.values[0]")
  33. def getEnd(ui):
  34.     JS("return ui.values[1]")
  35.  
  36. class Greed:
  37.     def onModuleLoad(self):
  38.         self.remote = DataService()
  39.         vPanel = VerticalPanel()
  40.  
  41.  
  42.         # create div to hold map
  43.         mapPanel = SimplePanel()
  44.         mapPanel.setSize('700px','400px')
  45.  
  46.         # initiate getting gps data from web2py
  47.         self.remote.getPoints(self)
  48.  
  49.         # create slider div
  50.         slider = SimplePanel()
  51.         self.slider = slider
  52.  
  53.         # add map and slide to main panel
  54.         vPanel.add(mapPanel)
  55.         vPanel.add(slider)
  56.  
  57.         # add everything to page's GreedyPyJs div
  58.         root = RootPanelCls(DOM.getElementById("GreedPyJs"))
  59.         root.add(vPanel)
  60.  
  61.         # Create initial google map
  62.         self.map = GMap2(mapPanel.getElement())
  63.         self.map.setCenter(GLatLng(37.4419, -122.1419), 13)
  64.  
  65.  
  66.         # create place to hold gps positions
  67.         # these will be in tuples: (date, latitude, longitude)
  68.         self.positions=[]
  69.  
  70.     def logger(self, event, ui):
  71.         """
  72.        When user slides slider, this function will be called
  73.        to update the map with the new date range
  74.        """
  75.  
  76.         v1 = getStart(ui)
  77.         v2 = getEnd(ui)
  78.         self.draw(v1,v2)
  79.  
  80.  
  81.     def onRemoteResponse(self, response, request_info):
  82.         if request_info.method == 'getPoints':
  83.             # set the positions variable and draw the slider
  84.             self.positions = response
  85.  
  86.             sliderOpts = jsOpts()
  87.             sliderOpts.range = True
  88.             #sliderOpts.slide = getattr(self, 'logger')
  89.             sliderOpts.min = 0
  90.             sliderOpts.max = len(response) - 1
  91.             sliderOpts.slide = getattr(self,'logger')
  92.  
  93.             jQuery()(self.slider.getElement()).slider(sliderOpts)
  94.  
  95.     def draw(self, start, end):
  96.         # Plot the lines on the google map
  97.         self.map.clearOverlays()
  98.         points = jsList()
  99.         for index in range(start,end):
  100.             # create a list of gps points
  101.             points.push(GLatLng(self.positions[index][1], self.positions[index][2]))
  102.         polyline = GPolyline(points, "#ff0000", 2)
  103.         self.map.addOverlay(polyline)
  104.         self.map.setCenter(GLatLng(self.positions[0][1], self.positions[0][2], 13))
  105.  
  106.     def onRemoteError(self, code, message, request_info):
  107.         pass
  108.  
  109. class DataService(JSONProxy):
  110.     def __init__(self):
  111.         JSONProxy.__init__(self, "/maps/default/call/jsonrpc", ["getPoints"])
  112.  
  113. if __name__ == '__main__':
  114.     app = Greed()
  115.     app.onModuleLoad()
  116.  
  117.  
RAW Paste Data