0

I would like to create a MKCoordinateRegion (to zoom to the good region on the map) from the northeast and southwest points given by Google. For that I need to compute the coordinate of the center between these two coordinates. Any clue? I could do simple math but I will have problems with the equator...

Thanks!!!

EmptyStack
  • 50,606
  • 20
  • 144
  • 175
ncohen
  • 6,184
  • 19
  • 61
  • 112

3 Answers3

1

Assuming you mean anti-meridian and not the equator then here goes (While all this works on a flattened map and should be good enough for your purpose, it's completely bung on a sphere. see note at the bottom).

What I've done in other cases is start at either point, and if the next point is more than 180 degrees to the east, I convert it so that it is less than 180 to the west like so

if(pointa.lon - pointb.lon > 180) pointb.lon += 360: else if (pointa.lon - pointb.lon < -180) pointb.lon -= 360

At this time pointb.lon might be an invalid longitude like 190 but you can at least work out the mid-point between pointa and point b because they will be on a continuous scale, so you might have points 175 and 190. Then just get the mid-point between them as 182.5, then convert that to make sure it is within the usual limits and you get -177.5 as the latitude between the two points. Working out the latitude is easy.

Of course on a sphere this is wrong because the midpoint between (-180,89) and (180,89) is (0*,90) not (0,89).
* = could be anything

Craig
  • 7,729
  • 8
  • 37
  • 69
0

Also, couldn't you just zoomToRect made with the defined corners? It'd save you doing this calculation and then next one which would be to work out what zoom level you need to be at when centered on that point to include the two corners you know about. Since the Maps app doesn't appear to scroll over the anti-meridian I assume MKMapview can't either so your rectangle is going to have to have the northeast coord as the top right and the southwest as the bottom left.

Craig
  • 7,729
  • 8
  • 37
  • 69
  • Please don't make more posts to add to your answer; just edit your previous answer. Questions and answers here are not like threads in forums. – jscs Sep 05 '11 at 19:56
  • I felt my answers were two different ways of solving the problem so they were entered separately so the user could pick which one was the best. But it appears they have wandered off else where and not commented on either one, so we'll never know which one, if any, worked. – Craig Sep 07 '11 at 04:51
-1

This SO post has the code to zoom a map view to fit all its annotations.

Community
  • 1
  • 1
EmptyStack
  • 50,606
  • 20
  • 144
  • 175
  • Do you mean the equator or the anti-meridan? There's no reason to have trouble with the equator since the latitudes are continuous across it. – Craig Aug 12 '11 at 00:43