导航地图3_定位

#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface ViewController () <CLLocationManagerDelegate,MKMapViewDelegate>

/** 定位管理者 */
@property (nonatomic, strong) CLLocationManager *locationManager;

/** 地图显示类 */
@property (nonatomic, strong) MKMapView *mapView;

@end

@implementation ViewController

#pragma mark - 懒加载
/**
 *  1. 定位对象
 *  2. 设置各种  授权 经度等
 *  3. 设置代理
 *  4. 开始定位 执行代理方法获取经纬度
 */
- (MKMapView *)mapView {
    
    if (_mapView == nil) {
        MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
        CLLocationCoordinate2D coordinate = {39.9087607478,116.3975780499};
        MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);
        MKCoordinateRegion region = MKCoordinateRegionMake(coordinate, span);
        [mapView setRegion:region];
        mapView.delegate = self;
        // 获取用户当前位置
        mapView.showsUserLocation = YES;
        [self.view addSubview:mapView];
        _mapView = mapView;
    }
    return _mapView;
}

/**
 *  定位时,我们需要使用CLLocationManager这个类 设置定位精确度、授权等
 */
- (CLLocationManager *)locationManager {
    
    if (_locationManager == nil) {
        _locationManager = [[CLLocationManager alloc] init];
        // 设置定位精确度
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        // 移动多远重新定位一次
        _locationManager.distanceFilter = 100.0f;
        
        if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
            // 1. 使用的时候授权
            [_locationManager requestWhenInUseAuthorization];
            // 2. 永久授权
            [_locationManager requestAlwaysAuthorization];
        }
        // 启动定位
        [_locationManager startUpdatingLocation];
        _locationManager.delegate = self;
        
//  NSLocationWhenInUseUsageDescription 当显示授权的alertView的时候我们可以再info.plist文件中添加这个键对应着值为描述.
        
    }
    return _locationManager;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self locationManager];
    [self test];
    
    // 1. 查看位置服务是否能用
    if ([CLLocationManager locationServicesEnabled]) {
        NSLog(@"位置服务可用");
    } else {
        NSLog(@"位置服务不可用");
    }
    
    // 2. 获取授权状态
    NSLog(@"status = %d", [CLLocationManager authorizationStatus]);
    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
    annotation.title = @"北京市";
    [self.mapView addAnnotation:annotation];
    
    // 3. 修改当前位置的文字
    // 获取用户当前的位置对象 前提是必须把showUserLocation设置为YES
    MKUserLocation *userLocation = self.mapView.userLocation;
    userLocation.title = @"当前位置";
}

#pragma mark - CLLocationManager 代理方法

/**
 *  已经定位到当前位置会触发
 */
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    
    // 获取当前的地理位置
    CLLocation *location = locations[0];
    // 当前的经纬度
    CLLocationCoordinate2D coordinate = location.coordinate;
    
    NSLog(@"%f %f", coordinate.latitude,coordinate.longitude);
    
#warning 将经纬度信息提交到服务器
    
    // 停止定位
    [manager stopUpdatingLocation];
    
}

/**
 *  当不能定位的时候调用这个方法
 */
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    
}

/**
 *  当用户进入到指定的区域内调用这个方法
 */
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    
    
}

#pragma mark - MKMapView 代理方法

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    
    // 如果是用户当前位置 不定制标注 // MKUserLocation是用来显示用户当前位置的类
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;
    }
    
    MKPinAnnotationView *pin = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"annotation"];
    if (pin == nil) {
        pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation"];
        // 显示气泡视图
        pin.canShowCallout = YES;
    }
    return pin;
}



#pragma mark - test

/**
 *  从网络请求经纬度,添加标注的练习
 */

- (void)test {
    
    NSString *string = @"http://apis.baidu.com/apistore/attractions/spot?id=yiheyuan&output=json";
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:string]];
    [request addValue:@"584554ed97a4498db8d8c6fab1c644cc" forHTTPHeaderField:@"apikey"];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        // 解析
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        jsonDict = jsonDict[@"result"];
        
        NSString *name = jsonDict[@"name"];
        NSLog(@"name - %@", name);
        
        jsonDict = jsonDict[@"location"];
        NSNumber *lat = jsonDict[@"lat"];
        NSNumber *lng = jsonDict[@"lng"];
        
        MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc] init];
        pointAnnotation.coordinate = CLLocationCoordinate2DMake([lat floatValue], [lng floatValue]);
        pointAnnotation.title = name;
        // 添加大头针
        [self.mapView addAnnotation:pointAnnotation];
    }];
    
}
@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值