今天重新看了看coreLocation框架 ,自己简单的在Xcode敲了一遍,然后分享过来给各位看看.上马---->
//CLLOcationManager的常用操作和属性
//开始用户定位 - (void)startUpdatingLocation;
//停止用户定位 - (void)stopUpdatingLocation;
//说明:当用户调用了startUpdatingLocation方法后,就开始不断的定位用户的位置,会不断的调用一个方法:
//-(void)LocationManager:(cllocationManager *)manager diduplocations:(NSArray *)locations;
//Cllocation的简单用法
//collocation用来表示某一个用户的位置,经纬度,海拔等;
//(1)经纬度
//@property(readonly nonatomic )COLLocationCoordinate2D coordinate;
//(2)海拔
//@property(readonly nonatomic)CLLocationDistance altitude;
//(3)路线,航向(取值范围是0.0° ~ 359.9°,0.0°代表真北方向)
//@property(readonly, nonatomic) CLLocationDirection course;
//(4)行走速度(单位是m/s)
// @property(readonly, nonatomic) CLLocationSpeed speed;
//(5)计算2个位置之间的距离
// - (CLLocationDistance)distanceFromLocation:(const CLLocation *)location方法
//3.CLLocationCoordinate2D
//
//CLLocationCoordinate2D是一个用来表示经纬度的结构体,定义如下
//
//typedef struct {
//
// CLLocationDegrees latitude; // 纬度
//
// CLLocationDegrees longitude; // 经度
//
//} CLLocationCoordinate2D;
//
//一般用CLLocationCoordinate2DMake函数来创建CLLocationCoordinate2D
// Created by 晗晗 on 15/3/18.
// Copyright (c) 2015年 晗晗哥. All rights reserved.
//
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
//这样我们需要遵循CLLocationManagerDelegate
@interface ViewController ()<CLLocationManagerDelegate>
@property(nonatomic, retain)CLLocationManager * clmg;
@end
@implementation ViewController
#pragma mark - 懒加载
-(CLLocationManager *)clmg
{
if (_clmg == nil) {
//创建位置管理器(定位用户的位置)
self.clmg = [[CLLocationManager alloc]init];
//2.设置代理
self.clmg.delegate = self;
}
return _clmg;
}
- (void)viewDidLoad {
[super viewDidLoad];
//判断用户定位是否开启
if ([CLLocationManager locationServicesEnabled]) {
//开始定位用户的位置
[self.clmg startUpdatingLocation];
//每隔多少米定位一次(这里的设置为任何的移动)
self.clmg.distanceFilter = kCLDistanceFilterNone;
//设置定位精确度,一般精确度越高,越费电(一般用在导航这边)
self.clmg.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
}else
{
//不能定位用户的位置
//1.提示用户检查当前的网络状况
//2.提醒用户打开定位开关
}
//测试方法,计算俩个位置之间的距离
[self countDistance];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark - CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//collocation数组里面存放的时CLLocation对象,一个collocation就代表一个位置
CLLocation * loc = [locations firstObject];
//纬度: loc.coordinate.latitude
//经度: loc.coordinate.longitude
NSLog(@"我现在的纬度 = %f,经度 = %f",loc.coordinate.latitude,loc.coordinate.longitude);
NSLog(@"%lu",(unsigned long)locations.count);
//停止跟新
[self.clmg stopUpdatingLocation];
}
//计算俩个位置之间的距离
-(void)countDistance
{
//根据经纬度创建俩个对象
CLLocation * loc1 = [[CLLocation alloc]initWithLatitude:40 longitude:122];
CLLocation * loc2 = [[CLLocation alloc]initWithLatitude:41 longitude:122];
//计算俩个位置之间的距离
CLLocationDistance distance = [loc1 distanceFromLocation:loc2];
NSLog(@"(%@)和(%@)的距离是 = %fM",loc1,loc2,distance);
}