地图与定位01

//
//  CoreLocationViewController.m
//  CoreLocation
//
//  Created by xiaoyao on 15/3/23.
//  Copyright (c) 2015年 lije. All rights reserved.
//

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

@interface CoreLocationViewController () <CLLocationManagerDelegate> {
  CLLocationManager *_locationManager;
}
@end

@implementation CoreLocationViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  
  // 创建定位管理器对象
  _locationManager = [[CLLocationManager alloc] init];
  
  if (![_locationManager locationServicesEnabled]) {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"是否允许启动定位服务"
                                                       delegate:nil
                                              cancelButtonTitle:@"取消"
                                              otherButtonTitles:@"确定", nil];
    [alertView show];
  }
  
  // 如果定位服务状态未授权的话进行授权
  if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
    [_locationManager requestWhenInUseAuthorization];
  } else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse ||
             [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways){
    // 设为使用应用程序的时候进行定位状态
    _locationManager.delegate = self;
    // 定位精度
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    // 定位频率
    CLLocationDistance distance = 10.0f;
    _locationManager.distanceFilter = distance;
    // 开始追踪定位
    [_locationManager startUpdatingLocation];
  }
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  
  [_locationManager stopUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
  for (int i = 0; i < locations.count; i++) {
    CLLocation *loction = [locations objectAtIndex:i];
    CLLocationCoordinate2D coor = loction.coordinate;
    NSLog(@"经度= %f, 纬度= %f, 海拔= %f, 航向=%f,行走速度=%f",coor.longitude,coor.latitude,loction.altitude,loction.course,
          loction.speed);
  }
  
  // 如果不需要时时定位,则定位一次完成关闭定位
  [_locationManager stopUpdatingLocation];
}
@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值