author | sylvain.thenault@logilab.fr |
Mon, 16 Feb 2009 20:55:00 +0100 | |
branch | tls-sprint |
changeset 646 | 8a9551089912 |
parent 553 | c69eac8298c0 |
child 692 | 800592b8d39b |
child 1587 | 56e347b8189c |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
2 |
||
3 |
import simplejson |
|
4 |
||
5 |
from cubicweb.interfaces import IGeocodable |
|
6 |
from cubicweb.common.view import EntityView |
|
431 | 7 |
from cubicweb.common.selectors import implement_interface |
0 | 8 |
|
9 |
class GeocodingJsonView(EntityView): |
|
10 |
id = 'geocoding-json' |
|
11 |
binary = True |
|
12 |
templatable = False |
|
13 |
content_type = 'application/json' |
|
14 |
||
431 | 15 |
__selectors__ = (implement_interface,) |
0 | 16 |
accepts_interfaces = (IGeocodable,) |
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
17 |
|
0 | 18 |
def call(self): |
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
19 |
zoomlevel = self.req.form.pop('zoomlevel', 8) |
0 | 20 |
extraparams = self.req.form.copy() |
21 |
extraparams.pop('vid', None) |
|
22 |
extraparams.pop('rql', None) |
|
23 |
markers = [self.build_marker_data(rowidx, extraparams) |
|
24 |
for rowidx in xrange(len(self.rset))] |
|
25 |
center = { |
|
26 |
'latitude': sum(marker['latitude'] for marker in markers) / len(markers), |
|
27 |
'longitude': sum(marker['longitude'] for marker in markers) / len(markers), |
|
28 |
} |
|
29 |
geodata = { |
|
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
30 |
'zoomlevel': int(zoomlevel), |
0 | 31 |
'center': center, |
32 |
'markers': markers, |
|
33 |
} |
|
34 |
self.w(simplejson.dumps(geodata)) |
|
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
35 |
|
0 | 36 |
def build_marker_data(self, row, extraparams): |
37 |
entity = self.entity(row, 0) |
|
38 |
return {'latitude': entity.latitude, 'longitude': entity.longitude, |
|
39 |
'title': entity.dc_long_title(), |
|
40 |
#icon defines : (icon._url, icon.size, icon.iconAncho', icon.shadow) |
|
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
41 |
'icon': entity.marker_icon() or (self.req.external_resource('GMARKER_ICON'), (20, 34), (4, 34), None), |
0 | 42 |
'bubbleUrl': entity.absolute_url(vid='gmap-bubble', __notemplate=1, **extraparams), |
43 |
} |
|
44 |
||
45 |
||
46 |
class GoogleMapBubbleView(EntityView): |
|
47 |
id = 'gmap-bubble' |
|
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
48 |
|
431 | 49 |
__selectors__ = (implement_interface,) |
0 | 50 |
accepts_interfaces = (IGeocodable,) |
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
51 |
|
0 | 52 |
def cell_call(self, row, col): |
53 |
entity = self.entity(row, col) |
|
54 |
self.w(u'<div>%s</div>' % entity.view('oneline')) |
|
55 |
# FIXME: we should call something like address-view if available |
|
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
56 |
|
0 | 57 |
|
58 |
class GoogleMapsView(EntityView): |
|
59 |
id = 'gmap-view' |
|
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
60 |
|
431 | 61 |
__selectors__ = (implement_interface,) |
0 | 62 |
accepts_interfaces = (IGeocodable,) |
63 |
need_navigation = False |
|
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
64 |
|
0 | 65 |
def call(self, gmap_key, width=400, height=400, uselabel=True, urlparams=None): |
66 |
self.req.add_js('http://maps.google.com/maps?file=api&v=2&key=%s' % gmap_key, |
|
67 |
localfile=False); |
|
68 |
self.req.add_js( ('cubicweb.widgets.js', 'cubicweb.gmap.js', 'gmap.utility.labeledmarker.js') ) |
|
69 |
rql = self.rset.printable_rql() |
|
70 |
if urlparams is None: |
|
71 |
loadurl = self.build_url(rql=rql, vid='geocoding-json') |
|
72 |
else: |
|
73 |
loadurl = self.build_url(rql=rql, vid='geocoding-json', **urlparams) |
|
74 |
self.w(u'<div style="width: %spx; height: %spx;" class="widget gmap" ' |
|
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
75 |
u'cubicweb:wdgtype="GMapWidget" cubicweb:loadtype="auto" ' |
0 | 76 |
u'cubicweb:loadurl="%s" cubicweb:uselabel="%s"> </div>' % (width, height, loadurl, uselabel)) |
77 |
||
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
78 |
|
0 | 79 |
class GoogeMapsLegend(EntityView): |
80 |
id = 'gmap-legend' |
|
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
81 |
|
0 | 82 |
def call(self): |
83 |
self.w(u'<ol>') |
|
84 |
for rowidx in xrange(len(self.rset)): |
|
85 |
self.w(u'<li>') |
|
86 |
self.wview('listitem', self.rset, row=rowidx, col=0) |
|
87 |
self.w(u'</li>') |
|
88 |
self.w(u'</ol>') |