今天开始总结一下地图与定位。首先要使用定位就要引用框架CoreLocation.framework,在类文件中加入#import <CoreLocation/CoreLocation.h>,其次在IOS8中调用的时候要在Info.plist中加两个Key,NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription,具体参考http://www.cnblogs.com/kenshincui/p/4125570.html
#import "ViewController.h"
@interface ViewController ()<CLLocationManagerDelegate>
@property(nonatomic,strong)CLLocationManager *locationManager;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_locationManager=[[CLLocationManager alloc]init];
//判断定位服务是否可用
BOOL enable= [CLLocationManager locationServicesEnabled];
// //CLAuthorizationStatus
// typedef NS_ENUM(int, CLAuthorizationStatus) {
// //用户尚未做出决定是否启用定位服务
// kCLAuthorizationStatusNotDetermined = 0,
// //没有获得用户授权使用定位服务,可能用户没有自己禁止访问授权
// kCLAuthorizationStatusRestricted,
// //用户已经明确禁止应用使用定位服务或者当前系统定位服务处于关闭状态
// kCLAuthorizationStatusDenied,
// //应用获得授权可以一直使用定位服务,即使应用不在使用状态
// kCLAuthorizationStatusAuthorizedAlways NS_ENUM_AVAILABLE(NA, 8_0),
// //使用此应用过程中允许访问定位服务
// kCLAuthorizationStatusAuthorizedWhenInUse NS_ENUM_AVAILABLE(NA, 8_0),
// //已经废弃
// kCLAuthorizationStatusAuthorized NS_ENUM_DEPRECATED(10_6, NA, 2_0, 8_0, "Use kCLAuthorizationStatusAuthorizedAlways") = kCLAuthorizationStatusAuthorizedAlways
//};
if ([CLLocationManager authorizationStatus]<3&&enable) {
//请求开启服务
[_locationManager requestWhenInUseAuthorization];
// [_locationManager requestAlwaysAuthorization];
}
else
{
// _locationManager=[[CLLocationManager alloc]init];
_locationManager.delegate=self;
// kCLLocationAccuracyBestForNavigation //导航级别
// kCLLocationAccuracyBest; //最精确定位
// kCLLocationAccuracyNearestTenMeters; //十米级定位
// kCLLocationAccuracyHundredMeters; //百米级定位
// kCLLocationAccuracyKilometer; //千米级定位
// kCLLocationAccuracyThreeKilometers; //3千米级定位
//设置精确度
_locationManager.desiredAccuracy=kCLLocationAccuracyBest;
//设置定位频率,每隔多少米定位一次
CLLocationDistance distance=10.0;
_locationManager.distanceFilter=distance;
//开始定位跟踪,开始后按照用户设定的更新频率更新
//(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;方法反馈定位信息
[_locationManager startUpdatingLocation];
if ([CLLocationManager headingAvailable]) {
[_locationManager startUpdatingHeading];
}
}
}
//参数newHeading是一个CLHeading对象。CLHeading通过一组属性来提供航向读数:magneticHeading和trueHeading。这些值的单位为度,类型为CLLocationDirection,即双精度浮点数
- (void)locationManager:(CLLocationManager *)manager
didUpdateHeading:(CLHeading *)newHeading
{
if(newHeading.headingAccuracy >=0)
{
//trueHeading和magneticHeading分别表示真实航向和磁性航向。如果位置服务被关闭了,GPS和wifi就只能获取magneticHeading(磁场航向)。只有打开位置服务,才能获取trueHeading(真实航向)。
NSString *headingDesc = [NSString stringWithFormat:@"%.0f degrees (true), %.0f degrees (magnetic)",newHeading.trueHeading,newHeading.magneticHeading];
NSLog(@"%@",headingDesc);
//关闭导航
[_locationManager stopUpdatingHeading];
}
}
//指定位置管理器是否向用户显示校准提示。该提示将自动旋转设备360°。由于指南针总是自我校准,因此这种提示仅在指南针读数剧烈波动时才有帮助。当设置为YES后,提示可能会分散用户的注意力,或影响用户的当前操作
- (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager
{
return YES;
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//获取位置
CLLocation *location=[locations lastObject];
//获取二维坐标经纬度 结构体类型 不用*
// typedef struct {
// CLLocationDegrees latitude;
// CLLocationDegrees longitude;
// } CLLocationCoordinate2D;
CLLocationCoordinate2D coordinate2D=location.coordinate;
NSLog(@"\n经度:%lf\n纬度:%lf\n海拔:%lf\n航向:%lf\n速度:%lf\n位置精度(半径负值无效):%lf\n海拔精度(负值无效)%lf",coordinate2D.longitude,coordinate2D.latitude,location.altitude,location.course,location.speed,location.horizontalAccuracy,location.verticalAccuracy);
//停止定位
[_locationManager stopUpdatingLocation];
}
//定位失败
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"%ld",error.code);
// error.code有以下枚举
// typedef NS_ENUM(NSInteger, CLError) {
// kCLErrorLocationUnknown = 0, // location is currently unknown, but CL will keep trying
// kCLErrorDenied, // Access to location or ranging has been denied by the user
// kCLErrorNetwork, // general, network-related error
// kCLErrorHeadingFailure, // heading could not be determined
// kCLErrorRegionMonitoringDenied, // Location region monitoring has been denied by the user
// kCLErrorRegionMonitoringFailure, // A registered region cannot be monitored
// kCLErrorRegionMonitoringSetupDelayed, // CL could not immediately initialize region monitoring
// kCLErrorRegionMonitoringResponseDelayed, // While events for this fence will be delivered, delivery will not occur immediately
// kCLErrorGeocodeFoundNoResult, // A geocode request yielded no result
// kCLErrorGeocodeFoundPartialResult, // A geocode request yielded a partial result
// kCLErrorGeocodeCanceled, // A geocode request was cancelled
// kCLErrorDeferredFailed, // Deferred mode failed
// kCLErrorDeferredNotUpdatingLocation, // Deferred mode failed because location updates disabled or paused
// kCLErrorDeferredAccuracyTooLow, // Deferred mode not supported for the requested accuracy
// kCLErrorDeferredDistanceFiltered, // Deferred mode does not support distance filters
// kCLErrorDeferredCanceled, // Deferred mode request canceled a previous request
// kCLErrorRangingUnavailable, // Ranging cannot be performed
// kCLErrorRangingFailure, // General ranging failure
// };
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
还有就是在模拟器中调试的问题:可以选中模拟器的Debug——>Location 来定义经纬度