iOS中利用系統地圖獲取定位信息(經緯度/地理位置)

首先在plist文件中

但是對於8.0之後的9.0之間的版本有時在設置中更改定位權限會閃退  改成下面的就好啦


然後在.h文件中:

//

//  CCLocationManager.h

//  MMLocationManager

//

//

//  Created by GuoFeng on 16/4/14.

//  Copyright © 2016 WXDL. All rights reserved.

//


#import <Foundation/Foundation.h>

#import <MapKit/MapKit.h>

#import <CoreLocation/CoreLocation.h>


#define  CCLastLongitude @"CCLastLongitude"

#define  CCLastLatitude  @"CCLastLatitude"

#define  CCLastCity      @"CCLastCity"

#define  CCLastAddress   @"CCLastAddress"


typedef void (^LocationBlock)(CLLocationCoordinate2D locationCorrrdinate);

typedef void (^LocationErrorBlock) (NSError *error);

typedef void(^NSStringBlock)(NSString *cityString);

typedef void(^NSStringBlock)(NSString *addressString);


@interface CCLocationManager :NSObject<CLLocationManagerDelegate>

@property (nonatomic)CLLocationCoordinate2D lastCoordinate;

@property(nonatomic,strong)NSString *lastCity;

@property (nonatomic,strong)NSString *lastAddress;


@property(nonatomic,assign)float latitude;

@property(nonatomic,assign)float longitude;



+ (CCLocationManager *)shareLocation;


/**

 *  獲取座標

 *

 *  @param locaiontBlock locaiontBlock description

 */

- (void) getLocationCoordinate:(LocationBlock) locaiontBlock ;


/**

 *  獲取座標和詳細地址

 *

 *  @param locaiontBlock locaiontBlock description

 *  @param addressBlock  addressBlock description

 */

- (void) getLocationCoordinate:(LocationBlock) locaiontBlock  withAddress:(NSStringBlock) addressBlock;


/**

 *  獲取詳細地址

 *

 *  @param addressBlock addressBlock description

 */

- (void) getAddress:(NSStringBlock)addressBlock;


/**

 *  獲取城市

 *

 *  @param cityBlock cityBlock description

 */

- (void) getCity:(NSStringBlock)cityBlock;


///**

// *  獲取城市和定位失敗

// *

// *  @param cityBlock  cityBlock description

// *  @param errorBlock errorBlock description

// */

//- (void) getCity:(NSStringBlock)cityBlock error:(LocationErrorBlock) errorBlock;








@end

在.m文件中

//

//  CCLocationManager.m

//  MMLocationManager

//

//

//  Created by GuoFeng on 16/4/14.

//  Copyright © 2016 WXDL. All rights reserved.

//


#import "CCLocationManager.h"

@interface CCLocationManager (){

    CLLocationManager *_manager;


}

@property (nonatomic,strong) LocationBlock locationBlock;

@property (nonatomic,strong) NSStringBlock cityBlock;

@property (nonatomic,strong) NSStringBlock addressBlock;

@property (nonatomic,strong) LocationErrorBlock errorBlock;


@end


@implementation CCLocationManager



+ (CCLocationManager *)shareLocation{

    staticdispatch_once_t pred = 0;

    __strongstatic id _sharedObject =nil;

    dispatch_once(&pred, ^{

        _sharedObject = [[self alloc] init];

    });

    return _sharedObject;

}


- (id)init {

    self = [super init];

    if (self) {

        NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];

        

        float longitude = [standard floatForKey:CCLastLongitude];

        float latitude = [standard floatForKey:CCLastLatitude];

        self.longitude = longitude;

        self.latitude = latitude;

        self.lastCoordinate = CLLocationCoordinate2DMake(longitude,latitude);

        self.lastCity = [standard objectForKey:CCLastCity];

        self.lastAddress=[standard objectForKey:CCLastAddress];

    }

    returnself;

}

//獲取經緯度

- (void) getLocationCoordinate:(LocationBlock) locaiontBlock

{

    self.locationBlock = [locaiontBlock copy];

    [self startLocation];

}


- (void) getLocationCoordinate:(LocationBlock) locaiontBlock  withAddress:(NSStringBlock) addressBlock

{

    self.locationBlock = [locaiontBlock copy];

    self.addressBlock = [addressBlock copy];

    [self startLocation];

}


- (void) getAddress:(NSStringBlock)addressBlock

{

    self.addressBlock = [addressBlock copy];

    [self startLocation];

}

//獲取省市

- (void) getCity:(NSStringBlock)cityBlock

{

    self.cityBlock = [cityBlock copy];

    [self startLocation];

}


//- (void) getCity:(NSStringBlock)cityBlock error:(LocationErrorBlock) errorBlock

//{

//    self.cityBlock = [cityBlock copy];

//    self.errorBlock = [errorBlock copy];

//    [self startLocation];

//}

#pragma mark CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

{

    

    NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];

    


    CLGeocoder *geocoder=[[CLGeocoder alloc]init];

    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks,NSError *error)

     {

         if (placemarks.count >0) {

             CLPlacemark *placemark = [placemarks objectAtIndex:0];

             _lastCity = [NSString stringWithFormat:@"%@%@",placemark.administrativeArea,placemark.locality];

             [standard setObject:_lastCity forKey:CCLastCity];//省市地址

             NSLog(@"______%@",_lastCity);

             

             


             _lastAddress = [NSString stringWithFormat:@"%@%@%@%@%@%@",placemark.country,placemark.administrativeArea,placemark.locality,placemark.subLocality,placemark.thoroughfare,placemark.subThoroughfare];//詳細地址

             if (placemark.subThoroughfare.length==0) {

                 

                 _lastAddress = [NSString stringWithFormat:@"%@%@%@%@%@",placemark.country,placemark.administrativeArea,placemark.locality,placemark.subLocality,placemark.thoroughfare];

                 

             }

             

             

             

             NSLog(@"______%@",_lastAddress);



         }

         if (_cityBlock) {

             _cityBlock(_lastCity);

             _cityBlock = nil;

         }

         if (_addressBlock) {

             _addressBlock(_lastAddress);

             _addressBlock = nil;

         }


         

     }];

    

    _lastCoordinate = CLLocationCoordinate2DMake(newLocation.coordinate.latitude ,newLocation.coordinate.longitude );

    if (_locationBlock) {

        _locationBlock(_lastCoordinate);

        _locationBlock = nil;

    }


    NSLog(@"%f--%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);

    [standard setObject:@(newLocation.coordinate.latitude) forKey:CCLastLatitude];

    [standard setObject:@(newLocation.coordinate.longitude) forKey:CCLastLongitude];


    [manager stopUpdatingLocation];

    

}



-(void)startLocation

{

    if([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)

    {

        _manager=[[CLLocationManager alloc]init];

        _manager.delegate=self;

        _manager.desiredAccuracy = kCLLocationAccuracyBest;

        [_manager requestAlwaysAuthorization];

        _manager.distanceFilter=100;

        [_manager startUpdatingLocation];

    }

    else

    {

        UIAlertView *alvertView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"需要開啓定位服務,請到設置->隱私,打開定位服務" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles: nil];

        [alvertView show];

        

    }

    

}

- (void)locationManager:(CLLocationManager *)manager

       didFailWithError:(NSError *)error{

    [self stopLocation];


}

-(void)stopLocation

{

    _manager = nil;

}



@end


最後在自己需要用的試圖控制器中導入頭文件

#import "CCLocationManager.h"

在需要用的地方:

-(void)addressInfo

{


    if (IS_IOS8) {

        [UIApplicationsharedApplication].idleTimerDisabled =TRUE;

        locationmanager = [[CLLocationManageralloc] init];

        [locationmanagerrequestAlwaysAuthorization];       //NSLocationAlwaysUsageDescription

        [locationmanagerrequestWhenInUseAuthorization];    //NSLocationWhenInUseDescription

        locationmanager.delegate =self;

    }

    //__block __weak MainTabbarController *wself = self;

    

    __blockNSString *string;

    

    

    if (IS_IOS8) {

        

        [[CCLocationManagershareLocation]getLocationCoordinate:^(CLLocationCoordinate2D locationCorrrdinate) {

            string = [NSStringstringWithFormat:@"%f %f",locationCorrrdinate.latitude,locationCorrrdinate.longitude];

            _lat=[NSStringstringWithFormat:@"%f",locationCorrrdinate.latitude];

            _long=[NSStringstringWithFormat:@"%f",locationCorrrdinate.longitude];

        } withAddress:^(NSString *addressString) {

            NSLog(@"+++++++++%@",addressString);

            string = [NSStringstringWithFormat:@"%@\n%@",string,addressString];

            // [wself setLabelText:string];

            _addressinfo=addressString;

            

            }

            

            

        }];

    }


}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章