IOS 地图

2017-03-13  本文已影响18人  HCong

定位服务是通过gps等方式查找定位自己的位置的,再通过地图标注出来。
在ios中定位服务和地图开发是使用两套api分别完成的。

位置信息获取:

ios系统开发提供3种不同定位途径:

定位:

要实现地图、导航功能,往往需要先熟悉定位功能,在iOS中通过Core Location框架进行定位操作。Core Location自身可以单独使用,和地图开发框架MapKit完全是独立的,但是往往地图开发要配合定位框架使用。在Core Location中主要包含了定位、地理编码(包括反编码)功能。

#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <MKMapViewDelegate, CLLocationManagerDelegate>
@property (nonatomic, weak) MKMapView *mpview;
// 定位管理者必须为强引用,避免在viewDidLoad方法过后被释放导致定位失败
@property (nonatomic, strong) CLLocationManager *manager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    MKMapView *mpview = [[MKMapView alloc] init];
    self.mpview = mpview;
    mpview.showsUserLocation = YES;
    self.mpview.frame = self.view.bounds;
    self.mpview.delegate = self;
    [self.view addSubview:self.mpview];
    
    CLAuthorizationStatus authorizationStatus = [CLLocationManager authorizationStatus];
    self.manager = [[CLLocationManager alloc] init];
     //设置定位精度
    self.manager.desiredAccuracy=kCLLocationAccuracyBest;

    //定位频率,每隔多少米定位一次
    CLLocationDistance distance=10.0;
    //十米定位一次
    self.manager.distanceFilter=distance;

    self.manager.delegate = self;
    if ([CLLocationManager locationServicesEnabled]) { // 定位服务开启
        switch (authorizationStatus) { // 定位服务授权
            case kCLAuthorizationStatusNotDetermined: // 未决定(app首次打开)
            case kCLAuthorizationStatusRestricted: //  受限制的
            case kCLAuthorizationStatusDenied:   // 被拒绝
            {
                // 跳转到app设置界面进行授权
                // UIApplicationOpenSettingsURLString(ios >= 8.0)
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:^(BOOL success) {
                    NSLog(@"Open URL success");
                }];
                break;
            }
                
            default:  // 已授权允许使用定位服务
                break;
        }
        // 开始定位
        [self.manager startUpdatingLocation];
    } else {
        // 开启GPS定位服务
    }
    
    
}

// 定位成功代理方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    CLLocation *location = locations.firstObject;
    CLLocationCoordinate2D coordinate = location.coordinate;
    NSLog(@"%f------%f",coordinate.latitude, coordinate.longitude);
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:locations.firstObject completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        CLPlacemark *placeM = placemarks.firstObject;
        NSDictionary *address = placeM.addressDictionary;
        NSLog(@"%@",address);
        [self.manager stopUpdatingLocation];
    }];
}
// 定位失败代理
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {

}
上一篇下一篇

猜你喜欢

热点阅读