CLLocationManager 位置定位

转:http://my.oschina.net/chengliqun/blog/147870

最近由于项目需要,需要使用LBS相关技术,但网上搜索了一圈,文章不是很多,故自己就从头整理记录些吧,以供日后温习参考;

    下面就开始吧,首先,如标题,先定位吧。

    第一步,新建一个singleView的空白工程,如果新建,这里不做赘述了。

    第二步:因为地图开发相关的framework:MapKit.framework、CoreLocation.framework, 至于如何添加,一般的ios相关博客都是有介绍。

   在主界面的控制器 ViewController.h 文件中,我们啥也不做,.m文件中,我们需声明一个 CLLocationManager* locationManager的属性,我们让其实现CLLocationManagerDelegate的协议,并覆写其更新位置的方法,如下:

//
//  ViewController.m
//  LBS_001_CLLocationManager
//
//  Created by liqun on 13-7-17.
//  Copyright (c) 2013年 Block Cheng. All rights reserved.
//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>{
    
}

@property (nonatomic,retain)CLLocationManager* locationManager;

@end

@implementation ViewController

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        NSLog(@"nibName:  %@   bundle: %@",nibBundleOrNil,nibBundleOrNil);
        _locationManager = [[CLLocationManager alloc] init];
        
    }
    
    return  self;
}

- (void)dealloc
{
    self.locationManager = nil;
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //delegate
    self.locationManager.delegate = self;
    //The desired location accuracy.
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    //Specifies the minimum update distance in meters.

    self.locationManager.distanceFilter = kCLDistanceFilterNone;

    self.locationManager.purpose = @"To provide functionality based on user's current location.";

    [self.locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    NSLog(@"didChangeAuthorizationStatus---%u",status);
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"didChangeAuthorizationStatus----%@",error);
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    UIAlertView* av = [[UIAlertView alloc] initWithTitle:@"update" message:[NSString stringWithFormat:@"didUpdateToLocation:  newLocation: %@  old:%@",newLocation,oldLocation] delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
    [av show];
    [av release];
}

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}

@end

程序启动后,系统会进行授权询问,如下:

点击好同意后,便能得到相应的位置信息,在代理的回调方法中,弹出信息

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

效果如下:

其实,在给出获取位置信息前,我们应该给出用户以提示的,见上效果图:

self.locationManager.purpose = @"To provide functionality based on user's current location.";

如果用户拒绝后,我们应该做相应的提示处理,在代理方法中也需做相应的处理。

还有一种开去位置服务的方法,先检查位置服务是否可用,再获取:

if ([CLLocationManager locationServicesEnabled]){
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.purpose = @"To provide functionality based on user's current location.";
    [self.locationManager startUpdatingLocation];
} else {
    /* Location services are not enabled.
     Take appropriate action: for instance, prompt the user to enable location services */
    NSLog(@"Location services are not enabled");
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值