'Get the current location of the instance(s) (at the given time)
@param timestmp: last datetime for presence (defaults to current time)
@param _fields: fields to retrieve from the location records (None for ALL)
@param _filter: filter for the locations
@param as_rows: return the result as Rows object
@param exclude: interlocks to break at (avoids circular check-ins)
@param empty: return None if no locations (set to False by gis.get_location_data())
@return: a location record, or a list of location records (if multiple)
@ToDo: Also show Timestamp of when seen there'

def get_location(self, timestmp=None, _fields=None, _filter=None, as_rows=False, exclude=[], empty=True):

    db = current.db
    s3db = current.s3db
    ptable = s3db[PRESENCE]
    ltable = s3db[LOCATION]
    if (timestmp is None):
        timestmp = datetime.utcnow()
    locations = []
    for r in self.records:
        location = None
        if (TRACK_ID in r):
            query = (((ptable.deleted == False) & (ptable[TRACK_ID] == r[TRACK_ID])) & (ptable.timestmp <= timestmp))
            presence = db(query).select(orderby=(~ ptable.timestmp), limitby=(0, 1)).first()
            if presence:
                if presence.interlock:
                    exclude = ([r[TRACK_ID]] + exclude)
                    (tablename, record_id) = presence.interlock.split(',', 1)
                    trackable = S3Trackable(tablename=tablename, record_id=record_id)
                    record = trackable.records.first()
                    if ((TRACK_ID not in record) or (record[TRACK_ID] not in exclude)):
                        location = trackable.get_location(timestmp=timestmp, exclude=exclude, _fields=_fields, as_rows=True).first()
                elif presence.location_id:
                    query = (ltable.id == presence.location_id)
                    if (_filter is not None):
                        query = (query & _filter)
                    if (_fields is None):
                        location = db(query).select(ltable.ALL, limitby=(0, 1)).first()
                    else:
                        location = db(query).select(limitby=(0, 1), *_fields).first()
        if (not location):
            if (len(self.records) > 1):
                trackable = S3Trackable(record=r, rtable=self.rtable)
            else:
                trackable = self
            location = trackable.get_base_location(_fields=_fields)
        if location:
            locations.append(location)
        elif (not empty):
            locations.append(Row({'lat': None, 'lon': None}))
    if as_rows:
        return Rows(records=locations, compact=False)
    if (not locations):
        return None
    else:
        return locations
