0

I am using MapKit for my project and so far it has been very good. Here is a chunk of code I use for displaying and centering the map.

CLLocation *locSouthWest = [[CLLocation alloc] initWithLatitude:southWest.latitude longitude:southWest.longitude];
CLLocation *locNorthEast = [[CLLocation alloc] initWithLatitude:northEast.latitude longitude:northEast.longitude];
CLLocationDistance meters = [locSouthWest distanceFromLocation:locNorthEast];


MKCoordinateRegion region;
region.center.latitude = (southWest.latitude + northEast.latitude) / 2.0;
region.center.longitude = (southWest.longitude + northEast.longitude) / 2.0;
region.span.latitudeDelta = meters / 111319.5;
region.span.longitudeDelta = 0.0;

[self.mapView setRegion:region animated:YES];

The problem is, that it works differently on iPhone 4 and iPhone 5.

Here is iPhone 4 (same results for iOS5 and iOS6):

iPhone 4

and here is iPhone 5 (using the same coordinates):

iPhone 5

Anybody experiencing the same?

Michal
  • 15,429
  • 10
  • 73
  • 104
  • I notice the iPhone 5 isn't in english but the iPhone 4 is. Perhaps this is also a factor? – Ryan Poolos Jan 14 '13 at 13:14
  • Not a "solution" to your question but you don't need to calculate the delta manually from meters. Use the MKCoordinateRegionMakeWithDistance function instead. –  Jan 14 '13 at 13:14
  • @RyanPoolos No, the code is the same for all languages. – Michal Jan 14 '13 at 13:17
  • @AnnaKarenina I believe you pointed it out in my other question(http://stackoverflow.com/questions/12173496/mkannotation-not-responding-on-tap), but I didn't understand how, I don't want to hardcode the distance, I still need to calculate it right? – Michal Jan 14 '13 at 13:19
  • I wasn't suggesting the code was different merely that MapKit is localized and may handle what you're trying to do differently either by design or by accident. Basically that this may not be your fault. – Ryan Poolos Jan 14 '13 at 13:22
  • You only need to calculate the center and supply the meters: `CLLocationCoordinate2D center; center.latitude = ...; center.longitude = ...; MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center, meters, 0);` –  Jan 14 '13 at 13:27
  • @RyanPoolos Happy now? :-) – Michal Jan 14 '13 at 13:27
  • 1
    Well no :p Your problem wasn't solved haha. – Ryan Poolos Jan 14 '13 at 13:29
  • @AnnaKarenina And how do I supply meters? How do I calculate them? – Michal Jan 14 '13 at 13:29
  • 1
    @Michal, If you _know the meters_, use MKCoordinateRegionMakeWithDistance. Code in question is calculating the _diagonal_ distance between corners and using that as the latitudeDelta which is the _vertical_ diff in degrees. Just set the delta values to maximum diff for each direction (eg. latitudeDelta = fabs(latTop - latBottom)). The map will then still modify that requested region so it matches the map frame ratio and zoom level. If you're just trying to show all annotations on map, see http://stackoverflow.com/questions/4680649/zooming-mkmapview-to-fit-annotation-pins. –  Jan 14 '13 at 13:49
  • I worked it out thanks to the question you linked. Thanks a bunch @AnnaKarenina !!! – Michal Jan 16 '13 at 13:14

2 Answers2

1

These are the lines of code that are causing the difference:

region.center.latitude = (southWest.latitude + northEast.latitude) / 2.0;
region.center.longitude = (southWest.longitude + northEast.longitude) / 2.0;

Which are bound to create a difference since the screen sizes vary.

What you can do here to center the map is to use mapView.centerCoordinate

Sahil Tyagi
  • 363
  • 3
  • 20
1

MapKit has fixed zoom levels. Setting the map's region ensures that the region will be visible in the map, but does not set the exact zoom. This has several benefits, the primary one being you can't create a map that scales latitude and longitude disproportionately (leading to a confusing and/or misleading map). The frames of the maps are different sizes, so your selected region can display at different zoom levels on each device.

As an experiment, try setting the frame of the map view to the same size on both devices. Then, if you absolutely need the maps to display at the same scale you could do some math to compute the appropriate region based on the frame of the map.

Brandon DuRette
  • 4,810
  • 5
  • 25
  • 31