任何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