• 简易的IOS位置定位服务


    有时一些小的需求,其实只是需要得知当前IOS APP使用的地点,有些只是想精确到城市级别,并不需要任何地图。

    有了以下的简易实现:

    @interface MainViewController ()<CLLocationManagerDelegate>

    ....

    @end

     

    @implementation MainViewController

    - (void)InitLocation {

        //初始化定位服务管理对象

        self.locationManager = [[CLLocationManager alloc] init];

        self.locationManager.delegate = self;

        self.locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;

        self.locationManager.distanceFilter = 1000.0f;

    }

    /*

    其中位置的精确性有如下几个级别,越是精确,程序回调就越慢!当前的例子只是到城市级别,所以使用kCLLocationAccuracyThreeKilometers

    extern const CLLocationAccuracy kCLLocationAccuracyBestForNavigation __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0);

    extern const CLLocationAccuracy kCLLocationAccuracyBest;

    extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;

    extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters;

    extern const CLLocationAccuracy kCLLocationAccuracyKilometer;

    extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers;

    */

    - (void) viewWillAppear:(BOOL)animated{

        //开始定位

        if(isSetAddress == FALSE){

            dispatch_async(dispatch_get_main_queue(), ^{

                [self InitLocation];

                [self.locationManager requestWhenInUseAuthorization];

                [self.locationManager startUpdatingLocation];

            });

        }

    }

     

    #pragma mark Core Location委托方法用于实现位置的更新,可以得到经纬度,省、城市、街道

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:

    (NSArray *)locations

    {

        CLLocation * currLocation = [locations lastObject];

        

        NSLog(@"latitude=%3.5f, longitude=%3.5f, altitude=%3.5f", currLocation.coordinate.latitude, currLocation.coordinate.longitude, currLocation.altitude);

        

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

        [geocoder reverseGeocodeLocation:currLocation

                       completionHandler:^(NSArray *placemarks, NSError *error) {

                           if ([placemarks count] > 0) {

                               CLPlacemark *placemark = placemarks[0];

                               NSDictionary *addressDictionary = placemark.addressDictionary;

                               NSString *address = [addressDictionary objectForKey:(NSString *) kABPersonAddressStreetKey];

                               address = address == nil ? @"": address;

                               NSString *state = [addressDictionary objectForKey:(NSString *) kABPersonAddressStateKey];

                               state = state == nil ? @"": state;

                               NSString *city = [addressDictionary objectForKey:(NSString *) kABPersonAddressCityKey];

                               city = city == nil ? @"": city;

                               NSLog(@"Place description: %@ %@ %@",state, address,city);

                               

                               self.userAddress = [[NSString alloc] initWithFormat:@"%@", city];

                               dispatch_async(dispatch_get_main_queue(), ^{

                                   NSLog(@"SetLiveShowAddress start");

                                   int ret = SetLiveShowAddress(city, address);

                                   if(ret == 0){

                                       isSetAddress = TRUE;

                                       [self.locationManager stopUpdatingLocation];

                                   }

                                   NSLog(@"SetLiveShowAddress end");

                               });

                           }

                       }];

    }

     

    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

    {

        NSLog(@"error: %@",error);

    }

    @end

  • 相关阅读:
    iOS 相册相机应用2
    运行时啊
    在iOS开发的Quartz2D使用中实现图片剪切和截屏功能
    内购
    自制数据挖掘工具分析北京房价 (二) 数据清洗
    数据挖掘工具分析北京房价 (一) 数据爬取采集
    程序员讨论 《黑客帝国》 (三) 情感
    程序员讨论《黑客帝国》(二)平衡和进化
    程序员讨论《黑客帝国》(一)真实与虚拟
    类库与框架,强类型与弱类型的闲聊
  • 原文地址:https://www.cnblogs.com/runner42/p/4638836.html
Copyright © 2020-2023  润新知