//
// 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
CLLocationManager地理定位
最新推荐文章于 2018-11-27 21:22:51 发布