Dynamic Zoom for mapbox

So here is my solution for my use case. Of course this may not work for everyone but I’m going to post it here in case someone finds it useful in their project. Once the dash team implements autozoom into the default behavior of a mapbox, this will be obsolete, and I am sure looking forward to that!

# THIS IS A TEMPORARY SOLUTION UNTIL THE DASH TEAM IMPLEMENTS DYNAMIC ZOOM
def determine_zoom_level(latitudes, longitudes):
    all_pairs=[]
    if not latitudes or not longitudes:
        return 0, (0,0)
    for lon, lat in zip(longitudes, latitudes):
        all_pairs.append((lon,lat))
    b_box = planar.BoundingBox(all_pairs)
    if b_box.is_empty:
        return 0, (0, 0)
    area = b_box.height * b_box.width
    zoom = numpy.interp(area, [0, 5**-10, 4**-10, 3**-10, 2**-10, 1**-10, 1**-5], 
                              [20, 17,   16,     15,     14,     7,      5])
    print(zoom)
    return zoom, b_box.center