@property (nonatomic,retain)CLLocationManager * locationManager;
-(void)Positioning
{
self.locationManager = [[CLLocationManageralloc] init];
self.locationManager.delegate =self;
// 设置定位精度
// kCLLocationAccuracyNearestTenMeters:精度10米
// kCLLocationAccuracyHundredMeters:精度100米
// kCLLocationAccuracyKilometer:精度1000米
// kCLLocationAccuracyThreeKilometers:精度3000米
// kCLLocationAccuracyBest:设备使用电池供电时候最高的精度
// kCLLocationAccuracyBestForNavigation:导航情况下最高精度,一般要有外接电源时才能使用
self.locationManager.desiredAccuracy =kCLLocationAccuracyNearestTenMeters;
// distanceFilter是距离过滤器,为了减少对定位装置的轮询次数,位置的改变不会每次都去通知委托,而是在移动了足够的距离时才通知委托程序
//它的单位是米,这里设置为至少移动1000再通知委托处理更新;
self.locationManager.distanceFilter = 1000.0f;// 如果设为kCLDistanceFilterNone,则每秒更新一次;
if ([CLLocationManagerlocationServicesEnabled]) {
// 启动位置更新
//开启位置更新需要与服务器进行轮询所以会比较耗电,在不需要时用stopUpdatingLocation方法关闭;
[self.locationManagerstartUpdatingLocation];
else {
NSLog(@"请开启定位功能!");
}
}
#pragma mark - CLLocationManagerDelegate
// 地理位置发生改变时触发
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// 获取经纬度
NSLog(@"纬度:%f",newLocation.coordinate.latitude);
NSLog(@"经度:%f",newLocation.coordinate.longitude);
// 停止位置更新
NSString * lat = [NSStringstringWithFormat:@"%lf",newLocation.coordinate.latitude];
NSString * lon = [NSStringstringWithFormat:@"%lf",newLocation.coordinate.longitude];
[manager stopUpdatingLocation];
}
// 定位失误时触发
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"error:%@",error);
}