软件开发者

IOS CLLocationManager定位反编码位置信息

//获取位置和坐标
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
        if (IOS_VERSION >= 8.0) {
            //由于IOS8中定位的授权机制改变 需要进行手动授权
            //获取授权认证
            [locationManager requestAlwaysAuthorization];
            [locationManager requestWhenInUseAuthorization];
        }
#else
#endif
        if ([CLLocationManager locationServicesEnabled] == YES) {
            [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
            [locationManager setDistanceFilter:kCLDistanceFilterNone];
            [locationManager startUpdatingLocation];
        } else {
            [self clearLocation];
        }

 

 

#pragma mark CLLocationManagerDelegate Method

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    debugLog(@"获取到定位信息");
    CLLocation *cllocation = [locations objectAtIndex:0];
    NSString *longtitudeStr = [NSString stringWithFormat:@"%f", cllocation.coordinate.longitude];
    NSString *latitudeStr = [NSString stringWithFormat:@"%f", cllocation.coordinate.latitude];
    [manager stopUpdatingLocation];
    
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:cllocation completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error == nil) {
            if ([Common isEmptyArray:placemarks] == NO) {
                CLPlacemark *placemark = [placemarks objectAtIndex:0];
                NSString *administrativeArea = placemark.administrativeArea;
                NSString *city = placemark.locality;
                NSString *subLocality = placemark.subLocality;
                NSString *thoroughfare = placemark.thoroughfare;
                if ([Common isEmptyString:administrativeArea] == YES) {
                    administrativeArea = @"";
                }
                if ([Common isEmptyString:city] == YES) {
                    city = @"";
                }
                if ([Common isEmptyString:subLocality] == YES) {
                    subLocality = @"";
                }
                if ([Common isEmptyString:thoroughfare] == YES) {
                    thoroughfare = @"";
                }
                NSString *locationStr = [administrativeArea stringByAppendingString:city];
                locationStr = [locationStr stringByAppendingString:subLocality];
                locationStr = [locationStr stringByAppendingString:thoroughfare];
                
                [self updateLongtitude:longtitudeStr];
                [self updateLatitude:latitudeStr];
                [self updateLocation:locationStr];
            } else {
                [self clearLocation];
            }
        } else {
            debugLog(@"获取定位信息失败, 失败原因 = %@", error.localizedDescription);
            [self clearLocation];
        }
        [manager setDelegate:nil];
    }];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    debugLog(@"获取定位信息失败, 失败原因 = %@", error.localizedDescription);
    [self clearLocation];
}

posted @ 2015-05-21 11:49  罗森尼  阅读(478)  评论(0)    收藏  举报