zoukankan      html  css  js  c++  java
  • iOS下获取用户当前位置的信息

    #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];
    }
    
  • 相关阅读:
    推荐]历史上最强的绕口令
    超级经理人的关系学:打造黄金人脉
    个人创业的难点和解决之道
    你的人脉关系中不可缺少的十种人[推荐]
    哲理短文一则:揭示最好的成功法则
    [经验交流]太精彩,太有启发性了(经典经典) 转
    权力领域是人才浪费的致命区域
    2006创业完全手册
    爱你我的宝贝(转)
    最远的你是我最近的爱
  • 原文地址:https://www.cnblogs.com/JayK/p/4067499.html
Copyright © 2011-2022 走看看