IOS development diary 49-detailed explanation of CLLocation, 49-cllocation

Source: Internet
Author: User

IOS development diary 49-detailed explanation of CLLocation, 49-cllocation

Today, the blogger has a need to locate CLLocation and encountered some difficulties. I would like to share with you the hope that we can make common progress.

To use the location function, you must first import the framework, follow the CLLocationManagerDelegate protocol, and then create the location manager CLLocationManager.

After iOS8.0, you must add NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription to info. plist to use the positioning function.

The code is posted to encourage you to study the code yourself.

{

Self. locationManager = [[CLLocationManager alloc] init];

_ LocationManager. delegate = self;

If ([CLLocationManager locationServicesEnabled] = NO ){

// NSLog (@ "no GPS service ");

}

// Geographic location accuracy

_ LocationManager. desiredAccuracy = kCLLocationAccuracyNearestTenMeters;

// Sets the distance filter and double type. The delegate proxy is called as long as the distance changes.

Self. locationManager. distanceFilter = kCLDistanceFilterNone; // meters

[_ LocationManager requestWhenInUseAuthorization]; // locates at the front end.

 

[_ LocationManager startUpdatingLocation];

 

}

 

-(Void) locationManager :( CLLocationManager *) manager

DidUpdateLocations :( NSArray *) locations

{

NSLog (@ "longpolling = % f", (CLLocation *) [locations

LastObject]). coordinate. longpolling );

NSLog (@ "latitude = % f", (CLLocation *) [locations lastObject]). coordinate. latitude );

CGFloat longTI = (CLLocation *) [locations

LastObject]). coordinate. longpolling;

CGFloat latTI = (CLLocation *) [locations lastObject]). coordinate. latitude;

// Display the longitude to the label.

_ LongitudeLabel. text = [NSString stringWithFormat: @ "% f", longTI];

// Bring the latitude to the label

_ LatitudeLabel. text = [NSString stringWithFormat: @ "% f", latTI];

// Obtain the name of the current city

CLGeocoder * geocoder = [[CLGeocoder alloc] init];

// Returns the address information based on the latitude and longitude.

[Geocoder reverseGeocodeLocation: locations. lastObject completionHandler: ^ (NSArray * array, NSError * error)

{

If (array. count> 0)

{

CLPlacemark * placemark = [array objectAtIndex: 0];

//// Display all obtained information on the label

// Self. location. text = placemark. name;

// Obtain the city

NSString * city = placemark. locality;

If (! City ){

// The city information of the four municipalities cannot be obtained through locality, but can only be obtained by obtaining the province information (if the city is empty, it can be known as a municipality)

City = placemark. administrativeArea;

}

// 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 update the data until you choose to stop the update. Because we only need to obtain the longitude and latitude once, the update will be stopped after obtaining the data.

[Manager stopUpdatingLocation];

}

 

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.