您的位置:首页 > 移动开发 > IOS开发

iOS—使用CoreLocation框架获取定位信息

2014-05-13 17:14 597 查看
iOS自带的CoreLocation框架使用Delegate将定位封装得很好用,下面我采用单例设计模式创建一个定位管理类:

//
//  MyLocationManager.h
//  Common
//
//  Created by victor on 14-5-13.
//  Copyright (c) 2014年 visp. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

typedef void (^CLLocationFailHandler)(NSError *error);

@interface MyLocationManager : NSObject <CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *myLocationManager;
@property (nonatomic, strong) CLGeocoder *myGeocoder;
@property (nonatomic, assign) CLGeocodeCompletionHandler completionHandler;
@property (nonatomic, assign) CLLocationFailHandler failHandler;

+ (id)defaultLocation;

- (void)startLocationManagerWithSuccess:(CLGeocodeCompletionHandler)completionHandler error:(CLLocationFailHandler)failHandler;

@end


//
//  MyLocationManager.m
//  Common
//
//  Created by victor on 14-5-13.
//  Copyright (c) 2014年 visp. All rights reserved.
//

#import "MyLocationManager.h"

static id locationManager;

@implementation MyLocationManager

+ (id)defaultLocation {
if (locationManager == nil) {
locationManager = [[self alloc] init];
}

return locationManager;
}

- (void)startLocationManagerWithSuccess:(CLGeocodeCompletionHandler)completionHandler error:(CLLocationFailHandler)failHandler {
if ([CLLocationManager locationServicesEnabled]) {
self.completionHandler = completionHandler;
self.failHandler = failHandler;
self.myLocationManager = [[CLLocationManager alloc] init];
self.myLocationManager.delegate = self;
[self.myLocationManager startUpdatingLocation];
} else {
NSLog(@"Location services are not enabled");
}
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *location = [locations objectAtIndex:0];
NSLog(@"Location is completed");
NSLog(@"Latitude = %f", location.coordinate.latitude);
NSLog(@"Longitude = %f", location.coordinate.longitude);

[self.myLocationManager stopUpdatingLocation];

self.myGeocoder = [[CLGeocoder alloc] init];

NSLog(@"Start reverse geocode location");

[self.myGeocoder reverseGeocodeLocation:location completionHandler:self.completionHandler];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
self.failHandler(error);
}

@end


调用的代码如下:

- (void)configureLocationManager {
[[MyLocationManager defaultLocation] startLocationManagerWithSuccess:^(NSArray *placemarks, NSError *error) {
if (error == nil && [placemarks count] > 0) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSDictionary *address = placemark.addressDictionary;
NSLog(@"Country = %@", placemark.country);
NSLog(@"Postal Code = %@", placemark.postalCode);
NSLog(@"Locality = %@", placemark.locality);
NSString *state=[address objectForKey:@"State"];
NSString *city=[address objectForKey:@"City"];
NSString *subLocality=[address objectForKey:@"SubLocality"];
NSString *street=[address objectForKey:@"Street"];
NSLog(@"Location = %@", [NSString stringWithFormat:@"%@%@%@%@",state, city, subLocality, street]);
} else if (error == nil && [placemarks count] == 0) {
NSLog(@"No results were returned");
} else if (error != nil) {
NSLog(@"An error occurred = %@", error);
}
} error:^(NSError *error) {
NSLog(@"%@", [error localizedDescription]);
}];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐