码迷,mamicode.com
首页 > 其他好文 > 详细

福州大学第十一届程序设计竞赛

时间:2014-04-27 21:21:59      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:ios

头文件:

#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);
            }
            
            
            
        }

    }];
}


福州大学第十一届程序设计竞赛

标签:ios

原文地址:http://blog.csdn.net/ok_again/article/details/24595453

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!