Package library :: Package pyjamas :: Package gmaps :: Module Polygon
[hide private]
[frames] | no frames]

Source Code for Module library.pyjamas.gmaps.Polygon

 1  # Copyright (C) 2009 Daniel Carvalho <idnael@gmail.com> 
 2  # 
 3  # Licensed under the Apache License, Version 2.0 (the "License"); 
 4  # you may not use this file except in compliance with the License. 
 5  # You may obtain a copy of the License at 
 6  # 
 7  #     http://www.apache.org/licenses/LICENSE-2.0 
 8  # 
 9  # Unless required by applicable law or agreed to in writing, software 
10  # distributed under the License is distributed on an "AS IS" BASIS, 
11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
12  # See the License for the specific language governing permissions and 
13  # limitations under the License. 
14   
15  from __pyjamas__ import JS 
16  from pyjamas.gmaps.Utils import dictToJs, createListenerMethods, listToJs 
17   
18   
19 -def Polygon(options):
20 JS("return new $wnd['google']['maps']['Polygon'](@{{options}})")
21
22 -def PolygonOptions(adict):
23 """Accepts a dictionary of options. If necessary, transforms "paths" from 24 python list or encoded string to javascript array.""" 25 if adict.has_key("paths"): 26 try: 27 if isinstance(adict["paths"], (list,tuple)): 28 adict["paths"] = listToJs(adict["paths"]) 29 elif isinstance(adict["paths"], basestring): #Gmaps 30 adict["paths"] = decodePoly(adict["paths"]) 31 except: #isinstance throws exception for raw javascript objects. 32 pass #That means it's already good. 33 return dictToJs(adict)
34
35 -def decodePoly(poly):
36 """Quickly decodes a gmaps api v2 encoded polyline... deprecated by google but still 37 a good over-the-wire compression format""" 38 JS(""" 39 var i=-1,j=-1,k,l,q=@{{poly}}['match'](/[\_-\~]*[\?-\^]/g),w=0,x=0,y=0,z=1e-5; 40 41 @{{poly}}=[]; 42 43 if (q) for (;;) 44 { 45 if (!q[++i]) break; 46 for (k=q[i]['length'],l=63,w=0;k--;l=95) w=(w<<5)+q[i]['charCodeAt'](k)-l; 47 y+=(w<<31>>31)^(w>>1); 48 if (!q[++i]) break; 49 for (k=q[i]['length'],l=63,w=0;k--;l=95) w=(w<<5)+q[i]['charCodeAt'](k)-l; 50 x+=(w<<31>>31)^(w>>1); 51 @{{poly}}[++j]=new $wnd['google']['maps']['LatLng'](y*z,x*z); 52 } 53 return @{{poly}} 54 """)
55