1. Getting the User's Current Location
Obtains the current user location.
There are three methods to obtain the location: GPS, cell tower triangulation, and Wi-Fi Positioning Service (WPS ). GPS is the most accurate positioning method, but not on the first generation of iphone. GPS determines the current position by reading many Satellite microwave signals. The cell site is calculated by the sites around the iphone. Using this technology in intensive areas of the cell site can achieve an accurate value, for example, in cities, but in areas with less intensive cellular sites, the effect is not that good. The last WPS uses the IP address connected to the Wi-Fi network to determine the location, which may cause a large error. These three methods consume a lot of electricity. How can we achieve positioning and power saving? The following method can be used to solve the problem: when using Core Location, you can choose accuracy, and carefully choose the accuracy you need, which can help you save a lot of electricity. The technology used by Core Location is hidden in applications. We don't need to choose GPS, cellular site technology, or WPS. We just need to tell it the accuracy we need. It will decide what technology to choose to meet our needs.
2. The Location Manager
The Core Location interface is easy to use. The main class used is CLLocationManager, which is usually called Location Manager. To use Core Location, we need to create the Location Manager object:
CLLocationManager * locationManager = [[CLLocationManager alloc] init];
In this case, we created the Location Manager object, but we have not started to locate it. We need to assign a proxy to Location Manager. When the Location information changes, Location Manager calls the proxy method. The positioning process takes an average of several seconds. The proxy method must follow the CLLocationManagerDelegate protocol.
3. Setting the Desired Accuracy
After setting the proxy, you must set the expected accuracy. As we just said, the higher the accuracy, the higher the power consumption. If you only want the application to determine which country or state it is, do not set high accuracy. Remember that sometimes you cannot get the precision you need.
The following is an example of setting proxy and setting accuracy:
LocationManager. delegate = self;
LocationManager. desiredAccuracy = kCLLocationAccuracyBest;
Precision is a constant of the double type. The Unit is meters, So if you set desiredAccuracy = 10, the precision is 10 meters, which tells Location Manager to reach the accuracy of 10 meters as much as possible. You can also set it to another constant:
[Cpp]
Extern const CLLocationAccuracy kCLLocationAccuracyBestForNavigation;
Extern const CLLocationAccuracy kCLLocationAccuracyBest;
Extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;
Extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters;
Extern const CLLocationAccuracy kCLLocationAccuracyKilometer;
Extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers; 4. Setting the Distance Filter
4. Setting the Distance Filter
By default, Location Manager calls a proxy whenever the Location changes.
By setting distance filter, Location Manager can call the corresponding proxy method only when the Location changes beyond a certain range. This can achieve power saving.
Example: locationManager. distanceFilter = 1000.0f;
If you set the default value:
LocationManager. distanceFilter = kCLDistanceFilterNone;
5. Starting the Location Manager
Start locating
We can call it when you prepare to consume battery for positioning.
[LocationManager startUpdatingLocation];
To tell Location Manager to start locating
6. Using the Location Manager Wisely
If you only need to determine the current Location without further locating, you must notify the agent to stop locating when Location Manager queries the current Location.
Stop method: [locationManager stopUpdatingLocation];
7. The Location Manager Delegate
Location Manager must comply with the CLLocationManagerDelegate protocol. The CLLocationManagerDelegate Protocol defines two methods, and both methods are optional. One method is called when the current location or location information is determined to change. Another method is to call
8. Getting Location Updates
When Location Manager wants to notify its proxy of the current Location, it will call
LocationManager: didUpdateToLocation: fromLocation: method.
This method has three parameters. The first parameter is Location Manager. The second parameter is a CLLocation object used to determine the current location, and the third parameter is the CLLocation object used to determine the location of the last update. When this method is called, the previous location object will be left empty.
9. Getting Latitude and longpolling Using CLLocation
Longitude and latitude
Location information is obtained from Location Manager by using an instance of the CLLocation class. This class has five attributes. Latitude and longpolling are encapsulated in the coordinate attribute.
The method is as follows:
CLLocationDegrees latitude = theLocation. coordinate. latitude;
CLLocationDegrees longpolling = theLocation. coordinate. longpolling;
The CLLocation object also tells you the accuracy.
The horizontalAccuracy attribute describes the center of the adjustment. The larger the value of horizontalAccuracy, the more inaccurate it is.
CLLocation also has an attribute named altitude to indicate the elevation of altitude.
CLLocationDistance altitude = theLocation. altitude;
Each CLLocation object has an attribute called verticalAccuracy to determine accuracy. The elevation value may have an error in verticalAccuracy. When verticalAccuracy is a negative value, the Core Location notifies you that you cannot obtain the elevation of altitude.
Another attribute of CLLocation, timestamp, is used to tell Location Manager when to locate the Location. CLLocation also has an instance method to tell you the distance between two CLLocation objects. This method is called getDistanceFrom:
It works like this:
CLLocationDistance distance = [fromLocation getDistanceFrom: toLocation];
Returns the distance between two time periods. Sometimes it does not consider the elevation, so you need to calculate the distance yourself.
10. Error configurations
If the Core Location cannot specify your current Location, it will call the second proxy method of CLLocation: locationManager: didFailWithError:, the most common is that the user cancels the use of Location information.
From likendsl's column