iOS Development-ios8 location targeting

Source: Internet
Author: User

Now the app basically has the positioning function, travel site according to location recommended tourist attractions, news app through geographical location recommend local news, social apps through location friends, iOS to achieve the above function requires a nuclear The framework of the heart Corelocation, the framework provides some services that can get and locate the user's current location. The service will notify the user of a change in location through a low-power mode, three ways of status in iOS , WiFi location (by querying the location of a WiFi router ), Cellular Base station positioning (by mobile use of the business base stood still GPS satellite positioning (highest accuracy, maximum power consumption).

1. Create a new iOS project and import the core framework (#import <CoreLocation/CoreLocation.h>) in Viewcontroller;

2. Define a cllocationmanager variable, Implement cllocationmanagerdelegate Protocol, Cllocationmanager is responsible for the specific implementation;

Code in ViewController.h:

#import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> @interface Viewcontroller: uiviewcontroller<cllocationmanagerdelegate>{    Cllocationmanager *mylocationmanager;} @end

Code in the Viewdidload method:

    Self.view.backgroundcolor=[uicolor Greencolor];    if (nil = = Mylocationmanager)        Mylocationmanager = [[Cllocationmanager alloc] init];        Mylocationmanager.delegate = self;    Set the accuracy of positioning    mylocationmanager.desiredaccuracy = Kcllocationaccuracykilometer;        Set Location service Update frequency    Mylocationmanager.distancefilter =;        if ([[[[Uidevice Currentdevice] systemversion] doublevalue]>=8.0)    {        [Mylocationmanager requestwheninuseauthorization];//Front Desk Location        NSLog (@ "Current version is%f", [[[Uidevice Currentdevice] systemversion] Doublevalue]);            [Mylocationmanager requestalwaysauthorization];//both front and rear positioning    }        [Mylocationmanager startupdatinglocation] ;

As follows:

3. If you cannot eject the above information, you need to set up in the Info.plist file, add a nslocationwheninuseusagedescription (front desk to get GPS location), Nslocationalwaysusagedescription (pre-background get GPS positioning), value can be empty;

4. Common method Calls:

Most protocols will contain a method for handling failures, Didfailwitherror in Corelocationdelegate:

-(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (nserror *) error{    NSLog (@ " Flyelephant-http://www.cnblogs.com/xiaofeixiang ");}

Get the change after the geolocation didupdatelocations,locations is a chronological collection:

An array after geolocation is complete-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations{    / /Get the latest location    cllocation * currentlocation = [locations lastobject];    Cllocationdegrees Latitude=currentlocation.coordinate.latitude;    Cllocationdegrees Longitude=currentlocation.coordinate.longitude;        NSLog (@ "didupdatelocations latitude of current position:%.2f--longitude%.2f", latitude,longitude);}

Get the starting and ending points for a geographic change, didupdatetolocation:

-(void) Locationmanager: (Cllocationmanager *) Manager didupdatetolocation: (cllocation *) newlocation fromlocation: ( Cllocation *) oldlocation{     cllocationdegrees latitude=newlocation.coordinate.latitude;    Cllocationdegrees Longitude=oldlocation.coordinate.longitude;    NSLog (@ "didupdatetolocation latitude of current position:%.2f--longitude%.2f", latitude,longitude);}    

Relatively simple, for specific use can refer to the official Apple document https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/ Locationawarenesspg/corelocation/corelocation.html#//apple_ref/doc/uid/tp40009497-ch2-sw1

iOS Development-ios8 location targeting

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.