'Initialize the map
@param field: the field
@param fieldname: the field name (to construct HTML IDs)
@param lat: the Latitude of the current point location
@param lon: the Longitude of the current point location
@param wkt: the WKT
@param radius: the radius of the location
@param callback: the script to initialize the widget, if to be
initialized as callback of the MapJS loader
@param geocoder: use a geocoder
@param tablename: tablename to determine the controller/function
for custom colorpicker style
@return: the HTML components for the map (including the map icon row)
@ToDo: handle multiple LocationSelectors in 1 page
(=> multiple callbacks, as well as the need to
migrate options from globals to a parameter)'

def _map(self, field, fieldname, lat, lon, wkt, radius, callback=None, geocoder=False, tablename=None):

    lines = self.lines
    points = self.points
    polygons = self.polygons
    circles = self.circles
    add_points_active = add_polygon_active = add_line_active = add_circle_active = False
    if (points and lines):
        toolbar = True
        if wkt:
            if ((not polygons) or wkt.startswith('LINE')):
                add_line_active = True
            elif polygons:
                add_polygon_active = True
            else:
                add_line_active = True
        else:
            add_points_active = True
    elif (points and polygons):
        toolbar = True
        if wkt:
            add_polygon_active = True
        else:
            add_points_active = True
    elif (points and circles):
        toolbar = True
        if wkt:
            add_circle_active = True
        else:
            add_points_active = True
    elif points:
        toolbar = False
        add_points_active = True
    elif (lines and polygons):
        toolbar = True
        if wkt:
            if wkt.startswith('LINE'):
                add_line_active = True
            else:
                add_polygon_active = True
        else:
            add_polygon_active = True
    elif (lines and circles):
        toolbar = True
        if wkt:
            if wkt.startswith('LINE'):
                add_line_active = True
            else:
                add_circle_active = True
        else:
            add_circle_active = True
    elif lines:
        toolbar = False
        add_line_active = True
    elif (polygons and circles):
        toolbar = True
        if wkt:
            if (radius is not None):
                add_circle_active = True
            else:
                add_polygon_active = True
        else:
            add_polygon_active = True
    elif polygons:
        toolbar = False
        add_polygon_active = True
    elif circles:
        toolbar = False
        add_circle_active = True
    else:
        raise SyntaxError
    s3 = current.response.s3
    color_picker = self.color_picker
    if color_picker:
        toolbar = True
        record_id = s3.record_id
        if (not record_id):
            color_picker = True
        else:
            db = current.db
            s3db = current.s3db
            (c, f) = field.tablename.split('_', 1)
            ftable = s3db.gis_layer_feature
            query = ((((ftable.deleted == False) & (ftable.controller == c)) & (ftable.function == f)) & (ftable.individual == True))
            rows = db(query).select(ftable.layer_id)
            if (not rows):
                color_picker = True
            else:
                layer_id = rows.first().layer_id
                stable = s3db.gis_style
                query = (((stable.deleted == False) & (stable.layer_id == layer_id)) & (stable.record_id == record_id))
                rows = db(query).select(stable.style)
                row = rows.first()
                if row:
                    color_picker = row.style
                else:
                    color_picker = True
    else:
        color_picker = False
    settings = current.deployment_settings
    _map = current.gis.show_map(id=('location_selector_%s' % fieldname), collapsed=True, height=settings.get_gis_map_selector_height(), width=settings.get_gis_map_selector_width(), add_feature=points, add_feature_active=add_points_active, add_line=lines, add_line_active=add_line_active, add_polygon=polygons, add_polygon_active=add_polygon_active, add_circle=circles, add_circle_active=add_circle_active, catalogue_layers=self.catalog_layers, color_picker=color_picker, toolbar=toolbar, clear_layers=False, nav=False, print_control=False, area=False, zoomWheelEnabled=False, callback=callback)
    if (polygons or lines):
        show_map_add = settings.get_ui_label_locationselector_map_polygon_add()
        show_map_view = settings.get_ui_label_locationselector_map_polygon_view()
        if (wkt is not None):
            label = show_map_view
        else:
            label = show_map_add
    else:
        show_map_add = settings.get_ui_label_locationselector_map_point_add()
        show_map_view = settings.get_ui_label_locationselector_map_point_view()
        if ((lat is not None) or (lon is not None)):
            label = show_map_view
        else:
            label = show_map_add
    T = current.T
    global_append = s3.js_global.append
    location_selector_loaded = s3.gis.location_selector_loaded
    if (not location_selector_loaded):
        global_append(('i18n.show_map_add="%s"\ni18n.show_map_view="%s"\ni18n.hide_map="%s"\ni18n.map_feature_required="%s"' % (show_map_add, show_map_view, T('Hide    Map'), T('Map    Input    Required'))))
    icon_id = ('%s_map_icon' % fieldname)
    row_id = ('%s_map_icon__row' % fieldname)
    _formstyle = settings.ui.formstyle
    if ((not _formstyle) or (isinstance(_formstyle, basestring) and ('foundation' in _formstyle))):
        map_icon = DIV(DIV(BUTTON(ICON('globe'), SPAN(label), _type='button', _id=icon_id, _class='btn    tiny    button    gis_loc_select_btn'), _class='small-12    columns'), _id=row_id, _class='form-row    row    hide')
    elif (_formstyle == 'bootstrap'):
        map_icon = DIV(DIV(BUTTON(ICON('icon-map'), SPAN(label), _type='button', _id=icon_id, _class='btn    gis_loc_select_btn'), _class='controls'), _id=row_id, _class='control-group    hide')
    else:
        map_icon = DIV(DIV(BUTTON(ICON('globe'), SPAN(label), _type='button', _id=icon_id, _class='btn    gis_loc_select_btn'), _class='w2p_fl'), _id=row_id, _class='hide')
    if geocoder:
        if (not location_selector_loaded):
            global_append(('i18n.address_mapped="%s"\ni18n.address_not_mapped="%s"\ni18n.location_found="%s"\ni18n.location_not_found="%s"' % (T('Address    Mapped'), T('Address    NOT    Mapped'), T('Address    Found'), T('Address    NOT    Found'))))
        map_icon.append(DIV(DIV(_class='throbber    hide'), DIV(_class='geocode_success    hide'), DIV(_class='geocode_fail    hide'), BUTTON(T('Geocode'), _type='button', _class='hide'), _id=('%s_geocode' % fieldname), _class='controls    geocode'))
    map_icon.append(_map)
    return map_icon
