//1.导入头文件 #import <CoreLocation/CoreLocation.h> //2.设置代理CLLocationManagerDelegate @interface ShopMyInfoViewController ()<CLLocationManagerDelegate> //3.viewDidLoad中调用getJingWeiDu方法 //4、自动调代理方法 locationManager // 经纬度 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"经度%f,纬度%f",newLocation.coordinate.longitude, newLocation.coordinate.latitude); [[UserModel sharedInstanced]setlongitude:[NSString stringWithFormat:@"%f",newLocation.coordinate.longitude]]; [[UserModel sharedInstanced]setlatitude:[NSString stringWithFormat:@"%f",newLocation.coordinate.latitude]]; //一直定位 [manager stopUpdatingLocation]; } - (void)getJingWeiDu{ self.locationManager=[[CLLocationManager alloc]init]; //期望精度 self.locationManager.desiredAccuracy=kCLLocationAccuracyBest; //大约变化10米更新一次 // self.locationManager.distanceFilter=10; //认证NSLocationAlwaysUsageDescription if([[UIDevice currentDevice] systemVersion].doubleValue>8.0)//如果ios是8.0以上版本 { if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])//位置管理对象中有requestAlwaysAuthorization这个行为 { //运行 [self.locationManager requestAlwaysAuthorization]; } } self.locationManager.delegate=self; [self.locationManager startUpdatingLocation]; }