I mention the code below.
MKPlacemark *source = [[MKPlacemark alloc]initWithCoordinate:CLLocationCoordinate2DMake(37.33554,-121.885209) addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil] ];
MKMapItem *srcMapItem = [[MKMapItem alloc]initWithPlacemark:source];
[srcMapItem setName:@""];
MKPlacemark *destination = [[MKPlacemark alloc]initWithCoordinate:CLLocationCoordinate2DMake(latttt, lonnn) addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil] ];
MKMapItem *distMapItem = [[MKMapItem alloc]initWithPlacemark:destination];
[distMapItem setName:@""];
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) {
NSLog(@"response = %@",response);
NSArray *arrRoutes = [response routes];
[arrRoutes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
MKRoute *rout = obj;
MKPolyline *line = [rout polyline];
[self.mapView setVisibleMapRect:[line boundingMapRect]];
[self.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) {
[arr_direction addObject:[obj instructions]];
int latt=[obj distance];
NSString *list_la=[NSString stringWithFormat:@"%d",latt];
[arr_distance addObject:list_la];
}];
[directions_table reloadData ];
}];
}];
and viewForOverlay function
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay {
if ([overlay isKindOfClass:[MKPolyline class]]) {
MKPolylineView* aView = [[MKPolylineView alloc]initWithPolyline:(MKPolyline*)overlay] ;
aView.strokeColor = [[UIColor redColor] colorWithAlphaComponent:1.0];
aView.lineWidth = 7;
return aView;
}
return nil;
}
Screenshot:
my output screen using this code
specific place
and my question is to draw a line between the user's location and the road, road and destination. Check out this image.
thanks for the support...
source
share