2

I want calculate the center point between my location and some annotation. So far I have done this:

CLLocation *myLoc = self.locMgr.location;

        MKPointAnnotation *middleAnnotation = [locationV.annotations objectAtIndex:locationV.annotations.count/2];

        CLLocation *someStuiodLoc = [[CLLocation alloc] initWithLatitude:middleAnnotation.coordinate.latitude longitude:middleAnnotation.coordinate.longitude];

        CLLocationDistance dist = [myLoc distanceFromLocation:someStuiodLoc];

How can I calculate the center point/cordinate of "dist" ??

Lewis Gordon
  • 1,711
  • 14
  • 19
nr5
  • 4,228
  • 8
  • 42
  • 82

3 Answers3

7
#define ToRadian(x) ((x) * M_PI/180)
#define ToDegrees(x) ((x) * 180/M_PI)

+ (CLLocationCoordinate2D)midpointBetweenCoordinate:(CLLocationCoordinate2D)c1 andCoordinate:(CLLocationCoordinate2D)c2 
{ 
          c1.latitude = ToRadian(c1.latitude); 
          c2.latitude = ToRadian(c2.latitude);
          CLLocationDegrees dLon = ToRadian(c2.longitude - c1.longitude); 
          CLLocationDegrees bx = cos(c2.latitude) * cos(dLon); 
          CLLocationDegrees by = cos(c2.latitude) * sin(dLon);
          CLLocationDegrees latitude = atan2(sin(c1.latitude) + sin(c2.latitude), sqrt((cos(c1.latitude) + bx) * (cos(c1.latitude) + bx) + by*by));
          CLLocationDegrees longitude = ToRadian(c1.longitude) + atan2(by, cos(c1.latitude) + bx);

           CLLocationCoordinate2D midpointCoordinate; 
           midpointCoordinate.longitude = ToDegrees(longitude); 
           midpointCoordinate.latitude = ToDegrees(latitude);

           return midpointCoordinate;
}
Bala
  • 2,895
  • 1
  • 19
  • 60
  • -1 just code no answer.. Please see naini's hint about the earth being a sphere – Daij-Djan May 31 '13 at 08:15
  • @Daij-Djan Oh really sorry for Just Code and no Answer .. Actually i just implemented this code from here http://stackoverflow.com/a/4167772/1059705 and thought it may help. – Bala May 31 '13 at 11:45
  • http://stackoverflow.com/a/4656937/1059705 even here they did the same.. @Daij-Djan – Bala May 31 '13 at 12:02
  • ;) a -1 is nothing personal and nothin not changeable. I just wanted to express that I think it is a bad idea to ONLY post code :) – Daij-Djan May 31 '13 at 12:08
  • Okay Fine :) .. Have a great day . Happy Coding @Daij-Djan – Bala Jun 01 '13 at 08:57
  • If you are going to do copy paste and then modify the code at least do it correctly man. Better if you refer to the links since the beginning so people can check the source. You didn't change this line CLLocationDegrees longitude = TORAD(c1.longitude) + atan2(by, cos(c1.latitude) + bx); – artud2000 Nov 18 '13 at 19:19
4

I have written library function in Swift to calculate the midpoint between multiple coordinates as following:

//        /** Degrees to Radian **/

class func degreeToRadian(angle:CLLocationDegrees) -> CGFloat{

    return (  (CGFloat(angle)) / 180.0 * CGFloat(M_PI)  )

}

//        /** Radians to Degrees **/

class func radianToDegree(radian:CGFloat) -> CLLocationDegrees{

    return CLLocationDegrees(  radian * CGFloat(180.0 / M_PI)  )

}

class func middlePointOfListMarkers(listCoords: [CLLocationCoordinate2D]) -> CLLocationCoordinate2D{

    var x = 0.0 as CGFloat

    var y = 0.0 as CGFloat

    var z = 0.0 as CGFloat



    for coordinate in listCoords{

        var lat:CGFloat = degreeToRadian(coordinate.latitude)

        var lon:CGFloat = degreeToRadian(coordinate.longitude)

        x = x + cos(lat) * cos(lon)

        y = y + cos(lat) * sin(lon);

        z = z + sin(lat);

    }

    x = x/CGFloat(listCoords.count)

    y = y/CGFloat(listCoords.count)

    z = z/CGFloat(listCoords.count)



    var resultLon: CGFloat = atan2(y, x)

    var resultHyp: CGFloat = sqrt(x*x+y*y)

    var resultLat:CGFloat = atan2(z, resultHyp)



    var newLat = radianToDegree(resultLat)

    var newLon = radianToDegree(resultLon)

    var result:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: newLat, longitude: newLon)

    return result

}

Detailed answer can be found here

thomasdao
  • 972
  • 12
  • 26
2

U can calculate the midPoint of two coordinates using the mid point formula (http://www.purplemath.com/modules/midpoint.htm) which gives a close approximation to the actual geographical point if the distances are lesser than 500 miles. If you consider that the earth is spherical, then a more complex treatment of the points would be involved.

everconfusedGuy
  • 2,709
  • 27
  • 43