ios-地图 大头针的使用

88 篇文章 0 订阅
#import "ViewController.h"
//1.导入MKMapKit
#import <MapKit/MapKit.h>

@interface ViewController ()

- (IBAction)segmentHandle:(UISegmentedControl *)sender;
@property (nonatomic, weak)MKMapView *mapView;

@end

@implementation ViewController


- (IBAction)segmentHandle:(UISegmentedControl *)sender {
    /*
     MKMapTypeStandard 标准地图
     MKMapTypeSatellite卫星地图
     MKMapTypeHybrid 混合地图
     */
    
    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) {
        
        
#if 0
        MKMapView *mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];
        
        
        CLLocationCoordinate2D coordinate2D;
        //保存经纬度对象
        coordinate2D.latitude = 39.9087607470;
        coordinate2D.longitude = 116.3975780490;
        
        
        //保存经纬度比例
        MKCoordinateSpan span;
        //纬度
        span.latitudeDelta = 1/100;
        span.longitudeDelta = 1/100;
        
        
       //范围:两部分 1.中心点经纬度 经纬度比例尺
        MKCoordinateRegion region;
        region.center = coordinate2D;
        region.span = span;
        
        [mapView setRegion:region];
        
        
        [self.view addSubview:mapView];
        
        _mapView = mapView;
#endif 
        
        MKMapView *mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];
        
        //中心点经纬度
        CLLocationCoordinate2D coordinate2D = {39.9087607478,116.3975780499};
        
        //比例
        MKCoordinateSpan span = {0.01,0.01};
        
        //设置范围
        [mapView setRegion:MKCoordinateRegionMake(coordinate2D, span)];
        
        [self.view addSubview:mapView];
        _mapView = mapView;
    }
    return _mapView;
}

/**
 *  添加标注
 */
- (void)addAnnotation
{
    CLLocationCoordinate2D coordinate2D = {39.9087607478,116.3975780499};
    
    //大头针对象
    MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc]init];
    
    pointAnnotation.coordinate = coordinate2D;
    //标题
    pointAnnotation.title = @"北京市";
    
    //子标题
    pointAnnotation.subtitle = @"天安门";
    
    //添加标注
    [self.mapView addAnnotation:pointAnnotation];
    
#if 0
    
    //1.添加一个标注
    [self.mapView addAnnotation:<#(id<MKAnnotation>)#>];
    
    //2.一次性添加多个标注
    [self.mapView addAnnotations:<#(NSArray *)#>];
    
    //3.移除指定标注
    [self.mapView removeAnnotation:<#(id<MKAnnotation>)#>];
    
    //4.移除多个标注
    [self.mapView removeAnnotations:<#(NSArray *)#>];
    
    //5.获取多个标注
    self.mapView.annotations;
    
    
    //6.移除所有标注
    
     [self.mapView removeAnnotations:self.mapView.annotations;];
    
#endif
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //地图显示类
    MKMapView *mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];
    
    [self.view addSubview:mapView];
    
    [self addAnnotation];
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值