您的当前位置:首页正文

功能-定位(六步实现定位)

来源:化拓教育网

第一步: 在Info.plist中添加以下两个键,iOS8新增的定位提示语,值可以为空,但必须添加,否则定位没有回调
  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription
    <p>
第二步: 导入头文件

#import <CoreLocation/CoreLocation.h>

<p>

第三步:添加代理

<CLLocationManagerDelegate>

<p>

第四步:声明定位管理者对象

CLLocationManager* locationManager;// 定位管理者

<p>

第五步:实现定位方法

<p>

//判断是否是模拟器的宏
#define isSimulator ([[UIDevice currentDevice].model isEqualToString:@"iPhone Simulator"] || [[UIDevice currentDevice].model isEqualToString:@"iPad Simulator"])
/*
 * 在模拟器中是无法开启定位的,为了实现在模拟器中调用定位服务,重写startUpdatingLocation方法模拟定位
 * 在真机中屏蔽该方法
 */
- (void)startUpdatingLocationInSimulator {
    #if TARGET_IPHONE_SIMULATOR
    float latitude = 32.061;
    float longitude = 118.79125;
    CLLocation *setLocation= [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
    [[self delegate]locationManager:self didUpdateToLocation:setLocation fromLocation:nil];
    #endif
}
// 开始定位
-(void)startLocation {
    locationManager = [[CLLocationManager alloc] init];
    if([CLLocationManager locationServicesEnabled]){
        // 设置代理
        locationManager.delegate = self;
        // 设置更新地理信息的最短距离,单位米
        locationManager.distanceFilter = 10.0f;
        // 设置GPS精确度
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        // iOS8新增方法,必须在startUpdatingLocation前调用,检查NSLocationWhenInUseUsageDescription是否添加进Info.plist
        if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
            [locationManager requestWhenInUseAuthorization];
        }
        if (isSimulator) {// 如果是在模拟器,调用模拟地点
            [locationManager startUpdatingLocationInSimulator];
        }
        else {
            [locationManager startUpdatingLocation];
        }
    }
    else {// 未开启定位
        [self hideActivityIndicator];
    }
}

// 停止定位
- (void)stopLocation {
    [locationManager stopUpdatingLocation];
    locationManager = nil;
}

GPS精度从高到低(越精确越耗电)

  • kCLLocationAccuracyBestForNavigation // (最精确)
  • kCLLocationAccuracyBest
  • kCLLocationAccuracyNearestTenMeters
  • kCLLocationAccuracyHundredMeters
  • kCLLocationAccuracyKilometer
  • kCLLocationAccuracyThreeKilometers
第六步:实现代理方法
// 定位代理经纬度回调
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    [self hideActivityIndicator];// 停止菊花
    [self stopLocation];
    [self getlocationFromCLLocation:newLocation];
    
}

// iOS6新增回调方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    [self hideActivityIndicator];// 停止菊花
    [self stopLocation];
    [self getlocationFromCLLocation:[locations lastObject]];
}

// 地理信息反编码
- (void)getlocationFromCLLocation:(CLLocation *)location {
    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
    [geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error){
        for (CLPlacemark * placemark in placemarks) {
            // 地理信息的Dictionary
            // Country(国家,例:中国)
            // State  (省,例:广东省)
            // City   (市,例:广州市)
            // SubLocality(区,例:海珠区)
            // Street     (街道,例:珠江东路)
            // Thoroughfare(大道,例:珠江东路)
            // Name  (地址,例:中国广东省广州市天河区冼村街道珠江东路)
            // FormattedAddressLines(地址编码,转码后的地址)
            // CountryCode(国家编码,例:CN)
            NSDictionary *test = [placemark addressDictionary];
            // TODO
        }
    }];
}

更多信息