标签:
有时一些小的需求。事实上仅仅是须要得知当前IOS APP使用的地点。有些仅仅是想精确到城市级别,并不须要不论什么地图。
有了下面的简易实现:
@interface MainViewController ()<CLLocationManagerDelegate>
....
@end
@implementation MainViewController
- (void)InitLocation {
//初始化定位服务管理对象
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
self.locationManager.distanceFilter = 1000.0f;
}
/*
当中位置的精确性有例如以下几个级别,越是精确,程序回调就越慢!当前的样例仅仅是到城市级别,所以使用kCLLocationAccuracyThreeKilometers
extern const CLLocationAccuracy kCLLocationAccuracyBestForNavigation __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0);
extern const CLLocationAccuracy kCLLocationAccuracyBest;
extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;
extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters;
extern const CLLocationAccuracy kCLLocationAccuracyKilometer;
extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers;
*/
- (void) viewWillAppear:(BOOL)animated{
//開始定位
if(isSetAddress == FALSE){
dispatch_async(dispatch_get_main_queue(), ^{
[self InitLocation];
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager startUpdatingLocation];
});
}
}
#pragma mark Core Location托付方法用于实现位置的更新,能够得到经纬度,省、城市、街道
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:
(NSArray *)locations
{
CLLocation * currLocation = [locations lastObject];
NSLog(@"latitude=%3.5f, longitude=%3.5f, altitude=%3.5f", currLocation.coordinate.latitude, currLocation.coordinate.longitude, currLocation.altitude);
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:currLocation
completionHandler:^(NSArray *placemarks, NSError *error) {
if ([placemarks count] > 0) {
CLPlacemark *placemark = placemarks[0];
NSDictionary *addressDictionary = placemark.addressDictionary;
NSString *address = [addressDictionary objectForKey:(NSString *) kABPersonAddressStreetKey];
address = address == nil ?
@"": address;
NSString *state = [addressDictionary objectForKey:(NSString *) kABPersonAddressStateKey];
state = state == nil ? @"": state;
NSString *city = [addressDictionary objectForKey:(NSString *) kABPersonAddressCityKey];
city = city == nil ? @"": city;
NSLog(@"Place description: %@ \n%@ \n%@",state, address,city);
self.userAddress = [[NSString alloc] initWithFormat:@"%@", city];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"SetLiveShowAddress start");
int ret = SetLiveShowAddress(city, address);
if(ret == 0){
isSetAddress = TRUE;
[self.locationManager stopUpdatingLocation];
}
NSLog(@"SetLiveShowAddress end");
});
}
}];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"error: %@",error);
}
@end
标签:
原文地址:http://www.cnblogs.com/bhlsheji/p/5218485.html