我提到的代码如下。
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]];
// NSLog(@"%@",arr_direction);
int latt=[obj distance];
NSString *list_la=[NSString stringWithFormat:@"%d",latt];
[arr_distance addObject:list_la];
// NSLog(@"%@",arr_distance);
}];
[directions_table reloadData ];
}];
}];
和viewForOverlay函数是
- (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;
}
屏幕截图:
my output screen using this code
我的问题是在用户位置和道路,道路和目的地之间划线。看到这张图片。
感谢支持......
答案 0 :(得分:3)
在用户位置和起点,终点和目的地之间划线。
[direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
NSLog(@"response = %@",response);
NSArray *arrRoutes = [response routes];
[arrRoutes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
MKRoute *route = obj;
MKPolyline *line = [route polyline];
[self.mapView setVisibleMapRect:[line boundingMapRect]];
[self.mapView addOverlay:line];
NSLog(@"Rout Name : %@",route.name);
NSLog(@"Total Distance (in Meters) :%f",route.distance);
////// EDITED FROM HERE by Kosuke //////
NSUInteger pointCount = route.polyline.pointCount;
// allocate a C array to hold this many points/coordinates...
CLLocationCoordinate2D *routeCoordinates
= malloc(pointCount * sizeof(CLLocationCoordinate2D));
// get the coordinates (all of them)...
[route.polyline getCoordinates:routeCoordinates
range:NSMakeRange(0, pointCount)];
// make line between User location and Start point
CLLocationCoordinate2D coordinates1[2] = {userCoordinate, routeCoordinates[0]};
MKPolyline *line1 = [MKPolyline polylineWithCoordinates:coordinates1 count:2];
[self.mapView addOverlay:line1];
// make line between Finish point and Destination location
CLLocationCoordinate2D coordinates2[2] = {routeCoordinates[pointCount - 1], destinationCoordinate};
MKPolyline *line2 = [MKPolyline polylineWithCoordinates:coordinates2 count:2];
[self.mapView addOverlay:line2];
//free the memory used by the C array when done with it...
free(routeCoordinates);
//////////////////
NSArray *steps = [rout steps];
NSLog(@"Total Steps : %lu",(unsigned long)[steps count]);
[steps enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {