//维度:loc.coordinate.latitude
//经度:loc.coordinate.longitude
NSLog(@"纬度=%f,经度=%f",loc.coordinate.latitude,loc.coordinate.longitude);
NSLog(@"%d",locations.count);
//停止更新位置(如果定位服务不需要实时更新的话,那么应该停止位置的更新)
// [self.locMgr stopUpdatingLocation];
}
//计算两个位置之间的距离
-(void)countDistance
{
//根据经纬度创建两个位置对象
CLLocation *loc1=[[CLLocation alloc]initWithLatitude:40 longitude:116];
CLLocation *loc2=[[CLLocation alloc]initWithLatitude:41 longitude:116];
//计算两个位置之间的距离
CLLocationDistance distance=[loc1 distanceFromLocation:loc2];
NSLog(@"(%@)和(%@)的距离=%fM",loc1,loc2,distance);
}
@end
打印查看:
代码说明:
1.关于代理方法
需要设置代理,通过代理告诉用户当前的位置,有两个代理方法:
locations参数里面装着CLLocation对象
其中后者是一个过期的方法,在新的方法(第一个)中使用了一个数组来替代。
说明:该方法在当定位到用户的位置时就会调用,调用比较频繁
注意:不要使用局部变量(创建位置管理器),因为局部变量的方法结束它就被销毁了。建议使用一个全局的变量,且只创建一次就可以了(使用懒加载)。
2.定位的精度












