如何在地图中显示从当前位置到目的地的路线

时间:2017-10-04 10:15:35

标签: ios objective-c

我正在静态制作一个基于花园的iOS应用程序。在这个应用程序中,我使用表视图并根据花园显示另一个视图的地图。特别的花园显示正常,但现在我只想显示从当前位置到目的地的路线。我正在使用目标c。请给我任何解决方案。这是我的代码。

#import "DetailViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
@synthesize vl,img,mylocation,mapview,coordinate;
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem {
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;

        // Update the view.
        [self configureView];
    }
}
- (void)configureView {
    // Update the user interface for the detail item.
    if (self.detailItem)
    {
        //adding map

        CLLocation *userLoc = mapview.userLocation.location;
        CLLocationCoordinate2D userCoordinate = userLoc.coordinate;

        NSLog(@"user latitude = %f",userCoordinate.latitude);
        NSLog(@"user longitude = %f",userCoordinate.longitude);
        mapview.mapType=MKMapTypeSatellite;
        mapview.delegate=self;

        NSMutableArray* annotations=[[NSMutableArray alloc] init];

         NSLog(@"%lu",(unsigned long)[annotations count]);

        MKMapRect flyTo = MKMapRectNull;
        for (id <MKAnnotation> annotation in annotations) {
            NSLog(@"fly to on");
            MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
            MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
            if (MKMapRectIsNull(flyTo)) {
                flyTo = pointRect;
            } else {
                flyTo = MKMapRectUnion(flyTo, pointRect);
                //NSLog(@"else-%@",annotationPoint.x);
            }
        }

        // Position the map so that all overlays and annotations are visible on screen.
        mapview.visibleMapRect = flyTo;

        /*mylocation=[[ThirdViewController alloc]init];
        mapview=[[MKMapView alloc]init];
        //setting map type
        mapview.mapType=MKMapTypeStandard;

        //display map
        mapview.showsUserLocation=YES;
        [self.view addSubview:mapview];
        [mapview addAnnotation:mylocation];*/



        //adding image
        NSArray*image=[[NSArray alloc]initWithObjects:@"Lodhi-Garden-Delhi.jpg",@"Garden-of-Five-Senses-Delhi.jpg",@"Japanese-Garden-Delhi.jpg",@"Deer-Park-Hauz-Khas-Delhi.jpeg",@"Nehru-Park-Delhi.jpg",nil];

        self.detailDescriptionLabel.text = [self.detailItem description];
        _detailDescriptionLabel.backgroundColor=[UIColor blueColor];

        vl=[self.detailItem description];
        if ([vl isEqual:@"Lodhi Garden"])
        {
            img.image=[UIImage imageNamed:[image objectAtIndex:0]];

            CLLocationCoordinate2D theCoordinate1;
            theCoordinate1.latitude = 28.5931;
            theCoordinate1.longitude = 77.2197;


            ThirdViewController* myAnnotation1=[[ThirdViewController alloc] init];

            myAnnotation1.coordinate=theCoordinate1;
            myAnnotation1.title=@"Lodhi Garden";
            myAnnotation1.subtitle=@"is here";
            [mapview addAnnotation:myAnnotation1];
        }
        else if([vl isEqual:@"Garden of Five Senses"])
        {
            img.image=[UIImage imageNamed:[image objectAtIndex:1]];

            CLLocationCoordinate2D theCoordinate2;
            theCoordinate2.latitude = 28.5133;
            theCoordinate2.longitude = 77.1985;

            ThirdViewController* myAnnotation2=[[ThirdViewController alloc] init];

            myAnnotation2.coordinate=theCoordinate2;
            myAnnotation2.title=@"Garden of Five Senses";
            myAnnotation2.subtitle=@"is here";

            [mapview addAnnotation:myAnnotation2];
        }
        else if([vl isEqual:@"Japanese Garden"])
        {
            img.image=[UIImage imageNamed:[image objectAtIndex:2]];

            CLLocationCoordinate2D theCoordinate3;
            theCoordinate3.latitude = 28.7231;
            theCoordinate3.longitude = 77.1199;

            ThirdViewController* myAnnotation3=[[ThirdViewController alloc] init];

            myAnnotation3.coordinate=theCoordinate3;
            myAnnotation3.title=@"Japanese Garden";
            myAnnotation3.subtitle=@"is here";

            [mapview addAnnotation:myAnnotation3];

        }
        else if([vl isEqual:@"Deer Park"])
        {
            img.image=[UIImage imageNamed:[image objectAtIndex:3]];

            CLLocationCoordinate2D theCoordinate4;
            theCoordinate4.latitude = 28.3324;
            theCoordinate4.longitude = 77.1145;

            ThirdViewController* myAnnotation4=[[ThirdViewController alloc] init];

            myAnnotation4.coordinate=theCoordinate4;
            myAnnotation4.title=@"Deer Park";
            myAnnotation4.subtitle=@"is here";

           [mapview addAnnotation:myAnnotation4];
        }
        else if([vl isEqual:@"Nehru Park"])
        {
            img.image=[UIImage imageNamed:[image objectAtIndex:4]];

            CLLocationCoordinate2D theCoordinate5;
            theCoordinate5.latitude = 28.5906;
            theCoordinate5.longitude = 77.1946;

            ThirdViewController* myAnnotation5=[[ThirdViewController alloc] init];

            myAnnotation5.coordinate=theCoordinate5;
            myAnnotation5.title=@"Nehru Park";
            myAnnotation5.subtitle=@"is here";

            [mapview addAnnotation:myAnnotation5];
        }
    }
}

- (void)ViewDidLoad {
    [super ViewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self configureView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

0 个答案:

没有答案