Using Location and Heading Services IPHONE APPS

The Core Location framework provides support for locating the user’s current location and heading. This framework gathers information from the available onboard hardware and reports it to your application asynchronously. The availability of data is dependent on the type of device and whether or not the needed hardware is currently enabled, which it may not be if the device is in airplane mode.

To use the features of the Core Location framework, you must add CoreLocation.framework to your Xcode project and link against it in any relevant targets. To access the classes and headers of the framework, include an #import <CoreLocation/CoreLocation.h> statement at the top of any relevant source files.

Getting the User’s Current Location

The Core Location framework lets you locate the current position of the device and use that information in your application. The framework takes advantage of the device’s built-in hardware, triangulating a position fix from available signal information. It then reports the location to your code and occasionally updates that position information as it receives new or improved signals.

If you do use the Core Location framework, be sure to do so sparingly and to configure the location service appropriately. Gathering location data involves powering up the onboard radios and querying the available cell towers, Wi-Fi hotspots, or GPS satellites, which can take several seconds. In addition, requesting more accurate location data may require the radios to remain on for a longer period of time. Leaving this hardware on for extended periods of time can drain the device’s battery. Given that position information does not change too often, it is usually sufficient to establish an initial position fix and then acquire updates periodically after that. If you are sure you need regular position updates, you can also configure the service with a minimum threshold distance to minimize the number of position updates your code must process.

To retrieve the user’s current location, create an instance of the CL Location Manager class and configure it with the desired accuracy and threshold parameters. To begin receiving location notifications, assign a delegate to the object and call the start Updating Location method to start the determination of the user’s current location. When new location data is available, the location manager notifies its assigned delegate object. If a location update has already been delivered, you can also get the most recent location data directly from the CL Location Manager object without waiting for a new event to be delivered.

Listing shows implementations of a custom start Updates method and the location Manager: did Update To Location: from Location: delegate method. The start Updates method creates a new location manager object (if one does not already exist) and uses it to start generating location updates. (In this case, the location Manager variable is a member variable declared by the My Location Getter class, which also conforms to the CL Location Manager Delegate protocol.) The handler method uses the timestamp of the event to determine how recent it is. If it is an old event, the handler ignores it and waits for a more recent one, at which point it disables the location service.

Initiating and processing location updates

Checking the timestamp of an event is recommended because the location service often returns the last cached location event immediately. It can take several seconds to obtain a rough location fix so the old data simply serves as a way to reflect the last known location. You can also use the accuracy as a means of determining whether you want to accept an event. As it receives more accurate data, the location service may return additional events, with the accuracy values reflecting the improvements accordingly.

Getting Heading-Related Events

The Core Location framework supports two different ways to get heading-related information. Devices that contain GPS hardware can provide rough information about the current direction of travel using the same location events used to determine the user’s latitude and longitude. Devices that contain a magnetometer can provide more precise heading information through heading objects, which are instances of the CL Heading class.

The process for retrieving rough location events using GPS hardware is the same as the one described in “Getting the User’s Current Location”. The CL Location object reported to your application delegate contains course and speed properties with the relevant information. This interface is appropriate for most applications that track the user’s movement over time, such as those that implement car-based navigation systems. For compass-based applications, or other applications that might involve knowing the user’s current heading when not in motion, you can ask the location manager to provide heading objects.

In order to receive heading objects, your application must be running on a device that contains a magnetometer. A magnetometer measures nearby magnetic fields emanating from the Earth and uses them to determine the precise orientation of the device. Although a magnetometer can be affected by local magnetic fields, such as those emanating from fixed magnets found in audio speakers, motors, and many other types of electronic devices, the Core Location framework is smart enough to filter out many local fields to ensure that heading objects contain useful data.

To receive heading events, you create a CL Location Manager object, assign a delegate to it, and call the start Updating Heading method, as shown in Listing. Before asking for heading events, however, you should always check the heading Available property of the location manager to make sure that the appropriate hardware is present. If it is not, you should fall back to using location-based events to retrieve course information.

Initiating the delivery of heading events

The object you assign to the delegate property must conform to the CL Location Manager Delegate protocol. When a new heading event arrives, the location manager object calls the location Manager: did Update Heading: method to deliver that event to your application. Upon receiving a new event, you should check the heading Accuracy property to ensure that the data you just received is valid, as shown in Listing.

Processing heading events

The magnetic Heading property of a CL Heading object contains the main heading data and is always present. This property gives you a heading measurement relative to magnetic North, which is not at the same location as the north pole. If you want a heading relative to the north pole (also known as geographical North), you must call the start Updating Location method to start the delivery of location updates before you call start Updating Heading. You can then use the true Heading property of the CL Heading object to obtain the heading to geographical North.

Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status