第十一章:位置服务

  任何IOS设备都没有配备独立的GPS芯片,实际上都是个辅助GPS芯片,需要通过网络数据计算位置信息(wifi,手机基站等)

  下面代码为:位置服务,区域监控

//
//  ViewController.m
//  Location_Demo
//
//  Created by Eric on 13-12-7.
//  Copyright (c) 2013年 Eric. All rights reserved.
//

#import "ViewController.h"


@interface ViewController ()

@end


@implementation ViewController

@synthesize locationManager;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    //启动定位服务
    [locationManager startUpdatingLocation];
    //启动方向
    //[locationManager startUpdatingHeading];
    
    
    //创建一个区域并监控
    
    //创建中心点,参数为经纬度
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(39, 119);
    //半径 单位:米
    CLLocationDistance radius =200;
    //标示
    NSString *address = @"address";
    //创建一个区域并监控
    CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:coordinate radius:radius identifier:address];
    //启用监控服务
    [self.locationManager startMonitoringForRegion:region];
}

//位置监控
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{

    //获取当前缓存的位置
    CLLocation *currLocation = [locations lastObject];
    //获取上次取的位置与现在的时间间隔
    NSTimeInterval interval = [[currLocation timestamp] timeIntervalSinceNow];
    //如果在30秒内 则直接输入你的位置
    if(interval >-30&&interval<0)
    {
        //输出经纬度
        NSLog(@"%.4f,%.4f",currLocation.coordinate.latitude,currLocation.coordinate.longitude);
        [[self locationManager] stopUpdatingLocation];
    }
}

//区域监控

//进入该区域
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{

}

//离开该区域
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{

}


//错误处理
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    //停止位置更新
    [[self locationManager] stopUpdatingLocation];
    
    //判断错误是否是拒绝访问
    if([error code]!=kCLErrorDenied)
    {
        //错误处理
    }else
    {
        //请求用户启用位置服务
    }
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
    
}

@end


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值