有时一些小的需求,其实只是需要得知当前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://blog.csdn.net/u012917616/article/details/46858473