0

注釈ピンとテーブルビューで詳細ビューを開いています。詳細ビューから方向を取得するために、ボタンクリックイベントに続く場所があります。

編集::-(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];

    }

}

アノテーションを押して取得したdetailViewからそれぞれのDirectionを呼び出すために、送信者またはIDを渡したい。tableview(listview)を選択して取得したdetailviewから方向デフォルトアプリを取得することに成功しました。ここに送信者タグのコードがあります。

2===をviewForAnnotationで編集

- (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

投稿されたコードに基づいて、ここに私の理解があります:

markeraddressは、およびを含む場所に関するすべての詳細を格納するトップレベルのクラスですcity

AddressAnnotationMKAnnotationは、現在場所の座標、タイトル、サブタイトルのみを含むプロトコルを実装するクラスです(addressとは含まれていませんcity)。addAnnotationこれは、マップビューのメソッドに渡されるオブジェクトを作成するために使用されるクラスです。

address注釈でコールアウトボタンが押されたときに、とを必要とする詳細ビューまたは情報を表示したいということcityです。

タグを使用して配列インデックスを追跡したり、配列内のオブジェクトを検索したりする代わりに、markerinへの参照を追加することをお勧めしAddressAnnotationます。このように、オブジェクトがある場合、その参照を使用してAddressAnnotation関連するオブジェクトを取得できますmarker(検索やインデックスは必要ありません)。

AddressAnnotationクラスに、というタイプの保持プロパティを追加markerしますparentMarkerAddressAnnotationからオブジェクトを作成するときは、markerを呼び出す前に、プロパティを現在のaddAnnotationに設定します。parentMarkermarker

次に、calloutAccessoryControlTappedにキャストview.annotationAddressAnnotationて、プロパティに簡単にアクセスできるようにしparentMarkerます。これで、マーカーからaddressとを取得できます。city(ちなみに、そのメソッドでは、すでに1つであり、名前を付ける必要がないため、キャストviewする必要はありません。)MKAnnotationViewannView

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 に答える