Determining Midpoint Between 4 different Coordinates

how to find Midpoint Between 4 different latitude or longitude

Hi @sham, I am not sure what you have tried or if you have any code to share but here is something that might work for you. This example below is not for four coordinates but it might help you in the right direction.
import MapKit

 extension CLLocationCoordinate2D {
   func middleLocationWith(location:CLLocationCoordinate2D) -> CLLocationCoordinate2D {

    let lon1 = longitude * M_PI / 180
    let lon2 = location.longitude * M_PI / 180
    let lat1 = latitude * M_PI / 180
    let lat2 = location.latitude * M_PI / 180
    let dLon = lon2 - lon1
    let x = cos(lat2) * cos(dLon)
    let y = cos(lat2) * sin(dLon)

    let lat3 = atan2( sin(lat1) + sin(lat2), sqrt((cos(lat1) + x) * (cos(lat1) + x) + y * y) )
    let lon3 = lon1 + atan2(y, cos(lat1) + x)

    let center:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat3 * 180 / M_PI, lon3 * 180 / M_PI)
    return center
}

}

Best,
Gina

@gdelarosa Excellent solution!

@sham My understanding of your question is that you have 4 points on a map, and you wish to find the midpoint enclosed by those 4 points. If this is correct, then perhaps you may be looking for the intersection point of the diagonal between the two pairs of points.

In other words:

If you have points A, B, C, D which form a shape:

A B

C D

Then you would have two diagonal lines: one extending from point B to C, and another from point A to D. At some point, the two diagonals will intersect, which means you have to find a point that is contained in both lines.

I hope this helps you understand the problem better!

All the best!

This topic was automatically closed after 166 days. New replies are no longer allowed.