Mike D - 3 months ago 23

Objective-C Question

I am trying to determine the midpoint between to locations in an

`MKMapView`

My method based on the java method linked above:

`+(CLLocationCoordinate2D)findCenterPoint:(CLLocationCoordinate2D)_lo1 :(CLLocationCoordinate2D)_loc2 {`

CLLocationCoordinate2D center;

double lon1 = _lo1.longitude * M_PI / 180;

double lon2 = _loc2.longitude * M_PI / 100;

double lat1 = _lo1.latitude * M_PI / 180;

double lat2 = _loc2.latitude * M_PI / 100;

double dLon = lon2 - lon1;

double x = cos(lat2) * cos(dLon);

double y = cos(lat2) * sin(dLon);

double lat3 = atan2( sin(lat1) + sin(lat2), sqrt((cos(lat1) + x) * (cos(lat1) + x) + y * y) );

double lon3 = lon1 + atan2(y, cos(lat1) + x);

center.latitude = lat3 * 180 / M_PI;

center.longitude = lon3 * 180 / M_PI;

return center;

}

The 2 parameters have the following data:

`_loc1:`

latitude = 45.4959839

longitude = -73.67826455

_loc2:

latitude = 45.482889

longitude = -73.57522299

The above are correctly place on the map (in and around Montreal). I am trying to center the map in the midpoint between the 2, yet my method return the following:

`latitude = 65.29055`

longitude = -82.55425

which somewhere in the arctic, when it should be around 500 miles south.

Answer

Just a hunch, but I noticed your lon2 and lat2 variables are being computed with M_PI/100 and not M_PI/180.

```
double lon1 = _lo1.longitude * M_PI / 180;
double lon2 = _loc2.longitude * M_PI / 100;
double lat1 = _lo1.latitude * M_PI / 180;
double lat2 = _loc2.latitude * M_PI / 100;
```

Changing those to 180 might help you out a bit.