如何在mapview的底部顶部和结束位置显示路线的起始位置?

时间:2015-11-20 17:23:17

标签: ios objective-c mkmapview ios8.1 mkmapviewdelegate

我正在iPhone应用程序的地图上显示起始位置和结束位置之间的路线,我想在地图顶部显示起始位置,在底部显示结束位置,同时确保所有路线都应在地图上可见。下面是我试过的代码。

-(void) viewDidLoad {

        MKPointAnnotation *myAnnotation = [[MKPointAnnotation alloc] init];
        myAnnotation.coordinate = CLLocationCoordinate2DMake(42.110532,    
           -87.912126);
         myAnnotation.title = @"Start point";

         [self.mapView addAnnotation:myAnnotation];

        MKPointAnnotation *destinatioAnnotation = [[MKPointAnnotation alloc] 
         init];
        destinatioAnnotation.coordinate = CLLocationCoordinate2DMake(42.105948, 
        -87.870664);
        destinatioAnnotation.title = @"End Point";

        [self.mapView addAnnotation:destinatioAnnotation];


        CLLocationCoordinate2D startCoord1 = 
        CLLocationCoordinate2DMake(42.110532, -87.912126);
        MKCoordinateRegion adjustedRegion1 = [self.mapView 
        regionThatFits:MKCoordinateRegionMakeWithDistance(startCoord1, 300000, 
        300000)];

        [self.mapView setRegion:adjustedRegion1 animated:YES];

        self.mapView.showsCompass = NO;

        MKDirectionsRequest *directionsRequest = [MKDirectionsRequest new];

        directionsRequest.requestsAlternateRoutes = NO;
        // Make the destination
        CLLocationCoordinate2D sourceCoords = 
        CLLocationCoordinate2DMake(42.110532, -87.912126);
        MKPlacemark *sourcePlacemark = [[MKPlacemark alloc] 
        initWithCoordinate:sourceCoords addressDictionary:nil];
        MKMapItem *sourceLoc = [[MKMapItem alloc] 
        initWithPlacemark:sourcePlacemark];
        // Set the source and destination on the request


        // Make the destination
        CLLocationCoordinate2D destinationCoords = CLLocationCoordinate2DMake(42.105948, -87.870664);
        MKPlacemark *destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:destinationCoords addressDictionary:nil];
        MKMapItem *destination = [[MKMapItem alloc] initWithPlacemark:destinationPlacemark];

        // Set the source and destination on the request
        [directionsRequest setSource:sourceLoc];
        [directionsRequest setDestination:destination];

        MKDirections *directions = [[MKDirections alloc] initWithRequest:directionsRequest];


        [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *
    _Nullable response, NSError * _Nullable error) {

            // Now handle the result
            if (error) {
                NSLog(@"There was an error getting your directions");
                return;
            }

            // So there wasn't an error - let's plot those routes
            _currentRoute = [response.routes firstObject];



            [self plotRouteOnMap:_currentRoute];

        }];
         }

    - (void)plotRouteOnMap:(MKRoute *)route {
        if(_routeOverlay) {
            [self.mapView removeOverlay:_routeOverlay];
        }

        // Update the ivar
        _routeOverlay = route.polyline;

        self.routePolyline = route.polyline;

        self.mapView.tag = 1;

        // Add it to the map

        [self.mapView setVisibleMapRect:[route.polyline boundingMapRect] edgePadding:UIEdgeInsetsMake(50, 50, 50, 40) animated:YES];

        /*[self.mapView setRegion:MKCoordinateRegionForMapRect([route.polyline
    boundingMapRect])
              animated:YES];*/

        [self.mapView addOverlay:_routeOverlay level:MKOverlayLevelAboveRoads];

        [self performSelector:@selector(changeHeading) withObject:nil 
        afterDelay:1.2];
         }
    - (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
        NSLog(@"called");
        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];

        if (mapView.tag == 1) {
                renderer.strokeColor = [UIColor redColor];
        }if (mapView.tag == 2) {

            renderer.strokeColor = [UIColor blueColor];
        }else{
            renderer.strokeColor = [UIColor blueColor];
        }

        renderer.lineWidth = 4.0;

        NSLog(@"x: %f, y:%f, widht: %f, height: %f",[overlay boundingMapRect].origin.x,[overlay
    boundingMapRect].origin.y,[overlay
    boundingMapRect].size.width,[overlay boundingMapRect].size.height);

        return  renderer;

    }

    -(void) changeHeading {

        MKMapCamera *camera = self.mapView.camera;

        CLLocationDistance altitude = camera.altitude;

        NSLog(@"altitude: %f",altitude);

        CLLocationDirection mapAngle = camera.heading;

         NSLog(@"mapAngle: %f",mapAngle);

        if (altitude < 3000 && altitude > 1000) {
            // do something
        }

        if (self.mapView.camera.heading != 257) {

            MKMapCamera *newCamera = [MKMapCamera camera];
            newCamera.centerCoordinate = self.mapView.camera.centerCoordinate;
            newCamera.heading = 257;
            newCamera.altitude = self.mapView.camera.altitude;
            [self.mapView setCamera:newCamera animated:YES];

        }



        /*MKMapCamera *turnCam = [MKMapCamera cameraLookingAtCenterCoordinate:CLLocationCoordinate2DMake(42.110532,
    -87.912126)
                                                          fromEyeCoordinate:CLLocationCoordinate2DMake(42.105948, -87.870664)
                                                                eyeAltitude:self.mapView.camera.altitude];




        self.mapView.camera = turnCam;*/

         }

0 个答案:

没有答案