码迷,mamicode.com
首页 > 移动开发 > 详细

iOS下获取用户当前位置的信息

时间:2014-11-01 19:02:12      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:blog   io   os   ar   for   sp   div   on   art   

#import <MapKit/MKMapView.h>

@interface ViewController (){
    CLLocationManager *_currentLoaction;
    CLGeocoder *_geocoder;
    CLPlacemark *_placeMark;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    _geocoder = [[CLGeocoder alloc] init];
    
    _currentLoaction = [[CLLocationManager alloc] init];
    _currentLoaction.delegate = self;
    [_currentLoaction startUpdatingLocation]; 
}

#pragma mark - Location
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"locError:%@", error);
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    CLLocationCoordinate2D locat = [newLocation coordinate];
    double lattitude = locat.latitude;
    double longitude = locat.longitude;
    CLGeocodeCompletionHandler handler = ^(NSArray *placemark, NSError *error)
    {
        for (CLPlacemark *mark in placemark) {
            NSMutableDictionary *area_dic = [mark addressDictionary];
            [area_dic setValue:[NSString stringWithFormat:@"%f", lattitude] forKeyPath:@"lattitude"];
            [area_dic setValue:[NSString stringWithFormat:@"%f", longitude] forKeyPath:@"longitude"];
            NSLog(@"area_dic is %@", area_dic);
            
            NSArray *array = [area_dic objectForKey:@"FormattedAddressLines"];
            NSString *address = [area_dic objectForKey:@"FormattedAddressLines"];
            NSLog(@"array is %@", array);
            address = [array objectAtIndex:0];
            NSLog(@"address is %@", address);
            address = [area_dic objectForKey:@"City"];
            NSLog(@"City is %@", address);
        }
    };
    [_geocoder reverseGeocodeLocation:newLocation completionHandler:handler];
}

 

iOS下获取用户当前位置的信息

标签:blog   io   os   ar   for   sp   div   on   art   

原文地址:http://www.cnblogs.com/JayK/p/4067499.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!