iOS_城市定位

來源:互聯網
上載者:User

標籤:

技術點-利用CoreLoaction架構,實現城市定位功能。

原理:

利用蘋果官方提供的CoreLocation架構實現城市定位的功能;

該架構主要包括兩個比較有用的類:定位類CLLocationManager、地理編碼類ClGeoCoder。

代碼:

 

 1 //  Copyright © 2016年 劉勇虎. All rights reserved. 2 // 3  4 #import "ViewController.h" 5 #import <CoreLocation/CoreLocation.h> 6  7 @interface ViewController ()<CLLocationManagerDelegate> 8 @property(strong,nonatomic)CLLocationManager *locationMananger; 9 @property(strong,nonatomic)CLLocation *startPoint;10 @property(strong,nonatomic)CLGeocoder *geoCode;11 @property (weak, nonatomic) IBOutlet UILabel *cityName;12 13 @end14 15 @implementation ViewController16 //    開始定位17 - (IBAction)startLocationing:(UIButton *)sender {18         [_locationMananger startUpdatingLocation];19 }20 21 - (void)viewDidLoad {22     [super viewDidLoad];23     // Do any additional setup after loading the view, typically from a nib.24     25     26     _geoCode = [[CLGeocoder alloc]init];27     self.locationMananger = [[CLLocationManager alloc]init];28     _locationMananger.delegate = self;29     _locationMananger.desiredAccuracy = kCLLocationAccuracyBest;30     //    向系統申請許可權31     [_locationMananger requestWhenInUseAuthorization];32 33     34 }35 36 #pragma mark -- location delegate --37 //更新定位38 - (void)locationManager:(CLLocationManager *)manager39     didUpdateToLocation:(CLLocation *)newLocation40            fromLocation:(CLLocation *)oldLocation {41     42 //    反地理編碼43 [_geoCode reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {44     if (placemarks && placemarks.count > 0) {45         46         CLPlacemark *newPlaceMark = [placemarks firstObject];47         _cityName.text = newPlaceMark.locality;48         [_locationMananger stopUpdatingLocation];49     }50 }];51 }52 //定位失敗後回調53 -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{54     NSLog(@"error == %@",error);55 }

修改info.Plist檔案配置:

 

iOS_城市定位

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.