# How to calculate midpoint given north/south latitude and east/west longitude?

First, sorry if this questions sound very simple. I am very new to GIS.

Given four values of north/south latitude, east/west longitude how to calculate the center point that is correct for all cases on earth?

                  ----------(north latitude)---------------
|                                         |
|                                         |
(west longitude)|                ? (calculate this point) |(east longitude)
|                                         |
|                                         |
----------(south latitude) --------------


The dateline is an issue if you just use the following formula (N+S)/2 and (E+W)/2 to calculate the center.

I think you could use the Midpoint formula found at Moveable Type Scripts.

This is the half-way point along a great circle path between the two points.1 Formula:

Bx = cos(φ2).cos(Δλ)
By = cos(φ2).sin(Δλ)
φm = atan2( sin(φ1) + sin(φ2), √((cos(φ1)+Bx)² + By²) )
λm = λ1 + atan2(By, cos(φ1)+Bx)


JavaScript:

var Bx = Math.cos(lat2) * Math.cos(dLon);
var By = Math.cos(lat2) * Math.sin(dLon);
var lat3 = Math.atan2(Math.sin(lat1)+Math.sin(lat2),
Math.sqrt( (Math.cos(lat1)+Bx)*(Math.cos(lat1)+Bx) + By*By ) );
var lon3 = lon1 + Math.atan2(By, Math.cos(lat1) + Bx);

• +1. Some important details and cautions: (1) "Δλ" is the difference of longitudes, lambda2 - lambda1 (and phi1 and phi2 are latitudes); (2) atan2(y,x) computes an angle whose tangent is y/x (not x/y, as is found in some implementations); (3) in some cases λm must be reduced modulo 360 degrees to lie between -180 and 180 degrees; (4) the formula is correct for the sphere; for an ellipsoid the relative error is good, but the absolute error can be considerable: see gis.stackexchange.com/questions/25494/… for an analysis. – whuber Mar 22 '13 at 15:16

The previous answer recommends using a script from the Movable Type site, but when checking that site more carefully we find that they now provide another solution that is simpler, and that also works near (and at) the Poles and the dateline: www.movable-type.co.uk/scripts/latlong-vectors.html

Their new solution is based on n-vectors (https://en.wikipedia.org/wiki/N-vector), and calculating a midpoint with n-vectors is very simple, as demonstrated in example 7 at http://www.navlab.net/nvector.

The original question asked for a solution that is "correct for all cases on earth", and then the vector-based solution should be used instead of the solution based on lat/long.

In addition: Since the n-vectors are normal to the earth ellipsoid, the n-vector solution will also work for the case of ellipsoidal earth model.