iOS Location Development corelocation

Source: Internet
Author: User

Sometimes we do not need to view the map, but only need their own current location, this time there is no need to use Mapkit, direct access to the location service is good

Apple provides a corelocation framework to do the positioning function

First you want to import the Corelocation framework into your project

It is best to check if the location service is available before the location service opens, it is possible that the user has refused or the user has a problem with the Mobile location module, unable to locate

Location services are managed by Cllocationmanager.

Positioning success and other information is also sent to the object through the agent, in addition to the location often need to use the location of the decoding and anti-decoding (in fact, latitude and longitude and the conversion between place names)

The following example code has basic usage and attention points

#import "ViewController.h"#import<CoreLocation/CoreLocation.h>@interfaceViewcontroller () <CLLocationManagerDelegate>{    //Location Service ManagerCllocationmanager *_locationmanager; //using the geocoding deviceClgeocoder *_geocoder;}@end@implementationViewcontroller/** 1. In the development of LBS class applications, before obtaining the user positioning information, it is important to determine whether the location service allows + locationservicesenabled 2. Turn on positioning to get your current position [_locationmanager  Startupdatinglocation]; 3. According to latitude and longitude, know the exact place name Reversegeocodelocation 4. Get to latitude and longitude application by naming: navigation geocodeaddressstring*/- (void) viewdidload{[Super Viewdidload]; if(![Cllocationmanager locationservicesenabled]) {NSLog (@"location service is not available!"); return; } NSLog (@"Location Services available!"); _locationmanager=[[Cllocationmanager alloc] init]; //Locate the user location and start updating the user location//[_locationmanager startupdatinglocation]; //Set up proxy_locationmanager.Delegate=Self ; //instantiating a geocode device_geocoder =[[Clgeocoder alloc] init]; [_geocoder geocodeaddressstring:@"Zhongshan Gate"completionhandler:^ (Nsarray *placemarks, Nserror *error) {                 for(Clplacemark *placemarkinchplacemarks) {NSLog (@"%@%lu", Placemark, (unsignedLong) placemarks.count); }    }];}#pragmaMark-Update Location proxy method#pragmaMark as long as the location changes, will be called, electricity! If status is complete, it is best to close the positioning function in time! - (void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations{NSLog (@"location Change:%@", locations[0]); //based on latitude and longitude search (go to Apple backstage to find the exact location, must be connected to use)[_geocoder reversegeocodelocation:locations[0] completionhandler:^ (Nsarray *placemarks, Nserror *error) {NSLog (@"%@", placemarks[0]); }];}@end

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.