Http://blog.csdn.net/csj1987/article/details/6657468
The core location in IOS provides the positioning function to locate the current coordinates of the device and obtain the device movement information. Because the polling of the positioning device consumes a lot of power, it is best to start it only when necessary.
Among them, the most important class is cllocationmanager, positioning management.
There are three methods for positioning:
1. GPS, the most accurate positioning method, seems to be not supported by iPhone 1.
2. Triangular positioning of cellular base stations. Such positioning is more accurate in cities where signal base stations are relatively confidential.
3. WiFi, which seems to be the data obtained through the database of the network operator, is the most inaccurate in the three types of positioning
Usage:
1. Introduce the corelocation package. Generally, it is not in the default template, so you need to import it manually.
2. Start the location service by starting the cllocationmanager, because the location information needs to be round-robin, and it takes some time for the program to get it, therefore, most of the operations of cuyu lcationmanager are performed by delegation.
Code for loading locationmanager:
[Plain]View
Plaincopy
- Cllocationmanager * locationmanager = [[cllocationmanager alloc] init]; // create a location manager
- Locationmanager. Delegate = self;
- Locationmanager. desiredaccuracy = kcllocationaccuracybest;
- Locationmanager. distancefilter = 1000.0f;
- // Start position update
- [Locationmanager startupdatinglocation];
Desiredaccuracy is set to the positioning precision. It can be set to the optimum, and the device will automatically locate it in the most accurate way.
Distancefilter is a distance filter. To reduce the number of polling times on the positioning device, the location change will not notify the delegate every time, but will only notify the delegate program when enough distance is moved, its unit is meters. Here it is set to move at least 1000 and then notify the delegate to process updates.
Startupdatinglocation is used to start positioning management. In general, it is best to disable it when you do not need to update the positioning, and use stopupdatinglocation to save power.
The most common method for entrusting cllocationmanagerdelegate is:
-(Void) locationmanager :( cllocationmanager *) Manager
Didupdatetolocation :( cllocation *) newlocation
Fromlocation :( cllocation *) oldlocation;
This method is used to locate the method that the delegate will execute when the change is made.
We can get the new location, the old location, and the cllocation contains the coordinates of the longitude and latitude,
At the same time, cllocation also has a horizontalaccuracy attribute to obtain horizontal precision. Its size is the radius of positioning accuracy, in meters.
If the value is-1, the positioning is untrusted.
Another common method of delegation is
-(Void) locationmanager :( cllocationmanager *) Manager
Didfailwitherror :( nserror *) error;
This method is called when an error is found.