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
    • 563
    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.

    1. - (void)displayRouteFrom:(CLLocationCoordinate2D)sourceCoordinate
    2. to:(CLLocationCoordinate2D)destinationCoordinate
    3. {
    4. MKDirectionsRequest *directionsRequest = [[MKDirectionsRequest alloc] init];
    5. directionsRequest.transportType = MKDirectionsTransportTypeAny;
    6. MKPlacemark *sourcePlacemark = [[MKPlacemark alloc] initWithCoordinate:sourceCoordinate
    7. addressDictionary:nil];
    8. MKMapItem *sourceItem = [[MKMapItem alloc] initWithPlacemark:sourcePlacemark];
    9.  
    10. MKPlacemark *destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:destinationCoordinate
    11. addressDictionary:nil];
    12. MKMapItem *destinationItem = [[MKMapItem alloc] initWithPlacemark:destinationPlacemark];
    13.  
    14. [directionsRequest setSource:sourceItem];
    15. [directionsRequest setDestination:destinationItem];
    16. directionsRequest.transportType = MKDirectionsTransportTypeAny;
    17. MKDirections *directions = [[MKDirections alloc] initWithRequest:directionsRequest];
    18. [self.indicatorIB startAnimating];
    19. [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
    20. [self.indicatorIB stopAnimating];
    21. [self.indicatorIB setHidden:YES];
    22. if (error) {
    23. NSLog(@"Unable to find directions for the bus stop.");
    24.  
    25. // [Utility showAlert:nil mess:@"Unable to find directions for the bus stop."];
    26. } else {
    27. MKRoute *routeDetails = response.routes.lastObject;
    28.  
    29. [self.mapView setVisibleMapRect:[routeDetails.polyline boundingMapRect] edgePadding:UIEdgeInsetsMake(15.0, 15.0, 15.0, 15.0) animated:true];
    30. [self.mapView addOverlay:routeDetails.polyline];
    31.  
    32. }
    33. }];
    34. }

    You can call above method like ->

    1. [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
Reset Password
Fill out the form below and reset your password: