Is it safe to say that your box will not contain a North or South poles (i.e., +/- 90 degrees lat)? I'll go with that for now. Certainly, it should be safe to assume that the poles are not interior to the box. (Possibly a pole could be on the horizontal edge of the box, but then we have to discuss the single point vs entire line caveat.)

Provided that both lats are in bounds (-90..+90), for the lat, you can simply use:

centerLat = (lat2 + lat1) / 2; (a rewrite of your function)

Even if the sum of the two lats is greater than 90, dividing by 2 brings it into bounds. So, for example, if two lats are 50 and 70, then 120 (is out of bounds), but 120/2 is 60 degrees latitude which is in bounds and is midway between the two lats. Keep in mind that in a mercator mapping, the north pole is represented by the top horizontal line; i.e. all the points on that top line represent a single point (north pole) on the sphere; so, as you get close to the north or south pole, the map will appear very distorted.

The long requires a little more explanation...