iOS获取定位信息

//
//  MyLocationManager.h
//  阿明
//
//  Created by 阿明 on 15/8/5.
//  Copyright (c) 2015年 阿明. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

@interface MyLocationManager : NSObject<CLLocationManagerDelegate>
@property (nonatomic,strong) CLLocationManager *locationManager;

@property (nonatomic,copy) NSString *country; //国家 中国
@property (nonatomic,copy) NSString *city; //city 北京市市辖区
@property (nonatomic,copy) NSString *province; //直辖区 北京市
@property (nonatomic,copy) NSString *thoroughfare; //街道 东直门南小街
@property (nonatomic,copy) NSString *address;

@property (nonatomic,assign) BOOL  isLocationed;

+ (HHLocationManager *)sharedHHLocationManager;


-(BOOL)isCanUseLocation;


@property(copy)void(^onLocationEnd)();

@end

//
//  MyLocationManager.m
//  Pacs
//
//  Created by liumingming on 15/8/5.
//  Copyright (c) 2015年 阿明. All rights reserved.
//

#import "MyLocationManager.h"

@implementation MyLocationManager

+ (MyLocationManager *)sharedHHLocationManager
{
    static MyLocationManager *sharedLocationManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedLocationManager = [[self alloc] init];
    });
    return sharedLocationManager;
}

- (id)init
{
    if (self = [super init])
    {
        if ([CLLocationManager locationServicesEnabled])
        {
            self.locationManager = [[CLLocationManager alloc] init];
            self.locationManager.delegate = self;
//            [self.locationManager requestAlwaysAuthorization];//始终
            [self.locationManager requestWhenInUseAuthorization];//只有在用的时候才使用
            self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
            self.locationManager.distanceFilter = 1000.0f;
//            [self.locationManager startUpdatingLocation];


        }

        _isLocationed = YES;
    }
    return self;
}


- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    if (status == kCLAuthorizationStatusAuthorizedAlways || status ==  kCLAuthorizationStatusAuthorizedWhenInUse)
    {
        NSLog(@"用户授权成功");

        [self.locationManager startUpdatingLocation];
    }
    else if(status == kCLAuthorizationStatusDenied)
    {
        NSLog(@"用户拒绝授权");
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *currentLocation = [locations lastObject];
//    NSLog(@"%f_____%f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);

    CLGeocoder* myGecoder = [[CLGeocoder alloc] init];
    [myGecoder reverseGeocodeLocation:currentLocation
                    completionHandler:^(NSArray *placemarks, NSError *error)
    {
             if(error == nil && [placemarks count]>0)
             {
                 CLPlacemark *placemark = [placemarks objectAtIndex:0];
                 self.city = placemark.locality;
                 if (!self.city)
                 {
                     self.city = placemark.administrativeArea;
                 }
                 self.province = placemark.administrativeArea;
                 self.country = placemark.country;
                 self.thoroughfare = placemark.thoroughfare;
                 self.address = placemark.name;
//                 NSLog(@"Country = %@", placemark.country);
//                 NSLog(@"Postal Code = %@", placemark.postalCode);
//                 NSLog(@"Locality = %@", placemark.locality);
                 NSLog(@"address = %@",placemark.name);
//                 NSLog(@"administrativeArea = %@",placemark.administrativeArea);
//                 NSLog(@"subAdministrativeArea = %@",placemark.subAdministrativeArea);
//                 NSLog(@"locality = %@", placemark.locality);
//                 NSLog(@"thoroughfare = %@", placemark.thoroughfare);
//                 NSLog(@"%@",[placemark description]);
             }
             else if(error==nil && [placemarks count]==0)
             {
//                 NSLog(@"No results were returned.");
             }
             else if(error != nil)
             {
//                 NSLog(@"An error occurred = %@", error);
             }
         }];
       [self.locationManager stopUpdatingLocation];

}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"定位失败");

}


-(BOOL)isCanUseLocation
{
    return _country&&_province&&_city;
}


@end

0 0 投票数
文章评分
订阅评论
提醒
guest
0 评论
内联反馈
查看所有评论
京ICP备17066706号-1
0
希望看到您的想法,请您发表评论x