I have 2 coordinates, top left and bottom right. I would like to find the city center. Right now I have the following way to calculate it. The central point is leaving. When I call a method with
[self.map setRegionTopLeft: CLLocationCoordinate2DMake(21.57524, -157.984514) bottomRight: CLLocationCoordinate2DMake(21.309766, -157.80766) animated:YES];
He should focus on Oahu in Hawaii, USA. I found this math here , so I'm not sure what is going on.
Code A is the way out. It doesn't put me anywhere near the island.
- (CLLocationCoordinate2D)centerPointFromRegionTopLeft:(CLLocationCoordinate2D)topLeft bottomRight:(CLLocationCoordinate2D)bottomRight { CLLocationCoordinate2D centerPoint; centerPoint.longitude = (topLeft.longitude + bottomRight.longitude) / 2; if (fabs(bottomRight.longitude - topLeft.longitude) > 180) { if (centerPoint.longitude > 0) { centerPoint.longitude = centerPoint.longitude + 180; } else { centerPoint.longitude = centerPoint.longitude - 180; } } centerPoint.latitude = asin((sin(bottomRight.latitude) + sin(topLeft.latitude))/2); return centerPoint; }
I also originally tried this method. This is just what appeared in my head when I thought about the center of the rectangle. If I'm much closer to what the center should be, I can see the island, but it's still not there.
Code B - The source code I tried. This is much closer to what I expected, but still not able to.
- (CLLocationCoordinate2D)centerPointFromRegionTopLeft:(CLLocationCoordinate2D)topLeft bottomRight:(CLLocationCoordinate2D)bottomRight { CLLocationCoordinate2D centerPoint; centerPoint.latitude = ((topLeft.latitude + bottomRight.latitude) / 2); centerPoint.longitude = ((topLeft.longitude + bottomRight.longitude) / 2); return centerPoint; }
So, the coordinate area (topLeft, bottomRight) is set, how to get the center coordinate? The idea is that I have to give any 2 coordinates and get the center coordinate.
Update * Code B works. I had the wrong topLeft and bottomRight. Code A puts me south and a little east where it should be.