show another view when clicked on pin annotation

447 views Asked by At

I have many garages and need to show multiple garages in map.

Below is the code I am using for Map to show multiple pins.

CLLocationCoordinate2D annotationCoord;
for (NSMutableDictionary* aDict in Gfeeds) {
    annotationCoord.latitude = [aDict[@"GLatitude"] floatValue];
    annotationCoord.longitude = [aDict[@"GLongitude"] floatValue];

    MKPointAnnotation *annotationPoint2 = [[MKPointAnnotation alloc] init];
    annotationPoint2.coordinate = annotationCoord;
    annotationPoint2.title = [NSString stringWithFormat:@"%@", aDict[@"GName"]];
    annotationPoint2.subtitle = aDict[@"GId"];

    [geomapView addAnnotation:annotationPoint2];

}

Now what I wanted is show the details of garage after clicking that map pin… for that I am doing below.

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{

    MKPinAnnotationView *pinAnnotation = nil;
    if(annotation != geomapView.userLocation)
    {
        static NSString *defaultPinID = @"myPin";
        pinAnnotation = (MKPinAnnotationView *)[geomapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinAnnotation == nil )
            pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

        pinAnnotation.canShowCallout = YES;

        //instatiate a detail-disclosure button and set it to appear on right side of annotation
        UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        pinAnnotation.rightCalloutAccessoryView = infoButton;

    }

    return pinAnnotation;
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
    GarageDetailsViewController *secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"GarageDetails"];

    secondView.garageMainId = view.annotation.subtitle;

    [self.navigationController pushViewController:secondView animated:YES];
}

Using this way I can able to go to GarageDetails, however risk I am taking is showing Id of garage in subtitle. I want to hide this subtitle so that Id would not get displayed.

Any idea how can I do this?

  1. Hide subtitle

OR

  1. Pass id to GarageDetails from map...
1

There are 1 answers

2
Michael Dautermann On BEST ANSWER

Why not subclass "MKPointAnnotation" (you can name it something like "FahimPointAnnotation), and in this subclass you can add a "garageID" property.

Then you can add the annotation to your map and when it's clicked on, you can retrieve the annotation and cast the "MKPointAnnotation" back to a "FahimPointAnnotation" and grab your garageID out of it (without having to worry about it appearing in the annotation view's subtitle field).