'Generate a Map widget
@param r: the S3Request instance
@param widget: the widget as a tuple: (label, type, icon)
@param attr: controller attributes for the request'

def _map(self, r, widget, widgets, **attr):

    T = current.T
    db = current.db
    s3db = current.s3db
    widget_get = widget.get
    label = widget_get('label', '')
    if (label and isinstance(label, basestring)):
        label = T(label)
    icon = widget_get('icon', '')
    if icon:
        icon = ICON(icon)
    _class = self._lookup_class(r, widget)
    context = widget_get('context', None)
    tablename = r.tablename
    (resource, context) = self._resolve_context(r, tablename, context)
    if context:
        cserialize_url = context.serialize_url
    else:
        cserialize_url = (lambda res: {})
    height = widget_get('height', 383)
    width = widget_get('width', 568)
    bbox = widget_get('bbox', {})
    ftable = s3db.gis_layer_feature
    mtable = s3db.gis_marker
    feature_resources = []
    fappend = feature_resources.append
    s3dbresource = s3db.resource
    for widget in widgets:
        if (widget['type'] not in ('datalist', 'datatable', 'report')):
            continue
        show_on_map = widget_get('show_on_map', True)
        if (not show_on_map):
            continue
        tablename = widget['tablename']
        list_id = ('profile-list-%s-%s' % (tablename, widget['index']))
        layer = dict(name=T(widget['label']), id=list_id, active=True)
        filter = widget_get('filter', None)
        marker = widget_get('marker', None)
        if marker:
            marker = db((mtable.name == marker)).select(mtable.image, mtable.height, mtable.width, limitby=(0, 1)).first()
        layer_id = None
        layer_name = widget_get('layer', None)
        if layer_name:
            row = db((ftable.name == layer_name)).select(ftable.layer_id, limitby=(0, 1)).first()
            if row:
                layer_id = row.layer_id
        if layer_id:
            layer['layer_id'] = layer_id
            resource = s3dbresource(tablename)
            filter_url = ''
            first = True
            if context:
                filters = cserialize_url(resource)
                for f in filters:
                    sep = ('' if first else '&')
                    filter_url = ('%s%s%s=%s' % (filter_url, sep, f, filters[f]))
                    first = False
            if filter:
                filters = filter.serialize_url(resource)
                for f in filters:
                    sep = ('' if first else '&')
                    filter_url = ('%s%s%s=%s' % (filter_url, sep, f, filters[f]))
                    first = False
            if filter_url:
                layer['filter'] = filter_url
        else:
            layer['tablename'] = tablename
            map_url = widget.get('map_url', None)
            if (not map_url):
                (c, f) = tablename.split('_', 1)
                map_url = URL(c=c, f=f, extension='geojson')
                resource = s3dbresource(tablename)
                first = True
                if context:
                    filters = cserialize_url(resource)
                    for f in filters:
                        sep = ('?' if first else '&')
                        map_url = ('%s%s%s=%s' % (map_url, sep, f, filters[f]))
                        first = False
                if filter:
                    filters = filter.serialize_url(resource)
                    for f in filters:
                        sep = ('?' if first else '&')
                        map_url = ('%s%s%s=%s' % (map_url, sep, f, filters[f]))
                        first = False
            layer['url'] = map_url
        if marker:
            layer['marker'] = marker
        fappend(layer)
    profile_layers = s3db.get_config(r.tablename, 'profile_layers')
    if profile_layers:
        for layer in profile_layers:
            fappend(layer)
    lat = widget_get('lat', None)
    lon = widget_get('lon', None)
    map = current.gis.show_map(height=height, lat=lat, lon=lon, width=width, bbox=bbox, collapsed=True, feature_resources=feature_resources)
    fullscreen = A(ICON('fullscreen'), _href=URL(c='gis', f='map_viewing_client'), _class='gis_fullscreen_map-btn', _title=T('View    full    screen'))
    s3 = current.response.s3
    if s3.debug:
        script = ('/%s/static/scripts/S3/s3.gis.fullscreen.js' % current.request.application)
    else:
        script = ('/%s/static/scripts/S3/s3.gis.fullscreen.min.js' % current.request.application)
    s3.scripts.append(script)
    output = DIV(fullscreen, H4(icon, label, _class='profile-sub-header'), DIV(map, _class='card-holder'), _class=_class)
    return output
