Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Calculate distance between two point and draw route on map

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 291
    Comment on it

    Hi all,

    Let take you have two points, Point1 (In Boston) and Point2 (In Atlanta), Now you have to show the route between two points.

    In below method , you have to pass two param's i.e.. two point of map.

    - (void)displayRouteFrom:(CLLocationCoordinate2D)sourceCoordinate
                          to:(CLLocationCoordinate2D)destinationCoordinate
    {
        MKDirectionsRequest *directionsRequest = [[MKDirectionsRequest alloc] init];
        directionsRequest.transportType = MKDirectionsTransportTypeAny;
        MKPlacemark *sourcePlacemark = [[MKPlacemark alloc] initWithCoordinate:sourceCoordinate
                                                             addressDictionary:nil];
        MKMapItem *sourceItem = [[MKMapItem alloc] initWithPlacemark:sourcePlacemark];
    
        MKPlacemark *destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:destinationCoordinate
                                                                  addressDictionary:nil];
        MKMapItem *destinationItem = [[MKMapItem alloc] initWithPlacemark:destinationPlacemark];
    
        [directionsRequest setSource:sourceItem];
        [directionsRequest setDestination:destinationItem];
        directionsRequest.transportType = MKDirectionsTransportTypeAny;
        MKDirections *directions = [[MKDirections alloc] initWithRequest:directionsRequest];
        [self.indicatorIB startAnimating];
        [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
            [self.indicatorIB stopAnimating];
            [self.indicatorIB setHidden:YES];
            if (error) {
                NSLog(@"Unable to find directions for the bus stop.");
    
    //            [Utility showAlert:nil mess:@"Unable to find directions for the bus stop."];
            } else {
                MKRoute *routeDetails = response.routes.lastObject;
    
                [self.mapView setVisibleMapRect:[routeDetails.polyline boundingMapRect] edgePadding:UIEdgeInsetsMake(15.0, 15.0, 15.0, 15.0) animated:true];
                [self.mapView addOverlay:routeDetails.polyline];
    
            }
        }];
    }
    

    You can call above method like ->

     [self displayRouteFrom:CLLocationCoordinate2DMake([@"26354.267867" floatValue], [@"26354.267867" floatValue]) to:CLLocationCoordinate2DMake([@"26354.267867" floatValue], [@"26354.267867" floatValue])];
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: