天天看點

CLLocationManager地理定位

//
//  ViewController.m
//  FindMe
//
//  Created by MQL on 15/3/14.
//  Copyright (c) 2015年 MQL. All rights reserved.
//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()<CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
    CLGeocoder *geocoder;
    CLPlacemark *placemark;
    
}

@property (weak, nonatomic) IBOutlet UILabel *addressLabel;

- (IBAction)findMeBtnClicked:(UIButton *)sender forEvent:(UIEvent *)event;


@end

@implementation ViewController

-(void)loadView
{
    [super loadView];
    

    //應用啟動時的預設操作
    if (locationManager == nil) {
        
        locationManager = [[CLLocationManager alloc]init];
        locationManager.delegate = self;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        
        geocoder = [[CLGeocoder alloc]init];
        
    }
 
    if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 8.0)
    {
        //設定定位權限 僅ios8有意義
        [locationManager requestWhenInUseAuthorization];// 前台定位
        
    }
    [locationManager startUpdatingLocation];
    //應用啟動時的預設操作
}

-(BOOL)isCanLocate
{
    if([CLLocationManager locationServicesEnabled] == NO){
            
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"請啟動裝置定位服務" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [alert show];
        return NO;
            
    }else if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied){
            
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"請啟動你的app定位服務" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [alert show];
        return NO;
    }
    
    return YES;
}


- (IBAction)findMeBtnClicked:(UIButton *)sender forEvent:(UIEvent *)event {
    
    if([self isCanLocate]){
        
        //擷取經緯度
        
    }
    
}


#pragma mark -- CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations
{
    CLLocation *loc = [locations lastObject];
    NSLog(@"lat%f - lon%f", loc.coordinate.latitude, loc.coordinate.longitude);
    
    //地理編碼
    [geocoder reverseGeocodeLocation:loc completionHandler:^(NSArray *placemarks, NSError *error) {
        
        if (error == nil && [placemarks count] > 0) {
            
            placemark = [placemarks lastObject];
            self.addressLabel.text = placemark.name;
            
        }
        
    }];

}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    if (status == 0) {
        
        if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 8.0)
        {
            //設定定位權限 僅ios8有意義
            [locationManager requestWhenInUseAuthorization];// 前台定位
            
        }
    }
    else if (status >= 3) {
        
        [locationManager startUpdatingLocation];
    }
}



@end