Note: In the measurement of the function of the time, more people will be surprised, why the code has been written, but when testing, only the first time to start the simulator location code is valid. That's because the simulator will have a default positioning location (the default location is Apple's US headquarters) in addition to the first boot, and all other times you'll need to manually turn it on, in the debug--and location--custom location (fill in latitude and longitude).
. h
1 #import <CoreLocation/CoreLocation.h>23// Location Manager to locate the current user's latitude and longitude 4 @property (nonatomic, Strong) Cllocationmanager *Locationmanager; 5 @property (nonatomic, strong) Clgeocoder *geocoder;
. m
1- (ID) Init2 {3 if(self =[Super Init]) {4 //Location Manager5_locationmanager =[[Cllocationmanager alloc] init];6_geocoder =[[Clgeocoder alloc] init];7 //The agent is notified when it is positioned to get the latitude and longitude of the user8_locationmanager.Delegate=Self ;9 }Ten One returnSelf ; A } - - #pragmaMark Start positioning the-(BOOL) startgetlocation - { - if([Cllocationmanager locationservicesenabled]) { + //Location Manager starts updating locations - [_locationmanager startupdatinglocation]; + returnYES; A}Else { atDLog (@"Location Services may not be open at this moment!"); - returnNO; - } -}
Clgeocoder is a geo-encoder that can encode the city name into latitude or vice versa, and this will be useful later. And the above is the use of Cllocationmanager start positioning a process, first determine whether the program location service is allowed, and then use Startupdatinglocation to start the positioning function.
1 #pragmaMark-Location Manager Agent method2- (void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) Locations3 {4 //1. Now that you have positioned the user's current latitude and longitude, you can let the location manager stop locating the5 [_locationmanager stopupdatinglocation];6 //2. Then, take out the first position, according to its latitude and longitude, by Clgeocoder reverse resolution, to obtain the location of the city name7Cllocation *loc =[Locations Firstobject];8 9 [self getaddressbylocation:loc];Ten } One A #pragmaMark obtains the place name according to coordinates --(void) Getaddressbylocation: (Cllocation *) Location - { the //__weak __typeof (self) weakself = self; - //Anti-geo-coding -[_geocoder reversegeocodelocation:location completionhandler:^ (Nsarray *placemarks, NSError *error) { -Clplacemark *placemark =[Placemarks firstobject]; +NSString *locality =placemark.locality; -Nsrange range = [locality rangeofstring:@"City"]; + if(Range.length >0) { A //remove the last character "city" atlocality = [Placemark.locality substringToIndex:placemark.locality.length-1];//City - } -NSLog (@"locality:%@", locality); - - //if ([Weakself.delegate respondstoselector: @selector (didfinishlocations:error:)]) { - //weakself.locationstate = suslocationfinish; in //[weakself.delegate didfinishlocations:[placemarks firstobject] error:error]; - // } to }]; +}
After booting through the proxy method Locationmanager:didupdatelocations: We can get the location of latitude and longitude, and then we need to use the Clgeocoder geocoding, Using the method Reversegeocodelocation:completionhandler: The anti-coding obtains a Clplacemark object, then the information that locates is inside the object. Clplacemark the corresponding properties can refer to the following code:
1Cllocation *location = placemark.location;//location2Clregion *region=placemark.region;//Area3Nsdictionary *addressdic= placemark.addressdictionary;//A dictionary of detailed address information that contains some of the following information4NSString *name=placemark.name;//Place Names5NSString *thoroughfare=placemark.thoroughfare;//Street6NSString *subthoroughfare=placemark.subthoroughfare;//Street-related information, such as house numbers, etc.7NSString *locality=placemark.locality;//City8NSString *sublocality=placemark.sublocality;//City-related information, such as iconic buildings9NSString *administrativearea=placemark.administrativearea;//StateTenNSString *subadministrativearea=placemark.subadministrativearea;//Other administrative area information OneNSString *postalcode=placemark.postalcode;//Postal Code ANSString *isocountrycode=placemark. Isocountrycode;//Country Code -NSString *country=placemark.country;//National -NSString *inlandwater=placemark.inlandwater;//Water, Lakes theNSString *ocean=placemark.ocean;//Ocean -Nsarray *areasofinterest=placemark.areasofinterest;//associated or interest-related landmarks
If you still need to get latitude and longitude by city name, you can use the following methods:
1 #pragmaMark determines geographical coordinates based on place names2- (void) Getcoordinatebyaddress: (NSString *) Address3 {4 //geo-coding5[_geocoder geocodeaddressstring:address completionhandler:^ (Nsarray *placemarks, Nserror *error) {6 //get the first landmark, the landmark stores detailed address information, note: A place name may search for multiple addresses7Clplacemark *placemark =[Placemarks firstobject];8 9DLog (@"location =%@--> latitude =%f----longitude =%f", Address, placemark.location.coordinate.latitude, placemark.location.coordinate.longitude);Ten }]; One}
positioning function of Ios-cllocationmanager