Xcode 5:绘制从当前位置到注释的方向

时间:2014-09-13 19:23:51

标签: objective-c iphone ipad ios7 xcode5

我观看GeekyLemon的iOS地图教程,但我需要帮助从当前位置绘制一条线到MKMapView中的注释,而不是打开地图应用程序。这个应用程序,如果为www.arf.net所以请帮助。

我的代码没有任何有效或无效的路线指示。

MapViewController.h

//
//  MapViewController.h
//  ARF
//
//  Created by Tyler on 9/9/14.
//  Copyright (c) 2014 Tyler Olson. All rights reserved.
//

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

@interface MapViewController : UIViewController {
    MKMapView *mapView;
}
@property (nonatomic, retain) IBOutlet MKMapView *mapView;

-(IBAction)setMap:(id)sender;

-(IBAction)goBack;

@end

MapViewController.m

//
//  MapViewController.m
//  ARF
//
//  Created by Tyler on 9/9/14.
//  Copyright (c) 2014 Tyler Olson. All rights reserved.
//

#import "MapViewController.h"
#import "MoreViewController.h"
#import "MapPin.h"

@interface MapViewController ()

@end

@implementation MapViewController

@synthesize mapView;

#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

- (void)viewDidLoad
{
    [super viewDidLoad];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];
    mapView.showsUserLocation = YES;

    MKCoordinateRegion
    regionARF = {{0.0, 0.0}, {0.0,0.0}};
    regionARF.center.latitude = 37.93301;
    regionARF.center.longitude = -122.02052;
    regionARF.span.latitudeDelta = 0.01f;
    regionARF.span.longitudeDelta = 0.01f;
    [mapView setRegion:regionARF animated:YES];

    MapPin *ann = [[MapPin alloc] init];
    ann.title = @"ARF - Animal Rescue Foundation";
    ann.subtitle = @"2890 Mitchel Drive, Walnut Crek, CA";
    ann.coordinate = regionARF.center;
    [mapView addAnnotation:ann];

    MKPlacemark *sourcePlackmark = [[MKPlacemark alloc] initWithCoordinate:mapView.userLocation.coordinate addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil]];
    MKMapItem *sourceMapItem = [[MKMapItem alloc] initWithPlacemark:sourcePlackmark];
    [sourceMapItem setName:@""];

    MKPlacemark *destinationPlackmark = [[MKPlacemark alloc] initWithCoordinate:ann.coordinate addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil]];
    MKMapItem *destinationMapItem = [[MKMapItem alloc] initWithPlacemark:destinationPlackmark];
    [destinationMapItem setName:@""];

    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
    [request setSource:sourceMapItem];
    [request setDestination:destinationMapItem];
    [request setTransportType:MKDirectionsTransportTypeAny];
    [request setRequestsAlternateRoutes:YES];
    MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
        if (!error) {
            for (MKRoute *route in [response routes]) {
                [mapView addOverlay:[route polyline] level:MKOverlayLevelAboveRoads];
            }
        }
    }];
}

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

- (MKOverlayPathRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
        [renderer setStrokeColor:[UIColor greenColor]];
        [renderer setLineWidth:5.0];
        return renderer;
    }
    return nil;
}

- (IBAction)setMap:(id)sender {
    switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
        case 0:
            mapView.mapType = MKMapTypeStandard;
            break;
        case 1:
            mapView.mapType = MKMapTypeSatellite;
            break;
        case 2:
            mapView.mapType = MKMapTypeHybrid;
            break;

        default:
            break;
    }
}

- (IBAction)showMe {
    mapView.showsUserLocation = !mapView.showsUserLocation;
}

@end

0 个答案:

没有答案