iOS MapKit导航及地理转码辅助类

头文件:

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

@interface DirectionRouteUtils : NSObject
{
    MKDirections *mDirections;
    CLGeocoder   *mGeocoder;
}

+ (instancetype)sharedInstance;

// 获取导航路线
- (void)findDirectionsFrom:(MKMapItem *)source
                        to:(MKMapItem *)destination
                   handler:(MKDirectionsHandler)completionHandler;

- (void)findDirectionsFrom:(MKMapItem *)source
                        to:(MKMapItem *)destination
             transportType:(MKDirectionsTransportType)transportType
                   handler:(MKDirectionsHandler)completionHandler;

- (void)cancelCalculateDirections;

// 地理转码

- (void)cancelGeocode;
- (void)geocodeAddressString:(NSString *)addressString
           completionHandler:(CLGeocodeCompletionHandler)completionHandler;

- (void)reverseGeocodeLocation:(CLLocation *)location
             completionHandler:(CLGeocodeCompletionHandler)completionHandler;

@end

实现文件:

#import "DirectionRouteUtils.h"

@implementation DirectionRouteUtils

+ (instancetype)sharedInstance
{
    static DirectionRouteUtils *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[[self class] alloc] init];
    });
    
    return sharedInstance;
}

- (id)init
{
    if (self = [super init]) {
        mGeocoder = [[CLGeocoder alloc] init];
    }
    return self;
}


- (void)findDirectionsFrom:(MKMapItem *)source
                        to:(MKMapItem *)destination
             transportType:(MKDirectionsTransportType)transportType
                   handler:(MKDirectionsHandler)completionHandler
{
    NSAssert(completionHandler != nil, @"Calculating directions handler shouldn't be nil!");
    
    [self cancelCalculateDirections];
    
    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
    request.source = source;
    request.destination = destination;
    request.requestsAlternateRoutes = YES;
    request.transportType = transportType; //MKDirectionsTransportTypeAutomobile;//MKDirectionsTransportTypeWalking;
    
    mDirections = [[MKDirections alloc] initWithRequest:request];
    [request release];
    
    /*
    [directions calculateDirectionsWithCompletionHandler:
     ^(MKDirectionsResponse *response, NSError *error) {
         
         if (error) {
             
             NSLog(@"error:%@", error);
         }
         else {
             NSLog(@"%@", response.routes);
             MKRoute *route = response.routes[0];
             
             for(MKRoute *step in route.steps)
             {
                 NSLog(@"Step: %@", ((MKRouteStep *)step).instructions);
             }
             
             
             [self.mapView addOverlay:route.polyline];
         }
     }];
     */
    
    
    if(completionHandler){
        [mDirections calculateDirectionsWithCompletionHandler:completionHandler];
    }

}



- (void)findDirectionsFrom:(MKMapItem *)source
                        to:(MKMapItem *)destination
                   handler:(MKDirectionsHandler)completionHandler
{
    [self findDirectionsFrom:source
                          to:destination
               transportType:MKDirectionsTransportTypeAutomobile
                     handler:completionHandler];
}


- (void)cancelCalculateDirections
{
    if(mDirections){
        [mDirections cancel];
        [mDirections release];
        mDirections = nil;
    }
}

- (void)cancelGeocode
{
    [mGeocoder cancelGeocode];
}


- (void)geocodeAddressString:(NSString *)addressString
           completionHandler:(CLGeocodeCompletionHandler)completionHandler
{
    NSAssert(completionHandler != nil, @"Geocoding handler shouldn't be nil!");
    
    [self cancelGeocode];
    [mGeocoder geocodeAddressString:addressString
                  completionHandler:completionHandler];
}

- (void)reverseGeocodeLocation:(CLLocation *)location
             completionHandler:(CLGeocodeCompletionHandler)completionHandler
{
    NSAssert(completionHandler != nil, @"Reversegeocoding handler shouldn't be nil!");
    
    [self cancelGeocode];
    [mGeocoder reverseGeocodeLocation:location
                    completionHandler:completionHandler];
    
}


@end

测试用例:

- (void)testGeocoding
{
    DirectionRouteUtils *utils = [DirectionRouteUtils sharedInstance];
    [utils geocodeAddressString:@"你要测试的地址" completionHandler:^(NSArray *placemarks, NSError *error) {
        for(CLPlacemark *mark in placemarks){
            NSLog(@"%@", mark.addressDictionary);
        }
    }];

}

- (void)testReverseGeocoding
{
    DirectionRouteUtils *utils = [DirectionRouteUtils sharedInstance];
    CLLocation *location = [[CLLocation alloc] initWithLatitude:24.6182746 longitude:118.131588];
    
    [utils reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
       
        for(CLPlacemark *mark in placemarks){
            NSLog(@"%@", mark.addressDictionary);
            NSLog(@"%@", mark);
        }
        
    }];
    [location release];
}


- (void)testDirections
{
    
    CLLocationCoordinate2D fromCoordinate = CLLocationCoordinate2DMake(24.6382086,
                                                                       118.131588);
    CLLocationCoordinate2D toCoordinate   = CLLocationCoordinate2DMake(24.6182746,
                                                                       118.131588);
    
    MKPlacemark *fromPlacemark = [[MKPlacemark alloc] initWithCoordinate:fromCoordinate
                                                       addressDictionary:nil];
    
    MKPlacemark *toPlacemark   = [[MKPlacemark alloc] initWithCoordinate:toCoordinate
                                                       addressDictionary:nil];
    
    MKMapItem *fromItem = [[MKMapItem alloc] initWithPlacemark:fromPlacemark];
    MKMapItem *toItem   = [[MKMapItem alloc] initWithPlacemark:toPlacemark];
    
    DirectionRouteUtils *utils = [DirectionRouteUtils sharedInstance];
    [utils findDirectionsFrom:fromItem to:toItem handler:^(MKDirectionsResponse *response, NSError *error) {
        if (error) {
            
            NSLog(@"error:%@", error);
        }
        else {
            NSLog(@"%@", response.routes);
            MKRoute *route = response.routes[0];
            
            for(MKRoute *step in route.steps)
            {
                NSLog(@"Step: %@", ((MKRouteStep *)step).instructions);
            }
            
            
            
        }

    }];
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值