1. 首先在info.plist文件中设置
NSLocationAlwaysUsageDescription string 我们需要您当前所在位置喔 //始终获取
NSLocationWhenInUseUsageDescription string 我们需要您当前所在位置喔 //在使用应用期间
2. 设置系统和百度的定位方式
1) 系统定位
//头文件中加入delegate
@property (nonatomic,retain)CLLocationManager *locationManager;
//.m文件中创建
//iOS自带定位,获取经纬度
- (void)createLocationManage
{
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 10;
[CLLocationManager locationServicesEnabled];
if ([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) {
[self.locationManager requestAlwaysAuthorization];//如果想让ios8获取当前地址,需要添加此代码
}
[self.locationManager startUpdatingLocation];
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
NSLog(@"定位不可用");
[self shopsList:@" " setLat:@" "];
}
}
#pragma mark CLLocationManager
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *currLocation = [locations lastObject];
localLongitude = currLocation.coordinate.longitude;
localLatitude = currLocation.coordinate.latitude;
NSString *localLongitudeStr = [NSString stringWithFormat:@"%f",localLongitude]; //经度
NSString *localLatitudeStr = [NSString stringWithFormat:@"%f",localLatitude]; //维度
//将经纬度保存到SharedInfo中
SharedInfo *sharedInfo = [SharedInfo sharedDataInfo];
sharedInfo.localLongitude = localLongitudeStr;
sharedInfo.localLatitude = localLatitudeStr;