我想在MapView中显示点对点路线,我使用此代码创建路线:
- (void)getDirections
{
//MKMapItem *srcMapItem = [MKMapItem mapItemForCurrentLocation];
MKPlacemark *source = [[MKPlacemark alloc]initWithCoordinate:CLLocationCoordinate2DMake(13.0833, 80.2333) addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil] ];
MKMapItem *srcMapItem = [[MKMapItem alloc]initWithPlacemark:source];
MKPlacemark *destination = [[MKPlacemark alloc]initWithCoordinate:CLLocationCoordinate2DMake(13.0983, 80.1622) addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil] ];
MKMapItem *desMapItem = [[MKMapItem alloc]initWithPlacemark:destination];
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
//request.source = [MKMapItem mapItemForCurrentLocation];
request.source = srcMapItem;
request.destination = desMapItem;
request.requestsAlternateRoutes = NO;
MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
[directions calculateDirectionsWithCompletionHandler: ^(MKDirectionsResponse *response, NSError *error) {
if (error) {
// Handle error
} else {
[self showRoute:response];
}
}];
}
-(void)showRoute:(MKDirectionsResponse *)response
{
for (MKRoute *route in response.routes)
{
[self.Route_Map addOverlay:route.polyline level:MKOverlayLevelAboveRoads];
for (MKRouteStep *step in route.steps)
{
NSLog(@"%@", step.instructions);
}
}
}
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
{
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
renderer.strokeColor = [UIColor redColor];
renderer.lineWidth = 10.0;
return renderer;
}
地图视图路线代码无效。有人发现错误吗?
有什么建议吗?
替换代码:
MKPlacemark *source = [[MKPlacemark alloc] initWithCoordinate: CLLocationCoordinate2DMake(37.776142, -122.424774) addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil]];
MKPlacemark *destination = [[MKPlacemark alloc] initWithCoordinate: CLLocationCoordinate2DMake(37.73787, -122.373962) addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil]];
现在正在工作。除了此代码之外,为什么路由映射不起作用?
有什么建议吗?