• 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);
                }
                
                
                
            }
    
        }];
    }
    


  • 相关阅读:
    汉语-词语:养生
    汉语-词语:道家美学
    汉语-词语:审美
    人物-书法家:王羲之
    人物-道家:庄子
    汉语-词语:含蓄
    关于finfo_file函数获取文件mime值验证出错的问题
    第一个Hello,OS World操作系统
    Lead软件项目半年感受
    Cts框架解析(15)-任务运行完
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4298130.html
Copyright © 2020-2023  润新知