How to pass sender tag in DetailView to get default Map application for direction?

我的梦境 提交于 2019-12-02 20:56:07

问题


I am opening the Detailview on annotation pin as well as on tableview. To get direction from detailview I have place following button click event.

Edited:: with -(IBAction)showDirectionUpdated; coding

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

    }

}

I want to pass some sender or id to call respective Direction from detailView which I get on pressing annotation. I am successful with getting direction default app from detailview which I get by selecting tableview(listview). Here some code of sender tag.

Edited 2=== with 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;
      }}

回答1:


Based on the code posted, here is my understanding:

marker is the top-level class that stores all the details about a location including address and city.

AddressAnnotation is the class that implements the MKAnnotation protocol currently containing just the coordinate, title, and subtitle for a location (it does not have the address and city). This is the class used to create objects that are passed to the map view's addAnnotation method.

What you would like to do is that when the callout button is pressed on an annotation, you would like to show some detail view or information that requires the address and city.

Instead of trying to keep track of array indexes using tags or searching for objects in arrays, I suggest adding a reference to the marker in AddressAnnotation. This way, when you have an AddressAnnotation object, you can get its related marker object using that reference (no searching or indexes needed).

In the AddressAnnotation class, add a retain property of type marker called parentMarker. When you create an AddressAnnotation object from a marker and before calling addAnnotation, set the parentMarker property to the current marker.

Then in calloutAccessoryControlTapped, cast view.annotation to an AddressAnnotation to get easy access to the parentMarker property. Now you can get the address and city from the marker. (By the way, in that method you don't need to cast view to MKAnnotationView since it already is one and you don't have to name it annView.)

I am not sure what BcardView is and why you're checking if it's hidden or not.

I'd also suggest a slight improvement to the viewForAnnotation method (use a non-blank re-use id and move the setting of the properties that don't change to inside the 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;
}


来源:https://stackoverflow.com/questions/8256288/how-to-pass-sender-tag-in-detailview-to-get-default-map-application-for-directio

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!