zoukankan      html  css  js  c++  java
  • 简易的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

  • 相关阅读:
    数据中的悖论
    Exchange server2007自动发现服务(Auto discover service)原理及调试
    Exchange 2007 安装完后需要注意的几件事情
    有感
    Exchange Server 2007 LCR小试
    【转】Vmware ESX 3.0出现“error connecting: can not connect to host x.x.x.x: a connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed……的解决方法
    学习Exchange的几个站点
    在Exchange server 2007中启用pop3和IMAP4协议访问
    Exchange server 2007环境下,outlook2007同步脱机地址簿时出现“0x80190194”错误的分析与解决方法
    安装isa2006后加入域提示“RPC服务器不可用”
  • 原文地址:https://www.cnblogs.com/runner42/p/4638836.html
Copyright © 2011-2022 走看看