From: http://www.cnblogs.com/syxchina/archive/2012/10/14/2723522.html
11.1 IOS positioning service
There are three positioning service components in IOS:
WiFi location: query the geographical location of a Wi-Fi router. Relatively power-saving, iPod Touch and iPad can also be used.
Positioning of the cellular base station through the mobile operator base station. It is also suitable for iPod Touch and iPad with 3G versions.
GPS satellite positioning: 3-4 GPS Positioning locations are the most accurate, but the power consumption is high and cannot be blocked.
Core location
Core location is the framework for developing and positioning service applications such as iPhone and iPad. We need to add a framework in xcode that exists in "corelocation. Framework.
The main class used is cllocationmanager, which is used to implement the locating service.
Corelocation. Framework
Locate a service instance
Project whereami:
Whereamiviewcontroller. h
#import <UIKit/UIKit.h>#import <CoreLocation/CoreLocation.h>@interface ViewController : UIViewController<CLLocationManagerDelegate> { CLLocationManager* locationManager;}@property (strong, nonatomic) CLLocationManager* locationManager;@property (retain, nonatomic) IBOutlet UILabel *longitudeText;@property (retain, nonatomic) IBOutlet UILabel *latituduText;@property (retain, nonatomic) IBOutlet UIActivityIndicatorView *activity;- (IBAction)findMe:(id)sender;- (IBAction)webMap:(id)sender;@end
Cllocationmanagerdelegate is the delegate for locating services. The common callback Method for location change is:
Locationmanager: didupdatetolocation: fromlocation: locationmanager: didfailwitherror:
Cllocationmanager is a service management class that allows you to set service parameters and obtain longitude and latitude.
M Loading Method
- (IBAction)findMe:(id)sender { self.locationManager = [[[CLLocationManager alloc] init] autorelease]; self.locationManager.delegate = self; self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; self.locationManager.distanceFilter = 1000.0f; [self.locationManager startUpdatingLocation]; [activity startAnimating]; NSLog(@"start gps");}
Cllocationmanager is the startupdatinglocation method to start all positioning hardware. The corresponding method is stopupdatinglocation. You can call this method to disable location Server Update. In order to save power, you must call this method to disable the location service.
In addition, we can also set the service parameters here, including distancefilter and desiredaccuracy.
Distancefilter, which is used to control the frequency of locating service updates. The Unit is meter ". Desiredaccuracy, which is used to control positioning accuracy and accuracy.
The higher the power consumption, the larger the power consumption.
Positioning Accuracy
The desiredaccuracy precision parameter can be implemented through constants in ios sdk:
Kcllocationaccuracynearesttenmeters, 10 meters
Kcllocationaccuracyhundredmeters, 100 meters
Kcllocationaccuracykilometer, 1000 meters
Kcllocationaccuracythreekilometers, 3000 meters
Kcllocationaccuracybest, the best precision
Kcllocationaccuracybestfornavigation, the best precision in navigation, IOS 4 SDK is added. Generally, an external power supply is required.
The delegate method is used to update the location.
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { latituduText.text = [NSString stringWithFormat:@"%3.5f",newLocation.coordinate.latitude]; longitudeText.text = [NSString stringWithFormat:@"%3.5f",newLocation.coordinate.longitude]; [activity stopAnimating]; [locationManager stopUpdatingLocation]; NSLog(@"location ok");}
This delegate method not only obtains the current location (newlocation), but also obtains the longitude of the last location (oldlocation), cllocation object coordinate. Latitude attribute, and coordinate. longpolling attribute to obtain the latitude.
[Nsstring stringwithformat: @ "% 3.5f", % 3.5f in newlocation. Coordinate. Latitude] indicates that the output integer is 3 digits, and the decimal part is a 5-digit floating point number.