To help you cut down on the amount of code you write, Apple provides functions in many of its frameworks. Today, we’ll look at the Core Location Functions that are available in that particular framework.
Just 2 functions are available for this framework although they each can regularly be used in your project.
CLLocationCoordinate2DIsValid
The first of the functions is called CLLocationCoordinate2DIsValid. This particular function returns a Boolean that informs you if specified coordinates are valid.
This function can be used whenever you want to check that what you are using is valid. It is implemented as follows:
BOOL isValid = CLLocationCoordinate2DIsValid(region.centre);
Or, if you wanted to use it as part of a conditional statement and cut out the requirement for BOOL isValid, you could do the following: (noting that region.centre is a CLLocationCoordinate2D from a region that was implemented earlier).
if (CLLocationCoordinate2DIsValid(region.centre)) {
//run if the coordinate is valid.
} else {
//run this is not valid
}
There isn’t really anything else to add for this function. It’s as simple as it gets. You provide it a coordinate and it returns a BOOL to let you know if valid. However, I might as well mention what would trigger a response saying the location is not valid.
What Would Trigger a NO
From the documentation:
Its latitude is greater than 90 degrees or less than -90 degrees.
Its longitude is greater than 180 degrees or less than -180 degrees.
You would be wise implementing this method, especially if you are doing some kind of calculations to create coordinates such as antipode where you are finding the exact opposite on the other side of the globe.
CLLocationCoordinate2DMake
Next up is the CLLocationCoordinate2DMake function which as its name suggests, creates a CLLocationCoordinate2D.
The code is implemented as follows:
CLLocationCoordinate2D centre = CLLocationCoordinate2DMake(52.0293, -1.01234);
This particular line of code can also cut down several lines to a couple of lines. Take an MKCoordinateRegion as an example. This requires a region.centre with centre being a CLLocationCoordinate2D. Rather than specify the lng and lat individually as follows:
MKCoordinateRegion = region;
region.center.latitude = 52.0293;
region.center.longitude = -1.01234;
We could do this in just two lines:
MKCoordinateRegion = region;
region.center = CLLocationCoordinate2D(52.0293, -1.01234);
Although out of the scope of this Core Location Functions tutorial, Map Kit Framework also has functions. One in particular can make an MKCoordinateRegion with a function so that you can cut down everything to just a single line.
Final Thoughts
If the functions are available, use them. They save you writing extra lines of code.
To find what functions are available for a framework, load up the documentation. On the right side of the screen you will see a list of Other References (the other two headings being Class Reference and Protocol Reference). Look for the functions under Other to find all functions available for the selected framework. Some will have a few and others have many. Map Kit framework for example has many functions available to create regions and all sorts of other data needed.
Leave a Reply
You must be logged in to post a comment.