The central coordinate of the coordinate area

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.

+4
source share
1 answer

You need the middle of L (longitude) and B (latitude). For B, the problem is around the pole, but as you install it, you simply cannot โ€œput the cap on the poleโ€, so there is no problem here.

 Middle(B1,B2)=(B1+B2)/2. 

But L is much worse. L can jump from -179 to -179. And one more problem: in the middle (-179, + 179) there should be 180, and the average (-1, + 1) should be 0. That is, we should always choose the middle one along a shorter path between opposite points, and not around all over the earth.

We must transfer the zero meridian so that the difference between L1, L2 is less than 180, make them a normal middle, and then return the zero meridian.

  • Let L1
  • if L2-L1> 180, choose L2 for the new zero meridian.
    • Shift = L2
    • L2 = L2-shift, L1 = L1 + 360-shift. Now notice L1-L2 <180!
    • LmShifted = (L1 + L2) / 2
    • Lm = LmShifted + shift.
    • If we take these formulas together, we will have:
    • Lm = (L1-L2 + 360) / 2 + L2
  • if L2-L1 <180, Lm = (L1 + L2) / 2

The problem is that L2-L1 = 180. In this case, you have two opposite meridians, dividing the Earth in half, and are suitable for the role of the middle and the โ€œfourthโ€ meridians, on the right and left. It is up to you what to choose.

0
source

Source: https://habr.com/ru/post/1447286/


All Articles