简单导航功能开发

2017-01-25  本文已影响58人  木小土

场景:场馆地址允许用户直接点击导航按钮开始导航,省去了让用户手动点开地图app和输入地址的操作。

实现方式:

点击“导航”按钮后,进入地图页面,此页面使用的是苹果自带的地图,打开页面后,在终点地址上显示一个小气泡,并添加一个导航按钮,点击地图页面的导航按钮,跳转到地图app开始导航。

地图页使用苹果地图,就不需要引入其它第三方的库,不会增加app的包大小。

导航时用户可以选择跳转到自己常用的地图app,不仅减小了开发的成本,用户的使用成本也会降低。

代码如下:

// DJMapViewController.h
@interface DJMapViewController

//终点标注标题
@property (nonatomic, strong) NSString *annotationTitle;

//终点标注副标题
@property (nonatomic, strong) NSString *annotationSubTitle;

- (instancetype)initWithLatitude:(CGFloat)latitude longitude:(CGFloat)longitude;

@end
#import <MapKit/MapKit.h>

@interface DJMapViewController () <MKMapViewDelegate>

@property (nonatomic, assign) CGFloat latitude;
@property (nonatomic, assign) CGFloat longitude;
@property (nonatomic, strong) MKMapView *mapView;
@property (nonatomic, assign) CLLocationCoordinate2D destCoordinate;

@end

@implementation DJMapViewController

- (instancetype)initWithLatitude:(CGFloat)latitude longitude:(CGFloat)longitude {
    if (self = [super init]) {
        self.latitude = latitude;
        self.longitude = longitude;
        self.destCoordinate = CLLocationCoordinate2DMake(self.latitude, self.longitude);
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self openMap];
    [self createTitleView];
}

- (void)createTitleView {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 64.0f)];
    [self.view addSubview:headerView];
    
    UIView *lineView = [SAUtility getLineWithFrame:CGRectMake(0, 63.5, WindowWidth, 0.5)];
    [headerView addSubview:lineView];
    
    CGRect leftItemRect = CGRectMake(0, APP_STATUSBAR_HEIGHT, APP_NAVIBAR_HEIGHT, APP_NAVIBAR_HEIGHT);
    UIButton* leftItem = [[UIButton alloc] initWithFrame:leftItemRect];
    [leftItem setImage:[UIImage imageNamed:IA_BackButton_thin_ImageName] forState:UIControlStateNormal];
    [leftItem addTarget:self action:@selector(leftButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:leftItem];
    
    UILabel *titleLabel=[SAUtility getNavigationTitle:@"导航" isMainPage:NO];
    [headerView addSubview:titleLabel];
    
    
}

- (void)openMap {

    //[MKMapItem openMapsWithItems:items launchOptions:options];
    
    
    self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 64, WindowWidth, WindowHeight)];
    self.mapView.mapType = MKMapTypeStandard;
    self.mapView.delegate = self;
    self.mapView.showsUserLocation = YES;
    [self.view addSubview:self.mapView];
    [self addActivityOnSuperView:self.view withFrame:CGRectMake(0, 64, WindowWidth, WindowHeight-64)];
    
    //设置地图显示范围
    [self.mapView setRegion:MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(self.latitude, self.longitude), 5000, 5000) animated:YES];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;
    if ([annotation isKindOfClass:[MKPointAnnotation class]]) {
        // Try to dequeue an existing pin view first.(这里跟UITableView的重用差不多)
        MKPinAnnotationView *customPinView = (MKPinAnnotationView*)[mapView
                                                                    dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
        if (!customPinView){
            // If an existing pin view was not available, create one.
            customPinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                                            reuseIdentifier:@"CustomPinAnnotationView"];
        }
        //iOS9中用pinTintColor代替了pinColor
        if ([SAUtility isSystemLaterThan:9.0]) {
            customPinView.pinTintColor = UIColorFromRGB(0x69BB0E);
        } else {
            customPinView.pinColor = MKPinAnnotationColorGreen;
        }
        
        customPinView.animatesDrop = YES;
        customPinView.canShowCallout = YES;
        
        
        UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 50)];
        rightButton.backgroundColor = UIColorFromRGB(0x69BB0E);
        [rightButton setTitle:@"导航" forState:UIControlStateNormal];
        [rightButton addTarget:self action:@selector(navigateButtonPressed) forControlEvents:UIControlEventTouchUpInside];
        customPinView.rightCalloutAccessoryView = rightButton;
        return customPinView;
    }
    return nil;
}

- (void)mapViewWillStartLoadingMap:(MKMapView *)mapView {

}
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView {
    [self removeActivityView];

    //设置标注
    if (self.annotationTitle && self.annotationTitle.length > 0) {
        MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
        [annotation setCoordinate:CLLocationCoordinate2DMake(self.latitude, self.longitude)];
        [annotation setTitle:self.annotationTitle];
        [annotation setSubtitle:self.annotationSubTitle];
        [self.mapView addAnnotation:annotation];
        
        [mapView selectAnnotation:annotation animated:YES];
    }

}

- (void)leftButtonPressed:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)navigateButtonPressed {
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"请选择地图" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    //自带地图
    [alertController addAction:[UIAlertAction actionWithTitle:@"使用苹果自带地图导航" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //使用自带地图导航
        MKMapItem *currentLocation =[MKMapItem mapItemForCurrentLocation];
        
        MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:self.destCoordinate addressDictionary:nil]];
        
        [MKMapItem openMapsWithItems:@[currentLocation,toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
                                                                                   MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];
        
        
    }]];
    
    //判断是否安装了高德地图,如果安装了高德地图,则使用高德地图导航
    if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"使用高德地图导航" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            NSString *urlsting =[[NSString stringWithFormat:@"iosamap://path?sourceApplication=applicationName&sid=BGVIS1&sname=我的位置&did=BGVIS2&dlat=%lf&dlon=%lf&dname=%@&dev=0&m=0&t=%@",self.latitude,self.longitude,self.annotationTitle,@"0"]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [[UIApplication  sharedApplication]openURL:[NSURL URLWithString:urlsting]];
            
        }]];
    }
    
    //判断是否安装了百度地图,如果安装了百度地图,则使用百度地图导航
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
        [alertController addAction:[UIAlertAction actionWithTitle:@"使用百度地图导航" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            NSString *urlsting =[[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",self.latitude,self.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlsting]];
            
        }]];
    }
    
    //添加取消选项
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [alertController dismissViewControllerAnimated:YES completion:nil];
        
    }]];
    
    //显示alertController
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

上一篇 下一篇

猜你喜欢

热点阅读