如何在当前位置和MKMapView上的所需位置之间绘制路线?

我想显示当前位置和所需位置之间的路线。 我能够这样做但是当源点和目标点到目前为止它不会显示路线并给出内存警告。 你能建议任何示例代码或任何方式吗?

下面的代码为当前位置到欲望位置之间的绘制路径

- (void)viewDidLoad { [super viewDidLoad]; mapView = [[[MapView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)] autorelease]; [self.view addSubview:mapView]; [self performSelector:@selector(LoadMaps) withObject:self afterDelay:1.0]; } -(void)LoadMaps{ MapWithRoutesAppDelegate *app = (MapWithRoutesAppDelegate *)[[UIApplication sharedApplication]delegate]; CLLocation *location = app.userlocation; // Configure the new event with information from the location CLLocationCoordinate2D coordinate = [location coordinate]; NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude]; NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude]; NSLog(@"dLatitude : %@", latitude); NSLog(@"dLongitude : %@",longitude); Place* home = [[[Place alloc] init] autorelease]; home.name = @"Home"; home.description = @"Sweet home"; home.latitude = coordinate.latitude; home.longitude = coordinate.longitude; Place* office = [[[Place alloc] init] autorelease]; office.name = @"Office"; office.description = @"Bad office"; office.latitude = 23.329404; office.longitude = 72.0039299; [mapView showRouteFrom:home to:office]; NSLog(@"latitide:%.7f, longitude: %.7f ", coordinate.latitude, coordinate.longitude); } 

在Appdelegate方法中包含

 #pragma mark- CLLocationManager delegate - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ MapWithRoutesAppDelegate *app = (MapWithRoutesAppDelegate*)[[UIApplication sharedApplication] delegate]; app.userlocation = newLocation; NSLog(@"latitide:%.7f, longitude: %.7f ", app.userlocation.coordinate.latitude, app.userlocation.coordinate.longitude); } 

您可以从此处下载完整的源代码。

愿这有用了。