我使用下面的代码是为了从我的位置获取路线并在列表视图中显示。
-(void)getDirectionFromMyPosition:(id)sender{
Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
if (!(networkStatus == NotReachable)) {
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
[request setSource:[MKMapItem mapItemForCurrentLocation]];
// Make the destination
CLLocationCoordinate2D destinationCoords = CLLocationCoordinate2DMake(self.lat,self.lon);
MKPlacemark *destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:destinationCoords addressDictionary:nil];
MKMapItem *destination = [[MKMapItem alloc] initWithPlacemark:destinationPlacemark];
[request setDestination:destination];
[request setTransportType:MKDirectionsTransportTypeAny]; // This can be limited to automobile and walking directions.
[request setRequestsAlternateRoutes:YES]; // Gives you several route options.
MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
[MBProgressHUD showHUDAddedTo:self animated:YES];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self animated:YES];
});
if (!error) {
NSArray *routes=[response routes];
if(delegate && [delegate respondsToSelector:@selector(calculatedRoute::)]) {
[delegate calculatedRoute:routes :(UIButton*)sender];
}
for (MKRoute *route in routes) {
[self.mapView addOverlay:[route polyline] level:MKOverlayLevelAboveRoads]; // Draws the
[self.mapView setVisibleMapRect:[[route polyline] boundingMapRect] edgePadding:UIEdgeInsetsMake(35.0, 35.0, 35.0, 35.0) animated:YES];
}
}else{
[self handleMapDirectionError];
}
}];
});
}else{
[self handleNoInternet];
}
}
但我不知道,我如何设置与路线指令相关联的正确图像。
我想获得下图中显示的结果:
到目前为止,我的残酷解决方案是:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"directionsDetailTableCell";
DirectionsDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DirectionsDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
/*********************/
/*Le API ad oggi non tornano nulla che possa far capire quale immagine usare*/
/*********************/
NSString *str=[[self.steps objectAtIndex:indexPath.row] instructions];
if(![str isEqualToString:@""]){
UIImage *img=nil;
if(([str rangeOfString:@"destinazione"].length!=0||[str rangeOfString:@"destination"].length!=0)){
img=[UIImage imageNamed:@"destination.png"];
}else if([str rangeOfString:@"Continua"].length!=0||[str rangeOfString:@"Proceed"].length!=0||[str rangeOfString:@"Continue"].length){
img=[UIImage imageNamed:@"continues.png"];
}else if([str rangeOfString:@"sinistra"].length!=0||[str rangeOfString:@"left"].length!=0){
img=[UIImage imageNamed:@"left.png"];
}else if ([str rangeOfString:@"destra"].length!=0||[str rangeOfString:@"right"].length!=0){
img=[UIImage imageNamed:@"right.png"];
}else if(([str rangeOfString:@"rotonda"].length!=0||[str rangeOfString:@"round"].length!=0)){
img=[UIImage imageNamed:@"round.png"];
}else if(([str rangeOfString:@"uscita"].length!=0||[str rangeOfString:@"exit"].length!=0)){
img=[UIImage imageNamed:@"exit.png"];
}
if(img!=nil){
cell.img.image=img;
}
cell.lbltitle.text=[[self.steps objectAtIndex:indexPath.row] instructions];
cell.lbldescription.text=[NSString stringWithFormat:@"%f Km",[[self.steps objectAtIndex:indexPath.row] distance]/1000];
}
return cell;
}