<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def fix_location(records, max_elapsed_seconds=300):
 """ Update position of all records based on the position of the closest GPS record. .. note:: Use this function when call and text records are missing a location, but you have access to accurate GPS traces. """

    groups = itertools.groupby(records, lambda r: r.direction)
    groups = [(interaction, list(g)) for interaction, g in groups]

    def tdist(t1, t2):
            return abs((t1 - t2).total_seconds())

    for i, (interaction, g) in enumerate(groups):
        if interaction == 'in':
            continue

        prev_gps = groups[i-1][1][-1]
        next_gps = groups[i+1][1][0]

        for r in g:
            if tdist(r.datetime, prev_gps.datetime) <= max_elapsed_seconds:
                r.position = prev_gps.position
            elif tdist(r.datetime, next_gps.datetime) <= max_elapsed_seconds:
                r.position = next_gps.position