0

我在注释针和 tableview 上打开 Detailview。为了从 detailview 中获取方向,我在按钮单击事件之后放置了位置。

编辑:: 与 -(IBAction)showDirectionUpdated; 编码

      -(IBAction)showDirectionUpdated;
    {

NSIndexPath *selectedIndexPath = [self._tableView indexPathForSelectedRow];

if( selectedIndexPath == [self._tableView indexPathForSelectedRow])
 {
    marker *aMarker = (marker *)[appDelegate.markers objectAtIndex:selectedIndexPath.row];

NSString *EndLoc=[NSString stringWithFormat:@"%@ %@", aMarker.address,aMarker.city];

NSString* addr = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=Current Location&daddr=%@",EndLoc];
        NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        [[UIApplication sharedApplication] openURL:url];
        NSLog(@"%@",url);
        [url release];      

[self._tableView deselectRowAtIndexPath:selectedIndexPath animated:YES];
}
else
{ 

     //here if I am opening the detailview from annotation callout button and calling 
   // direction in map default app. But respective address is not passing 
    //in default map app
    NSInteger selectedIndex = [sender tag];
    AddressAnnotation *selectedObject = [self.annobjs objectAtIndex:selectedIndex];

marker *aMarker = [appDelegate.markers objectAtIndex:selectedIndex];
        NSString *EndLoc=[NSString stringWithFormat:@"%@ %@", aMarker.address,aMarker.city];

        NSString* addr = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=Current Location&daddr=%@",EndLoc];
        NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        [[UIApplication sharedApplication] openURL:url];
        NSLog(@"%@",url);
        [url release];

    }

}

我想传递一些发件人或 id 以从 detailView 调用相应的方向,我在按下注释时得到它。我通过选择 tableview(listview) 从 detailview 获取方向默认应用程序成功。这里有一些发件人标签的代码。

使用 viewForAnnotation 编辑 2===

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id   <MKAnnotation>) annotation
 {     MKAnnotationView *annView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@""];

 if (annView == nil)
 {
    annView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@""] autorelease];
    annView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

}


annView.image = [UIImage imageNamed:@"flag.png"];
annView.annotation = annotation;
[annView setEnabled:YES];
[annView setCanShowCallout:YES];
 return annView;

 }

 -(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
  {
NSLog(@"calloutAccessoryControlTapped");

MKAnnotationView* annView = (MKAnnotationView*) view;
AddressAnnotation* annotation = (AddressAnnotation*)[annView annotation];


if(BcardView.hidden == YES)
{
    SearchView.hidden = YES;

    BcardView.hidden = NO;


    BackButtontitle.text = @"Map View";

    marker *aMarker = [[marker alloc]init];

    ShowroomName.text = aMarker.name;
      }}
4

1 回答 1

2

根据发布的代码,这是我的理解:

marker是存储有关位置的所有详细信息的顶级类,包括addresscity

AddressAnnotation是实现MKAnnotation协议的类,当前仅包含位置的坐标、标题和副标题(它没有addressand city)。这是用于创建传递给地图视图addAnnotation方法的对象的类。

您想要做的是,当在注释上按下标注按钮时,您想要显示一些需要addressand的详细视图或信息city

我建议不要尝试使用标签来跟踪数组索引或搜索数组中的对象,而是添加对markerin的引用AddressAnnotation。这样,当您拥有一个AddressAnnotation对象时,您可以使用该引用获取其相关marker对象(无需搜索或索引)。

AddressAnnotation类中,添加一个marker名为的保留属性parentMarker。从 a创建AddressAnnotation对象时marker,在调用之前addAnnotation,将parentMarker属性设置为 current marker

然后在 中calloutAccessoryControlTappedview.annotation转换为 anAddressAnnotation以轻松访问该parentMarker属性。现在您可以从标记中获取addressand 。city(顺便说一句,在这种方法中,您不需要转换view为,MKAnnotationView因为它已经是一个并且您不必命名它annView。)

我不确定是什么BcardView以及为什么要检查它是否隐藏。

我还建议对该viewForAnnotation方法进行一些改进(使用非空白重用 id 并将不会更改的属性设置移动到内部if):

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>) annotation
{
    MKAnnotationView *annView = (MKAnnotationView *)[mapView 
        dequeueReusableAnnotationViewWithIdentifier:@"AddrAnnot"];

    if (annView == nil)
    {
        annView = [[[MKAnnotationView alloc] initWithAnnotation:annotation 
            reuseIdentifier:@"AddrAnnot"] autorelease];
        annView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        annView.image = [UIImage imageNamed:@"flag.png"];
        [annView setEnabled:YES];
        [annView setCanShowCallout:YES];
    }

    annView.annotation = annotation;

    return annView;
}
于 2011-11-28T13:39:40.940 回答