0

I have been learning a piece of code, and trying to understand the difference between MKCoordinateRegion vs region.span.latitudeDelta?

MKCoordinateRegion region = {{0.0, 0.0}, {0.0, 0.0}};
region.center.latitude = latitude;
region.center.longitude = longitude;
region.span.latitudeDelta = 0.1f;

Can someone please explain what the differences are based on the above. I feel like the first line decided the frame of the map but the rest seemed to repeat this (I defining the frame and position on map)

Rob
  • 415,655
  • 72
  • 787
  • 1,044

1 Answers1

0

The region is a MKCoordinateRegion. So the first line initializes a local variable with the structure (using the old C89 convention):

MKCoordinateRegion region = {{0.0, 0.0}, {0.0, 0.0}};

and the subsequent lines just update that previously initialized structure with the desired values:

region.center.latitude = latitude;
region.center.longitude = longitude;
region.span.latitudeDelta = 0.1f;

For what it is worth, you could alternatively just instantiate it and set the values all in one step (again, using the C89 convention):

MKCoordinateRegion region = { { latitude, longitude }, { 0.1, 0.1 } };

Or, nowadays, one might use the C99 convention of designated initializers:

MKCoordinateRegion region = {
    .center = { .latitude = latitude, .longitude = longitude },
    .span = { .latitudeDelta = 0.1, .longitudeDelta = 0.1 }
};

Again, this initializes and populates the MKCoordinateRegion in one step (and in this case, in a less cryptic manner than the C89 pattern).

The alternative is to build CLLocationCoordinate2D, MKCoordinateSpan, and MKCoordinateRegion using the various xxxMake functions:

CLLocationCoordinate2D center = CLLocationCoordinate2DMake(latitude, longitude);
MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);

For more information about initializing C structs, I would refer you to How to initialize a struct in accordance with C programming language standards. Also, the GCC documentation talks about designated initializers, including structs.


Or, if you are trying to set a map view’s region, another approach is to use MKMapCamera instead:

CLLocationCoordinate2D center = CLLocationCoordinate2DMake(latitude, longitude);
MKMapCamera *camera = [MKMapCamera cameraLookingAtCenterCoordinate:center fromDistance:1000 pitch:0 heading:0];
[self.mapView setCamera:camera animated:true];

But, bottom line, the region is effectively the range of latitude and longitude coordinates that the map will show (as defined by the combination of the center, where the map is looking, and the span, how much of the map is visible).

All of these map coordinates and spans should not be confused with the frame of the map view, which is the visual size of the map view that will show that region.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Thanks, wrapping my head around the explicit difference between `region` and `span`. If `region` defines the range of lat/long coordinates displayed then how does this differ from `span`. To the point what is `span`? I've clearly misunderstood as the Apple documentation seems to define it as similar to your region definition "_The horizontal and vertical span representing the amount of map to display._" If anyone has the inclination to assist further, visuals might help. – Alex Peter James Mar 05 '21 at 11:00
  • @PeterAskins - A region (aka, a `MKCoordinateRegion`, see [its documentation](https://developer.apple.com/documentation/mapkit/mkcoordinateregion)) consists of two properties, the `center` and the `span`. The `center` tells you where the map is looking and the span tells you how much of the map is visible. A region is the combination of those two pieces of information. – Rob Mar 05 '21 at 14:58
  • Great stuff. So am I right in saying that my original piece of code defined center and span in this line `MKCoordinateRegion region = {{0.0, 0.0}, {0.0, 0.0}};` and then when on to create variables for `centre` (long/lat) and `span`? If so, then thanks. Think I've got the basics. – Alex Peter James Mar 05 '21 at 21:19
  • @PeterAskins - Maybe we're saying the same thing, but the first line of your code snippet is creating a local variable that is a structure, and initializing all with zeros. Then the next three lines merely update that very same struct (not creating variables, but just accessing the elements of that existing struct). You can create the struct and populate in a single line of code, if you want. See expanded answer above. – Rob Mar 05 '21 at 21:45
  • 1
    Thanks for clarifying that. – Alex Peter James Mar 05 '21 at 22:58