首页 > 其他 > 详细

地图与定位01

时间:2015-03-24 11:05:48      阅读:258      评论:0      收藏:0      [点我收藏+]
//
//  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

地图与定位01

原文:http://blog.csdn.net/u010606986/article/details/44587475

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!