iOS定位功能
1.实现定位功能需要导入系统库MapKit.framework
2.在iPhone手机上默认是禁止手机定位的,所以,要询问系统是否开启手机定位功能。
为了开启手机定位功能,还需在info.plist中添加开启定位描述。如图,
3.功能代码如下:
// // ViewController.m // MapLocationDemo // // Created by apple on 15/12/3. // Copyright © 2015年 hoondraw. All rights reserved. // #import "ViewController.h" #import <CoreLocation/CoreLocation.h> #import "MapViewController.h" @interface ViewController () <CLLocationManagerDelegate> /** 定位管理类*/ @property (strong, nonatomic) CLLocationManager *mgr; /** 地理编码对象*/ @property (strong, nonatomic) CLGeocoder *geocoder; @property (weak, nonatomic) IBOutlet UILabel *latLabel; @property (weak, nonatomic) IBOutlet UILabel *longLabel; @property (weak, nonatomic) IBOutlet UILabel *geocoderLabel; /** 当前定位信息*/ @property (strong, nonatomic) CLLocation *currentLocation; @end @implementation ViewController - (CLLocationManager *)mgr { if (!_mgr) { _mgr = [[CLLocationManager alloc] init]; } return _mgr; } - (CLGeocoder *)geocoder { if (!_geocoder) { _geocoder = [CLGeocoder new]; } return _geocoder; } - (void)viewDidLoad { [super viewDidLoad]; self.mgr.delegate = self; if ([[UIDevice currentDevice].systemVersion doubleValue]> 8.0) { //只在前台定位 [self.mgr requestWhenInUseAuthorization]; } else { NSLog(@"iOS7"); //直接开始定位 [self.mgr startUpdatingLocation]; } } #pragma mark - 定位协议方法 - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { if (status == kCLAuthorizationStatusAuthorizedWhenInUse) { //定位的设置参数 self.mgr.desiredAccuracy = kCLLocationAccuracyBest; self.mgr.distanceFilter = kCLHeadingFilterNone; [self.mgr startUpdatingLocation]; } else { //不允许定位 } } - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations { CLLocation *location = [locations lastObject]; self.latLabel.text = [NSString stringWithFormat:@"经度:%lf", location.coordinate.latitude]; self.longLabel.text = [NSString stringWithFormat:@"纬度:%lf", location.coordinate.longitude]; //获取当前的location self.currentLocation = location; [self.mgr stopUpdatingLocation]; } /** 地理编码*/ - (IBAction)clickGecoder:(id)sender { NSString *name = @"广州市天河区天河珠江新城"; //从服务器获取name对应的经纬度(请求) [self.geocoder geocodeAddressString:name completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { if (!error) { //地理编码成功 for (CLPlacemark *placemark in placemarks) { self.latLabel.text = [NSString stringWithFormat:@"纬度:%lf",placemark.location.coordinate.latitude]; self.longLabel.text = [NSString stringWithFormat:@"经度:%lf",placemark.location.coordinate.longitude]; self.geocoderLabel.text = [NSString stringWithFormat:@"地址信息:%@",placemark.addressDictionary]; } } }]; } /** 反地理编码*/ - (IBAction)clickGeoDecoder:(id)sender { CLLocation *location = [[CLLocation alloc] initWithLatitude:self.currentLocation.coordinate.latitude longitude:self.currentLocation.coordinate.longitude]; [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { if (!error) { //反地理编码成功 for (CLPlacemark *placemark in placemarks) { self.latLabel.text = [NSString stringWithFormat:@"纬度:%lf",placemark.location.coordinate.latitude]; self.longLabel.text = [NSString stringWithFormat:@"经度:%lf",placemark.location.coordinate.longitude]; self.geocoderLabel.text = [NSString stringWithFormat:@"地址信息:%@",placemark.addressDictionary]; } } }]; } - (IBAction)gotoMapView:(id)sender { MapViewController *vc = [[MapViewController alloc] init]; [self presentViewController:vc animated:YES completion:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; }
其中,gotoMapView是跳转到地图控制器的方法。
代码如下:
// // MapViewController.m // MapLocationDemo // // Created by apple on 15/12/3. // Copyright © 2015年 hoondraw. All rights reserved. // #import "MapViewController.h" #import <MapKit/MapKit.h> @interface MapViewController () <MKMapViewDelegate> @property (weak, nonatomic) IBOutlet MKMapView *mapView; @property (strong, nonatomic) CLLocationManager *mgr; @end @implementation MapViewController - (void)viewDidLoad { [super viewDidLoad]; self.mgr = [[CLLocationManager alloc] init]; [self.mgr requestWhenInUseAuthorization]; //设置和地图相关的属性 self.mapView.delegate = self; //设置地图的旋转属性 self.mapView.rotateEnabled = YES; //使用地图视图开始定位 self.mapView.userTrackingMode = MKUserTrackingModeFollow; } #pragma mark - MKMapViewDelegate - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { //地图显示的区域 //设置地图的中心 CLLocationCoordinate2D center = userLocation.location.coordinate; //设置跨度 MKCoordinateSpan span = MKCoordinateSpanMake(5, 5); MKCoordinateRegion region = MKCoordinateRegionMake(center, span); [self.mapView setRegion:region animated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
这样就可以跳转到地图中显示当前地理位置。