Detailed explanation of iOS Positioning Service programming and ios Positioning
Currently, many mobile devices provide location services. The iPhone, iPod Touch, and iPad of iOS can provide location services. iOS devices can provide location services in three ways: Wifi, cellular Mobile phone base station, GPS satellite
Unlike the Android system, iOS does not specify which method to use to locate Service programming. The iOS API shields the bottom layer from these details. developers and users do not know which method the device uses to locate the problem. The iOS system will locate the problem based on the device situation and the surrounding environment, use an optimal solution. This solution is like this. If GPS information can be received, GPS positioning is preferred for devices; otherwise, Wifi or cellular base station positioning is preferred, and Wifi is preferred between Wi-Fi and cellular base stations, if you cannot connect to Wifi, use the cell Base Station to locate the problem.
In general, GPS positioning has the advantages of accuracy and wide coverage. Its disadvantage is that it cannot be blocked (for example, GPS satellite signals cannot be received in buildings), and it is more expensive after GPS is enabled. The cellular base station not only has a large error, but also consumes the user traffic fee. Wifi positioning is the most economical.
Positioning Service Programming
The API of the bitwise service does not change much after iOS 6. It mainly uses the CoreLocation framework and CLLocationManager, CLLocationManagerDelegate, and CLLocation. CLLocationManager is the management class of the positioning service. It can provide us with location and Height Information about the device, monitor the device to enter or exit a certain area, and help us obtain the operation direction of the device. CLLocationManagerDelegate is a CLLocationManager class delegation protocol. The CLLocation class encapsulates location and Height Information.
In the application that locates the service, when the first request obtains the location information, the system will prompt you whether to enable the location service. The user's location is relatively private information, and the application obtains such information. The user has the right to know and the right to deny. If the application obtains the location information of the user without the user's knowledge, this is illegal in some countries.
If you select "Not Allowed", the location information cannot be obtained by locating the service. To change these settings, you can enable or disable it in the system settings application.
We can disable all the positioning services. We only need to disable the "positioning service" control at the top. You can also disable and enable the following applications.
Next we will use a case to introduce the positioning service programming. When the application is started, the location information will be obtained when it enters the screen and displayed in the corresponding text box. If the device location sends a change, the location information is updated and the corresponding text box is updated.
First, we need to introduce the CoreLocation framework for the project to implement the locating Service case. The specific steps are to select TARGETS → WhereAmI → Build Phases → Link Binary With Libraries in the project, select the "+" button in the lower-right corner to open the framework and library selection dialog box.
In the Add dialog box, select CoreLocation. framework and click Add. We will not introduce the uidesign part. Let's take a look at the implementation code. The main code is written in ViewController of the View Controller. The ViewController. h code is as follows:
Cpp Code
- # Import <UIKit/UIKit. h>
- # Import <CoreLocation/CoreLocation. h>
- # Import <CoreLocation/CLLocationManagerDelegate. h>
- @ Interface ViewController: UIViewController <CLLocationManagerDelegate>
- // Longitude
- @ Property (weak, nonatomic) IBOutlet UITextField * txtLng;
- // Latitude
- @ Property (weak, nonatomic) IBOutlet UITextField * txtLat;
- // Height
- @ Property (weak, nonatomic) IBOutlet UITextField * txtAlt;
- @ Property (nonatomic, strong) CLLocationManager * locationManager;
- @ End
In the h file, you must first introduce the header files <CoreLocation/CoreLocation. h> and <CoreLocation/CLLocationManagerDelegate. h>. Then, when defining ViewController, you must declare the implementation of the CLLocationManagerDelegate protocol. We also defined the CLLocationManager * locationManager attribute.
The viewDidLoad code of ViewController. m is as follows:
Cpp Code
- -(Void) viewDidLoad
- {
- [Super viewDidLoad];
- // Locate service management object initialization
- _ LocationManager = [[CLLocationManager alloc] init];
- _ LocationManager. delegate = self;
- _ LocationManager. desiredAccuracy = kCLLocationAccuracyBest; ①
- _ LocationManager. distanceFilter = 1000.0f; ②
- }
In the viewDidLoad method, initialize the member Variable _ locationManager of CLLocationManager. First, use the [[CLLocationManager alloc] init] statement to instantiate the CLLocationManager object. Then the _ locationManager. delegate = self statement sets the positioning service delegate to self. The Code in line ① sets the desiredAccuracy attribute, which is a very important attribute and has six common values: kCLLocationAccuracyNearestTenMeters. Precision: 10 meters; kCLLocationAccuracyHundredMeters. Accuracy: 100; kCLLocationAccuracyKilometer. Precision: 1000 meters; kCLLocationAccuracyThreeKilometers. Accuracy: 3000; kCLLocationAccuracyBest. The highest precision when the device is powered by a battery; kCLLocationAccuracyBestForNavigation. The highest precision in navigation is generally used only when an external power supply is available;
The higher the precision, the shorter the request time for obtaining location information, which means the more power consumption the device consumes. Therefore, an application should choose the appropriate precision. If your application is used for on-board navigation, kCLLocationAccuracyBestForNavigation is a good choice. You can use the battery on the car to power the device. KCLLocationAccuracyHundredMeters is a good choice for navigation apps provided by hiking travelers.
The Code in line ② sets the distanceFilter attribute, which is a distance filter. It defines the minimum distance between the device's moving and updating location information. Its unit is meters. In this example, it is set to 1000 meters.
After the CLLocationManager is initialized, you need to use the startUpdatingLocation method to start locating the service. It is in the viewWillAppear: Method of ViewController. m, the Code is as follows:
Cpp Code
- -(Void) viewWillAppear :( BOOL) animated
- {
- [Super viewWillAppear: animated];
- // Start locating
- [_ LocationManager startUpdatingLocation];
- }
When you call the startUpdatingLocation method, the service is enabled. Based on the set conditions, the service continuously requests a new bit confidence message. Therefore, you must be cautious when enabling this method. To enable this method at the most appropriate time, viewWillAppear is the most suitable method in the View Controller's declaration cycle method. The corresponding method to enable the Service is the stopUpdatingLocation method, which is called in viewWillDisappear: Method of the View Controller. The Code is as follows:
Cpp Code
- -(Void) viewWillDisappear :( BOOL) animated
- {
- [Super viewWillDisappear: animated];
- // Stop Positioning
- [_ LocationManager stopUpdatingLocation];
- }
ViewWillDisappear: called when the view disappears (the application returns to the background), it ensures that the positioning service is disabled in the most timely manner. After iOS 6, the request changes. After the service application is removed from the background, the location information can be updated with a delay. The allowDeferredLocationUpdatesUntilTraveled: timeout method can be used to set a delayed update so that the application does not update the location information in the background. Disable delayed update using the disallowDeferredLocationUpdates method. In addition, the pausesLocationUpdatesAutomatically attribute is added after iOS 6. It can set Automatic pause for location update, and locate the Service's enable and suspend management permissions to the system, which makes it more reasonable and simple.
Once the location service is enabled and the CLLocationManager delegate attribute delegate is set, when the user's device moves to the filtering distance, the delegate method will be called back. There are two methods related to the location service:
LocationManager: didUpdateLocations: the location is successful. It is a new method of iOS 6, replacing the previous locationManager: didUpdateToLocation: fromLocation: method;
LocationManager: didFailWithError: locating failed;
The code for implementing the CLLocationManager delegate is as follows:
Cpp Code
- # The pragma mark Core Location delegate method is used to update the Location.
- -(Void) locationManager :( CLLocationManager *) manager didUpdateLocations :( NSArray *) locations
- {
- CLLocation * currLocation = [locations lastObject]; ①
- _ TxtLat. text = [NSString stringWithFormat: @ "% 3.5f ",
- CurrLocation. coordinate. latitude]; ②
- _ TxtLng. text = [NSString stringWithFormat: @ "% 3.5f ",
- CurrLocation. coordinate. longpolling]; ③
- _ TxtAlt. text = [NSString stringWithFormat: @ "% 3.5f ",
- CurrLocation. altitude]; ④
- }
- -(Void) locationManager :( CLLocationManager *) manager didFailWithError :( NSError *) error
- {
- NSLog (@ "error: % @", error );
- }
In the locationManager: didUpdateLocations: method, the locations parameter is a set of location changes, which are stored in the order of time changes. If you want to obtain the location of the current device, you can use the [locations lastObject] statement in line ① To obtain the last element in the Set, which is the current location of the device. The returned object type is CLLocation. CLLocation encapsulates location, height, and other information. In the code above, we used two attributes: altitude and coordinate. The altitude attribute is the height value, and the coordinate attribute is the struct that encapsulates the longitude and latitude CLLocationCoordinate2D. The definition of CLLocationCoordinate2D is:
Cpp Code
- Typedef struct {
- CLLocationDegrees latitude; // latitude
- CLLocationDegrees longpolling; // longitude
- } CLLocationCoordinate2D;
Here, latitude is the longitude information and longpolling is the latitude information. They are all CLLocationDegrees types, and CLLocationDegrees is the double type defined by typedef.
NewLocation in line ② code. coordinate. the latitude expression is used to obtain the current latitude of the device. The newLocation in the Code in line ③. coordinate. the longpolling expression is used to obtain the current latitude of the device, and the obtained height can be indicated by line 4 newLocation. the altitude expression is obtained directly.
How does iOS implement the positioning service?
Of course, too many software can be implemented now.
IOS60 locates where the service is enabled
In the privacy settings, there is a location service switch,