In the project function has a positioning cllocation needs, encountered some knowledge and difficulties, after the help of the heroes, problem solving, hereby share for everyone to learn, I hope we learn and progress together.
One, simple explanation
1.CLLocationManager
Common operations and properties for Cllocationmanager
Start user positioning-(void) startupdatinglocation;
Stop user positioning-(void) stopupdatinglocation;
Note: When the Startupdatinglocation method is invoked, the user's position is constantly positioned, and the following method of the proxy is frequently invoked
-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations;
Every number of meters to locate once
@property (Assign, nonatomic) cllocationdistance distancefilter;
Positioning accuracy (the more accurate the more power consumption)
@property (Assign, nonatomic) cllocationaccuracy desiredaccuracy;
With the positioning function, you first import the framework, follow the Cllocationmanagerdelegate protocol, and then create the location manager Cllocationmanager
After the iOS8.0, The positioning feature needs to include the Nslocationwheninuseusagedescription and nslocationalwaysusagedescription two nsstring type fields in Info.plist. To use the positioning function
Code posted out to share with you, you reader self study
{Self.locationmanager = [[Cllocationmanager alloc] init];
_locationmanager.delegate = self;
if ([cllocationmanager locationservicesenabled] = no) {//NSLog (@ "No GPS service");
}//geographic accuracy _locationmanager.desiredaccuracy=kcllocationaccuracynearesttenmeters; Set the distance filter, double type, as long as the distance changes how much, call the delegate agent Self.locationManager.distanceFilter = Kcldistancefilternone;
meters [_locationmanager startupdatinglocation] at the front of the _locationmanager requestwheninuseauthorization];//; }-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) Locations {NSLog (@ "longitude =
%f ", ((Cllocation *) [Locations Lastobject]). Coordinate.longitude);
NSLog (@ "latitude =%f", ((Cllocation *) [Locations Lastobject]). Coordinate.latitude);
CGFloat longti= (cllocation *) [Locations Lastobject]). Coordinate.longitude;
CGFloat latti= (cllocation *) [Locations Lastobject]). Coordinate.latitude; Displays the longitude to _longitudelabel on the label.Text = [NSString stringwithformat:@ "%f", Longti];
The latitude reality to the label _latitudelabel.text = [NSString stringwithformat:@ "%f", Latti];
Get the name of the city that is currently in clgeocoder *geocoder = [[Clgeocoder alloc] init]; Compile the address information according to the longitude and latitude [geocoder reverseGeocodeLocation:locations.lastObject completionhandler:^ (Nsarray *array, Nserror *error) {if (Array.count > 0) {clplacemark *placemark = [array objectatindex:0];//
Displays all the information obtained to the label//Self.location.text = Placemark.name;
Get the city nsstring *city = placemark.locality; if (!city) {//Four municipalities of the city information can not be obtained through the locality, only through the acquisition of provincial methods to obtain (if City is empty, then known as the central Municipality) cities = Placemark.administrative
area;
}//NSLog (@ "city =%@", city);
_cityname=city;
else if (Error = = Nil && [array count] = 0) {//NSLog (@ "No results were returned.");}
else if (Error!= nil) {//NSLog (@ "An error occurred =%@", error);
}
}]; The system will always be moreNew data until you choose to stop the update because we only need to get a latitude and longitude, so we stop updating [manager Stopupdatinglocation] after we get it. }
The above is about the cloud Habitat Community Small series for everyone to tidy up the development of iOS detailed positioning cllocation, follow-up will continue to update, I hope we can like.