您的位置:首页 > 移动开发 > IOS开发

iOS反地理编码,lbs定位

2016-06-15 10:52 411 查看
#import <UIKit/UIKit.h>

#import <CoreLocation/CoreLocation.h>

#import "MainTabBarViewController.h"

#define appDelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate])

@interface AppDelegate :
UIResponder <UIApplicationDelegate,UIActionSheetDelegate,CLLocationManagerDelegate>

@property (nonatomic,
assign) double longitude;//经度

@property (nonatomic,
assign) double latitude;//纬度

@property (strong,
nonatomic) UIWindow *window;

#import "AppDelegate.h"

@interface
AppDelegate (){
   
//定位
   
CLLocationManager *_locationManager;//用于获取位置
   
CLLocation *_checkLocation;//用于保存位置信息

}

@end

@implementation AppDelegate

-(void)initMapLatToLong{

    _latitude =
LATITUDE_DEFAULT;

    _longitude =
LONGITUDE_DEFAULT;

    _locationManager = [[CLLocationManager
alloc] init];

    

    if ([CLLocationManager
locationServicesEnabled]) {
       
NSLog(@"开始定位");

        _locationManager.delegate =
self;

        // distanceFilter是距离过滤器,为了减少对定位装置的轮询次数,位置的改变不会每次都去通知委托,而是在移动了足够的距离时才通知委托程序

        //
它的单位是米,这里设置为至少移动1000再通知委托处理更新;

        _locationManager.distanceFilter = 200.0;

        // kCLLocationAccuracyBest:设备使用电池供电时候最高的精度

        _locationManager.desiredAccuracy =
kCLLocationAccuracyBest;

        

        

        //ios8+以上要授权,并且在plist文件中添加NSLocationWhenInUseUsageDescription,NSLocationAlwaysUsageDescription,值可以为空
       
if (IOS8 >=8.0) {//ios8+,不加这个则不会弹框

            [_locationManager
requestWhenInUseAuthorization];//使用中授权

            [_locationManager
requestAlwaysAuthorization];
        }

        [_locationManager
startUpdatingLocation];
    }else{

        NSLog(@"定位失败,请确定是否开启定位功能");

        //        _locationManager.delegate = self;

        //        // distanceFilter是距离过滤器,为了减少对定位装置的轮询次数,位置的改变不会每次都去通知委托,而是在移动了足够的距离时才通知委托程序

        //        //
它的单位是米,这里设置为至少移动1000再通知委托处理更新;

        //        _locationManager.distanceFilter = 200.0;

        //        // kCLLocationAccuracyBest:设备使用电池供电时候最高的精度

        //        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;

        //        [_locationManager startUpdatingLocation];
    }

    

    
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {

    // Override point for customization after application launch.

    [self
initMapLatToLong];

}

#pragma mark - CLLocationManagerDelegate

//舍弃了
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation
*)newLocation fromLocation:(CLLocation *)oldLocation{

    NSLog(@"didUpdateToLocation----");
   
_checkLocation = newLocation;
}

//ios 6.0sdk以上
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray
*)locations{

    NSLog(@"didUpdateToLocation+++");

    //此处locations存储了持续更新的位置坐标值,取最后一个值为最新位置,如果不想让其持续更新位置,则在此方法中获取到一个值之后让locationManager stopUpdatingLocation
   
CLLocation *cl = [locations
lastObject];

    _latitude = cl.coordinate.latitude;

    _longitude = cl.coordinate.longitude;

    NSLog(@"纬度--%f",_latitude);

    NSLog(@"经度--%f",_longitude);

    //
获取当前所在的城市名

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

    //根据经纬度反向地理编译出地址信息

    [geocoder reverseGeocodeLocation:cl
completionHandler:^(NSArray *array,
NSError *error){
       
if (array.count > 0){
           
CLPlacemark *placemark = [array
objectAtIndex:0];
           
//将获得的所有信息
          
NSString *loction =  placemark.name;
          
DDLogInfo(@"loction====%@",loction);
           
//获取城市
           
NSString *city = placemark.locality;
           
if (!city) {

                //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
                city = placemark.administrativeArea;
            }
           
NSLog(@"city = %@", city);
          
NSString *cityText = city;

//            [_cityButton setTitle:city forState:UIControlStateNormal];
           
DDLogInfo(@"cityText======%@",cityText);
        }
       
else if (error ==
nil && [array count] == 0)
        {

            NSLog(@"No results were returned.");
        }
       
else if (error !=
nil)
        {

            NSLog(@"An error occurred = %@", error);
        }
    }];

    //系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新

     [manager stopUpdatingLocation];
}

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

    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  lbs iOS 反地理编码