iOS development-Enables simple positioning functions

Source: Internet
Author: User

I. Description of positioning

Many of today's applications have positioning capabilities. Indeed, the positioning function is very useful, for our life also has a great help. For example, to help you find a hotel, find a bank, etc...

There are three ways to achieve positioning:

1. Cell Phone Base station: (Depends on the intensity of mobile phone warfare (signal tower))
2. GPS: satellite positioning, high accuracy, but the cost of electricity
3. WIFI: According to the Internet IP address, (must be able to surf the internet)

And we are generally in the development of GPS positioning is more, we want to achieve positioning needs to use the Corelocation framework, corelocation mainly includes the positioning, geocoding (including anti-coding) function.

After iOS8.0 to achieve the positioning also need to add two key to the Info.plist file Nslocationalwaysusagedescription and Nslocationwheninusagedescription, respectively, represents the " Allow the application to access your location when you are not using it? "," Allow the application to access your location when you use the application? ”

Second, the realization of positioning function:

#import "ViewController.h"#import<CoreLocation/CoreLocation.h>@interfaceViewcontroller () <CLLocationManagerDelegate>//Location Manager@property (nonatomic, strong) Cllocationmanager *Manager;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.[self location];}-(Cllocationmanager *) manager{if(!_manager) {_manager=[[Cllocationmanager alloc] init]; }    return_manager;}#pragmaMark--Positioning-(void) location{        //1. Create a location managerSelf.manager =[[Cllocationmanager alloc] init]; //determine the current system version number    if([[Uidevice currentdevice].systemversion Floatvalue] >=8.0) {        //ask the user for our program usage location[Self.manager requestalwaysauthorization];    [Self.manager requestwheninuseauthorization]; }        //set the update distance for the location (10m update once) the real thing is to get the proxy method to go once again.Self.manager.distanceFilter =10.0f; //Set up proxySelf.manager.Delegate=Self ; //turn on the positioning function[Self.manager startupdatinglocation]; //turn on the phone's orientation function[Self.manager startupdatingheading]; NSLog (@"%@", self.manager.location); }#pragmaMark--Update your phone's location information-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations{//NSLog (@ "locations =%@", locations); //get the current location of the latestCllocation *location = (cllocation *) [Locations Lastobject]; DoubleLat =Location.coordinate.latitude; DoubleLon =Location.coordinate.longitude; NSLog (@"lat =%f,lon =%f", Lat,lon); //resolving location information using positional anti-coding//to create an object that is a positional anti-codeClgeocoder *geocoder =[[Clgeocoder alloc] init]; //use this object to get location information[Geocoder reversegeocodelocation:location completionhandler:^ (Nsarray *placemarks, Nserror *error) {        //traversal of parsed location information         for(Clplacemark *placemarkinchplacemarks) {            //nameNSLog (@"name =%@", Placemark.name); //StreetNSLog (@"thoroughfare =%@", Placemark.thoroughfare); //zi Jie daoNSLog (@"Subthoroughfare =%@", Placemark.subthoroughfare);    }    }]; //method of obtaining coordinates based on address[Geocoder geocodeaddressstring:@"Guangzhou"completionhandler:^ (Nsarray *placemarks, Nserror *error) {        //get the first landmark, the landmark stores detailed address information, note: A place name may search for multiple addressesClplacemark *placemark=[Placemarks Lastobject]; Cllocation*location=placemark.location;//locationClregion *region=placemark.region;//AreaNsdictionary *addressdic= placemark.addressdictionary;//A dictionary of detailed address information that contains some of the following information//nsstring *name=placemark.name;//Place Names//nsstring *thoroughfare=placemark.thoroughfare;//Street//nsstring *subthoroughfare=placemark.subthoroughfare;//Street-related information, such as house numbers, etc.//nsstring *locality=placemark.locality;//City//nsstring *sublocality=placemark.sublocality;//City-related information, such as iconic buildings//nsstring *administrativearea=placemark.administrativearea;//State//nsstring *subadministrativearea=placemark.subadministrativearea;//Other administrative area information//nsstring *postalcode=placemark.postalcode;//Postal Code//nsstring *isocountrycode=placemark. Isocountrycode; //Country Code//nsstring *country=placemark.country;//National//nsstring *inlandwater=placemark.inlandwater;//Water, Lakes//nsstring *ocean=placemark.ocean;//Ocean//Nsarray *areasofinterest=placemark.areasofinterest;//associated or interest-related landmarksNSLog (@"Location:%@, Area:%@, details:%@", Location,region,addressdic); }];}- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{    //Stop Location Update[Self.manager stopupdatinglocation];}#pragmaMark--Update the phone orientation-(void) Locationmanager: (Cllocationmanager *) Manager didupdateheading: (clheading *) newheading{//get the angle between the magnetometer on the phone and the geomagnetic Arctic    intHeader =newheading.magneticheading; if(Header >315&& Header < the) && (Header >=0&& Header <= $) {NSLog (@"your current cell phone is facing north."); }    Else if(Header > $&& Header <=135) {NSLog (@"your current cell phone is facing east."); }    Else if(Header >135&& Header <225) {NSLog (@"your current cell phone is facing south."); }    Else{NSLog (@"your current cell phone is facing west."); }}

Attention:

1. The positioning frequency and positioning accuracy should not be more accurate and better, depending on the actual situation, because the more accurate the more cost-performance, the more electricity.

2. If the positioning is successful, it will be called frequently according to the setting condition -(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) Locations method, this method returns an array of geo-location objects, each of which represents the location information (including longitude, latitude, poster, walking speed, and so on) and returns the array because sometimes a location point may contain multiple locations.

3. If you do not need real-time monitoring after using location services, you should immediately turn off location services to conserve resources.

4. In addition to providing positioning capabilities, Cllocationmanager can also call the Startmonitoringforregion: method to monitor the specified area.

iOS development-Enables simple positioning functions

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.