如何在Mkmapview中根据路线绘制折线

时间:2016-10-05 09:52:10

标签: objective-c mkmapview core-location polyline

大家好我需要在Polyline中显示MKapview。我已经这样做但我需要根据路线显示Polyline。如果我使用的位置确实更新,那么我会得到下面的图像我会尝试存储纬度和经度,然后下次我必须显示之前的Polyline我没有得到,请帮助我

enter image description here

1 个答案:

答案 0 :(得分:1)

- (void)drowRoautLines_FromLocation:(CLLocationCoordinate2D)FromLocation ToLocation:(CLLocationCoordinate2D)ToLocation
{
    MKPlacemark *source = [[MKPlacemark alloc]initWithCoordinate:FromLocation
                                               addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil]];
    MKMapItem *srcMapItem = [[MKMapItem alloc]initWithPlacemark:source];
    [srcMapItem setName:@"Source"];

    MKPlacemark *destination = [[MKPlacemark alloc]initWithCoordinate:ToLocation
                                                    addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil]];
    MKMapItem *distMapItem = [[MKMapItem alloc]initWithPlacemark:destination];
    [distMapItem setName:@"Destination"];

    MKDirectionsRequest *request = [[MKDirectionsRequest alloc]init];
    [request setSource:srcMapItem];
    [request setDestination:distMapItem];
    [request setTransportType:MKDirectionsTransportTypeAutomobile];

    MKDirections *direction = [[MKDirections alloc]initWithRequest:request];

    [direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error)
    {
        [self ShowLoading:NO];

        NSLog(@"response = %@",response);
        NSLog(@"error = %@",error);
        NSArray *arrRoutes = [response routes];
        if (arrRoutes.count == 0)
        {
            //Show Error Message.
        }
        else
        {
            [arrRoutes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
            {

                MKRoute *rout = obj;
                MKPolyline *line = [rout polyline];
                [mapView addOverlay:line];

                NSLog(@"Rout Name : %@",rout.name);
                NSLog(@"Total Distance (in Meters) :%f",rout.distance);
                NSArray *steps = [rout steps];
                NSLog(@"Total Steps : %lu",(unsigned long)[steps count]);

                [steps enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
                {
                    //NSLog(@"Rout Instruction : %@",[obj instructions]);
                }];
            }];
        }
    }];

}