diff -r 000000000000 -r b97547f5f1fa web/views/igeocodable.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/views/igeocodable.py Wed Nov 05 15:52:50 2008 +0100 @@ -0,0 +1,86 @@ +# -*- coding: utf-8 -*- + +import simplejson + +from cubicweb.interfaces import IGeocodable +from cubicweb.common.view import EntityView +from cubicweb.common.selectors import interface_selector + +class GeocodingJsonView(EntityView): + id = 'geocoding-json' + binary = True + templatable = False + content_type = 'application/json' + + __selectors__ = (interface_selector,) + accepts_interfaces = (IGeocodable,) + + def call(self): + extraparams = self.req.form.copy() + extraparams.pop('vid', None) + extraparams.pop('rql', None) + markers = [self.build_marker_data(rowidx, extraparams) + for rowidx in xrange(len(self.rset))] + center = { + 'latitude': sum(marker['latitude'] for marker in markers) / len(markers), + 'longitude': sum(marker['longitude'] for marker in markers) / len(markers), + } + geodata = { + 'center': center, + 'markers': markers, + } + self.w(simplejson.dumps(geodata)) + + def build_marker_data(self, row, extraparams): + entity = self.entity(row, 0) + return {'latitude': entity.latitude, 'longitude': entity.longitude, + 'title': entity.dc_long_title(), + #icon defines : (icon._url, icon.size, icon.iconAncho', icon.shadow) + 'icon': entity.marker_icon() or (self.req.external_resource('GMARKER_ICON'), (20, 34), (4, 34), None), + 'bubbleUrl': entity.absolute_url(vid='gmap-bubble', __notemplate=1, **extraparams), + } + + +class GoogleMapBubbleView(EntityView): + id = 'gmap-bubble' + + __selectors__ = (interface_selector,) + accepts_interfaces = (IGeocodable,) + + def cell_call(self, row, col): + entity = self.entity(row, col) + self.w(u'
%s
' % entity.view('oneline')) + # FIXME: we should call something like address-view if available + + +class GoogleMapsView(EntityView): + id = 'gmap-view' + + __selectors__ = (interface_selector,) + accepts_interfaces = (IGeocodable,) + need_navigation = False + + def call(self, gmap_key, width=400, height=400, uselabel=True, urlparams=None): + self.req.add_js('http://maps.google.com/maps?file=api&v=2&key=%s' % gmap_key, + localfile=False); + self.req.add_js( ('cubicweb.widgets.js', 'cubicweb.gmap.js', 'gmap.utility.labeledmarker.js') ) + rql = self.rset.printable_rql() + if urlparams is None: + loadurl = self.build_url(rql=rql, vid='geocoding-json') + else: + loadurl = self.build_url(rql=rql, vid='geocoding-json', **urlparams) + self.w(u'
' % (width, height, loadurl, uselabel)) + + +class GoogeMapsLegend(EntityView): + id = 'gmap-legend' + + def call(self): + self.w(u'
    ') + for rowidx in xrange(len(self.rset)): + self.w(u'
  1. ') + self.wview('listitem', self.rset, row=rowidx, col=0) + self.w(u'
  2. ') + self.w(u'
')