iOS基础-系统地图使用

  1. 地图开发注意:
  • 项目中必须添加MapKit/MapKit框架
  1. 地图导航注意:
  • 设置起点和终点
  • 设置导航参数
  • 通过MKMapItem调用类方法openMapsWithItems
  1. 地图显示行走路线注意:
  • 设置请求方向
  • 创建方向
  • 在代理方法中返回遮盖物view

** Demo百度云网盘下载地址:链接: http://pan.baidu.com/s/1xwuiq 密码: ih36**

@interface ViewController () @property (weak, nonatomic) IBOutlet MKMapView *mapkit;
@property (nonatomic, strong) CLGeocoder *gecoder;
@property (nonatomic, strong) MKPlacemark *mkPlaceMark1;
@property (nonatomic, strong) MKPlacemark *mkPlaceMark2;
- (IBAction)myLocation;
@end
@implementation ViewController
- (void)viewDidLoad{
    [super viewDidLoad];
    [self addNavigationNutton];
       self.mapkit.userTrackingMode = MKUserTrackingModeFollow;
    self.mapkit.delegate = self;
    NSString *city1 = @"北京"; 
   NSString *city2 = @"广州";    //    地理编码 (北京)
    [self.gecoder geocodeAddressString:city1 completionHandler:^(NSArray *placemarks, NSError *error) {        CLPlacemark *placeMark1 = [placemarks firstObject];
  //        添加大头针 (北京)
        HSLAnnotion *ann1 = [[HSLAnnotion alloc]init];        

ann1.coordinate = placeMark1.location.coordinate;
        ann1.title = city1; 
       ann1.subtitle = placeMark1.name;
        [self.mapkit addAnnotation:ann1];
   //        地理编码 (广州)
        [self.gecoder geocodeAddressString:city2 completionHandler:^(NSArray *placemarks, NSError *error) {                       
CLPlacemark *placeMark2 = [placemarks firstObject];
//            添加大头针 (广州)
            HSLAnnotion *ann2 = [[HSLAnnotion alloc]init];            ann2.coordinate = placeMark2.location.coordinate;            ann2.title = city2; 
           ann2.subtitle = placeMark2.name; 
           [self.mapkit addAnnotation:ann2];
            //            开始画线 
           [self drawLineWithSourceCLPm:placeMark1 destinationCLPm:placeMark2];
        }];
    }]; 
 }
//添加北京到广州的导航按钮
- (void)addNavigationNutton{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(200, 370, 100, 30);
    [button setTitle:@"start Nav" forState:UIControlStateNormal];    [button setBackgroundColor:[UIColor greenColor]];
    [button addTarget:self action:@selector(startNavigation) forControlEvents:UIControlEventTouchUpInside]; 
   [self.view addSubview:button];
}
//打开北京到广州的导航功能
- (void)startNavigation{
//    起点
    MKMapItem *item1 = [[MKMapItem alloc]initWithPlacemark:self.mkPlaceMark1];
//    终点    
MKMapItem *item2 = [[MKMapItem alloc]initWithPlacemark:self.mkPlaceMark2];
//    添加起点终点到数组中
    NSArray *array = @[item1, item2];
//    设置导航参数
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//    设置驾车模式
    dict[MKLaunchOptionsDirectionsModeDriving] = MKLaunchOptionsDirectionsModeKey;
//    打开路况信息    
dict[MKLaunchOptionsShowsTrafficKey] = @(YES);
    [MKMapItem openMapsWithItems:array launchOptions:dict];
}
- (void)drawLineWithSourceCLPm:(CLPlacemark *)sourceCLPm destinationCLPm:(CLPlacemark *)destinationCLPm
{
//     设置方向请求
    MKDirectionsRequest *request = [[MKDirectionsRequest alloc]init];
//    起点
    MKPlacemark *place1 = [[MKPlacemark alloc]initWithPlacemark:sourceCLPm];
    request.source = [[MKMapItem alloc]initWithPlacemark:place1];
    self.mkPlaceMark1 = place1;
//    终点
    MKPlacemark *place2 = [[MKPlacemark alloc]initWithPlacemark:destinationCLPm];
    request.destination = [[MKMapItem alloc]initWithPlacemark:place2];
    self.mkPlaceMark2 = place2;
       MKDirections *dirce = [[MKDirections alloc]initWithRequest:request];
//    创建方向
    [dirce calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) 
{
        if (error) return ;
//        在mapview上添加遮盖
        for (MKRoute *routess in response.routes)
 { 
           [self.mapkit addOverlay:routess.polyline];
        }
    }];
}
- (CLGeocoder *)gecoder{
    if (_gecoder == nil) {
        self.gecoder = [[CLGeocoder alloc]init];
    }
    return _gecoder;
}
//  代理方法
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id)overlay{
//    添加折现遮盖
    MKPolylineRenderer *poly = [[MKPolylineRenderer alloc]initWithOverlay:overlay];
    poly.lineWidth = 3;
    poly.strokeColor = [UIColor redColor];
    return poly;
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    CLLocationCoordinate2D coor = userLocation.location.coordinate;
    [self.mapkit setCenterCoordinate:coor animated:YES];    MKCoordinateRegion regin = MKCoordinateRegionMake(coor, MKCoordinateSpanMake(0.2, 0.3));
    [self.mapkit setRegion:regin animated:YES];
}
- (IBAction)myLocation {
       CLLocationCoordinate2D coor = self.mapkit.userLocation.location.coordinate;
    [self.mapkit setCenterCoordinate:coor animated:YES];
}
@end

你可能感兴趣的:(iOS基础-系统地图使用)