1.系统自带的定位
#判断是否打开定位
+(BOOL)boolOpenLocation{
bool boolResult;
if ([CLLocationManager locationServicesEnabled] &&
([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized
|| [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)) {
boolResult = YES;
}else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied){
boolResult = NO;
}
return boolResult;
}
#跳转到系统设置中打开系统定位
NSURL *url;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
}else{
url = [NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]; //LOCATION_SERVICES打开系统定位
}
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
#iOS系统定位
NSLocationWhenInUseUsageDescription
.m文件中
- (void)dealloc
{
self.myLocationManager = nil;
self.myLocation = nil;
self.myGeocoder = nil;
}
- (void)viewDidLoad {
[super viewDidLoad];
if ([CLLocationManager locationServicesEnabled]) {
self.myLocationManager = [[CLLocationManager alloc] init];
[self.myLocationManager setDelegate:self];
self.myLocationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.myLocationManager.distanceFilter = 1.0;
[self.myLocationManager requestAlwaysAuthorization]; //iOS 7
[self.myLocationManager requestWhenInUseAuthorization]; //iOS 8以上
[self.myLocationManager startUpdatingLocation];
}else {
NSLog(@"Location services are not enabled");
}
}
#pragma mark CLLocationManagerDelegate 代理方法实现
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"locations is %@",locations);
CLLocation *location = [locations lastObject];
self.myGeocoder = [[CLGeocoder alloc] init];
[self.myGeocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error)
{
if(error == nil && [placemarks count]>0)
{
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSLog(@"name = %@",placemark.name);
NSLog(@"Country = %@", placemark.country);
NSLog(@"Postal Code = %@", placemark.postalCode);
NSLog(@"locality = %@", placemark.locality);
NSLog(@"subLocality = %@", placemark.subLocality);
NSLog(@"address = %@",placemark.name);
NSLog(@"administrativeArea = %@",placemark.administrativeArea);
NSLog(@"subAdministrativeArea = %@",placemark.subAdministrativeArea);
NSLog(@"ISOcountryCode = %@",placemark.ISOcountryCode);
NSLog(@"thoroughfare = %@", placemark.thoroughfare);
NSLog(@"subThoroughfare = %@",placemark.subThoroughfare);
}
else if(error==nil && [placemarks count]==0){
NSLog(@"No results were returned.");
}
else if(error != nil) {
NSLog(@"An error occurred = %@", error);
}
}];
[self.myLocationManager stopUpdatingLocation];
}
//定位失败
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"reverse geocoder error: %@", [error description]);
}