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)