导航地图1_地图显示

// 1. 导入MKMapKit
#import 

@interface ViewController ()
- (IBAction)changeMap:(UISegmentedControl *)sender;

@property (nonatomic, weak) MKMapView *mapView;

@end

@implementation ViewController

/**
 *  当点击segmentControl的按钮时,切换地图的风格
 */
- (IBAction)changeMap:(UISegmentedControl *)sender {
    
    MKMapType type;
    
    switch (sender.selectedSegmentIndex) {
            
        case 0:
            type = MKMapTypeStandard;
            break;
        case 1:
            type = MKMapTypeSatellite;
            break;
        case 2:
            type = MKMapTypeHybrid;
            break;
        default:
            break;
    }
    
    // 设置地图类型
    self.mapView.mapType = type;
}

- (MKMapView *)mapView {
    if (_mapView == nil) {
        MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
        // 保存经纬度对象
        CLLocationCoordinate2D coordinate2D;
        // 纬度
        coordinate2D.latitude = 39.90;
        // 经度
        coordinate2D.longitude = 116.39;
        MKCoordinateSpan span = MKCoordinateSpanMake(0.01f, 0.01f);
        [mapView setRegion:MKCoordinateRegionMake(coordinate2D, span)];
        [self.view addSubview:mapView];
        _mapView = mapView;
    }
    return _mapView;
}

/**
 *  1. 地图显示
 *  2. 定位当前位置
 *  3. 添加标注
 *  4. 添加覆盖物
 *  5. 搜索功能
 *  6. 路线查询
 *  7. 地理编码和反编码
 *  8. 经纬度
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    [self mapView];
    [self addAnnotation];
}

- (void)addAnnotation {
    
    CLLocationCoordinate2D coordinate2D = {39.90,116.39};
    MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc] init];
    pointAnnotation.coordinate = coordinate2D;
    pointAnnotation.title = @"北京市";
    pointAnnotation.subtitle = @"天安门";
    
    // 1.添加一个标注
    //    [self.mapView addAnnotation:pointAnnotation];
    // 2.添加多个标注
    //    [self.mapView addAnnotations:<#(NSArray *)#>];
    // 3.移除指定的标注
    //    [self.mapView removeAnnotation:<#(id)#>];
    // 4.移除多个标注
    //    self.mapView removeAnnotations:<#(NSArray *)#>
    // 5.获取地图上所有的标注
    //    NSArray *annotations = self.mapView.annotations;
    // 6.移除所有标注
    //    [self.mapView removeAnnotations:self.mapView.annotations;];
}

你可能感兴趣的:(iOS)