CoreLocation technology and corelocation Technology

Source: Internet
Author: User

CoreLocation technology and corelocation Technology

The CoreLocation framework can be used to locate the current longitude and latitude of a device. Through this framework, applications can use nearby cellular base stations, Wi-Fi signals, or GPS information to calculate user locations.

Three modes supported by iOS positioning. (1) GPS satellite positioning (2) base station positioning (3) WiFi iOS developers use the CoreLocation. framework for locating is very simple. The commonly used APIs of the CoreLocation framework mainly include the following. (1) CLLocationManger: locates the manager class. (2) CLLocationManagerDelegate: This Protocol represents the delegate protocol of the positioning manager. Objects that implement this protocol can handle the location events of CLLocationManager. (3) CLLocation: This object represents a location. This object contains the longitude, latitude, height, speed, route, and other information of the current device, as well as the horizontal precision, vertical precision, and timestamp information of the positioning information. (4) CLHeading: This object represents the direction of the device. (5) CLRegion: This object represents a region. Generally, the program does not directly use this class, but uses its two subclasses, CLCircularRegion (circular area) and CLBeaconRegion (Bluetooth signal area ). 1. Obtain location information (1) create a CLLocationManager object that obtains location information. And set some necessary properties for this object. (2) Specify the delegate attribute for CLLocationManager. The attribute value must be an object that implements the CLLocationManagerDelegate protocol. When implementing the CLLocationManagerDelegate protocol, you can implement specific methods in the Protocol as needed. (3) Call the startUpdatingLocation method of CLLocationManager to obtain the location information. When positioning ends, you can call the stopUpdatingLocation method to end and obtain positioning information. 1. Add CoreLocation. framework to the project. 2, In iOS8, you must add two fields in info. plist.A device with GPS hardware can generate the current direction (course attribute) and speed information of the device. The Location Manager carried by the iPhone device can return a calculated course value. With this value, we can get the current forward direction. The course value is 0 ~ Floating point between 360, 0 ° indicates the positive north direction, 90 ° indicates the positive east direction, 180 ° indicates the positive south direction, 270 ° indicates the positive west direction, the program can use the course value to detect the direction of the user's position. In addition, you can obtain the real direction of the device by using a magnetic meter. Follow these steps to use CLLocationManager to obtain the device direction. 1. Create a CLLocationManager object to obtain positioning information and set necessary attributes for the object. 2. Specify the delegate attribute for CLLocationManager. The attribute value must be an object that implements the CLLocationManagerDelegate protocol. When implementing the CLLocationManagerDelegate protocol, you can implement specific methods in the Protocol as needed. 3. Call the startUpdatingHeading method of CLLocationManager to obtain the direction information. When the direction is obtained, you can call the stopUpdatingHeading method to obtain the direction information. When the direction of the device changes, the iOS system automatically fires the locationManager: didUpdateHeading: Method of the delegate object of CLLocationManager, and the program can override this method to obtain the direction of the device. IOS allows you to set the following attributes for detection direction changes. 1) CLLocationDegress headingFilter: sets the delegate method only when the value of the device direction changes beyond the value of this attribute. 2) CLDeviceOrientation headingOrientation: sets the current direction of the device. A CLHeading object is returned in the listener direction, which contains the following attributes. 1) magnticHeading: This property returns the relative direction between the device and the magnetic north. 2) trueHeading: This property returns the relative direction between the device and zhenbei. 3) headingAccuracy: error range of the returned direction value. 4) timestamp: the generation time of the direction value returned by this attribute. Regional monitoring if you want iOS devices to enter a region to send a notification, this regional monitoring function is also called a proximity warning. The user's device keeps approaching the specified fixed point. When the distance from the fixed point is smaller than the specified range, the system can trigger corresponding processing. The user's device leaves the specified fixed point. When the distance from the fixed point is greater than the specified range, the system can also trigger corresponding processing. IOS region monitoring can also be implemented using CLLocationManager. The steps for listening to whether a device enters/leaves a region are as follows: 1) create a CLLocationManager object, which obtains location information, no necessary attributes are set for this object. For region monitoring, the CLLocationManager object needs to set the monitoredRegions attribute. This attribute value is used to set multiple regions that the device listens. 2) Specify the delegate attribute for CLLocationManager. The attribute value must be an object that implements the CLLocationManagerDelegate protocol. When implementing the CLLocationManagerDelegate protocol, you can implement specific methods in the Protocol as needed. 3) Call startMonitoringForRegion of CLLocationManager to perform regional monitoring. When the region monitoring is over, you can call the stopMonitoringForRegion method to end the region monitoring. Detailed CoreLocation usage code
# Import "ViewController. h "# import <CoreLocation/CoreLocation. h> @ interface ViewController () <CLLocationManagerDelegate> @ property (nonatomic, strong) CLLocationManager * mange; @ end @ implementation ViewController-(CLLocationManager *) mange {if (! _ Mange) {// create positioning manager _ mange = [[CLLocationManager alloc] init];/* extern const CLLocationAccuracy completion _ OSX_AVAILABLE_STARTING (_ MAC_10_7 ,__ IPHONE_4_0 ); extern const CLLocationAccuracy kCLLocationAccuracyBest; extern const CLLocationAccuracy accuracy 10 meters extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters; extern cons T CLLocationAccuracy kCLLocationAccuracyKilometer; extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers; one mile * // positioning accuracy, higher accuracy, more power consumption _ mange. desiredAccuracy = kCLLocationAccuracyBest; // location update frequency, in meters _ mange. distanceFilter = 5; _ mange. delegate = self;} return _ mange;}-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // enable after iOS8 You need to execute this method or to start locating, and configure info. plist CGFloat systemVersion = [UIDevice currentDevice]. systemVersion. floatValue; if (systemVersion & gt; = 8.0) {[self. mange requestAlwaysAuthorization];} // 2. determine whether a method can be executed // if ([self. mange respondsToSelector: @ selector (requestAlwaysAuthorization)]) {// [self. mange requestAlwaysAuthorization]; //} // 3. determine whether the if (! [CLLocationManager locationServicesEnabled]) {NSLog (@ "Open the map navigation button... ");} else {// obtain the status of the current location, clthorizationstatus = [CLLocationManager authorizationStatus]; // if the status of the location is not enabled if (status = kclthorizationstatusnotdetermined) {// front-end positioning [self. mange requestAlwaysAuthorization] ;}// enable the [self. mange startUpdatingLocation];}-(void) locationManager :( CLLocationManager *) manager didUpdateLocations :( NSArray <CLLocation *> *) locations {CLLocation * location = [locations lastObject]; // latitude and longitude CLLocationCoordinate2D coordinate = location. coordinate; NSLog (@ "longitude: % f, latitude: % f", coordinate. longpolling, coordinate. latitude); // After the positioning is successful, this proxy will only execute once, And all stop location updates [manager stopUpdatingLocation];}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.