Question

Basically I use a (model) array containing an array of places objects. For each place object I have various properties such as latitude, longitude, place name, place description etc.

I can place annotations where the places are. However when a user clicks on the right call out disclosure button I want them to segue to another view controller. Now I know how to do that.

The serious problem I am having is that I am unable to link the clicked annotation with my model array. In my next view controller I shall display all of the place info, but I can not discover which annotation has been clicked.

Think of a tableview which has index path.row, I need something similar to discover the right annotation.

Thanks.

    -(void)setMapAnnotations
{
    //Create the region - N.B - part of the map you wish to show
    MKCoordinateRegion dealMapRegion;

    //Center - N.B - The exact center of the region.
    CLLocationCoordinate2D center;
    center.latitude = 51.481623;
    center.longitude = -0.18519;

    //Span
    MKCoordinateSpan span;
    span.latitudeDelta = THE_SPAN;
    span.longitudeDelta = THE_SPAN;

    dealMapRegion.center = center;
    dealMapRegion.span = span;

    [self.dealMapMapView setRegion:dealMapRegion animated:YES];

    //array to hold places
    NSMutableArray *placeLocations = [[NSMutableArray alloc]init];
    CLLocationCoordinate2D location;


    //After region has been set you need to populate the annotations
    for (DealListModel *dealLstObj in self.dealMapModelArray) {
        //Get the place from the model objects stored in the model array
        PlaceDealAnnotation *placeAnnontation = [[PlaceDealAnnotation alloc]init];
        double tempPlaceLatitude = [dealLstObj.placeLatitude doubleValue];
        double tempPlaceLongitude = [dealLstObj.placeLongitude doubleValue];

        location.latitude = tempPlaceLatitude;
        location.longitude = tempPlaceLongitude;
        placeAnnontation.coordinate = location;
        placeAnnontation.title = dealLstObj.placeDescription;
        //adding array of locations map
        [placeLocations addObject:placeAnnontation];
    }

    [self.dealMapMapView addAnnotations:placeLocations];

}//end of setMapAnnotations


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

    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    NSString *annotationIdentifier = @"CustomViewAnnontation";

    DealMapCustomAnnotationView *customAnnotationView = (DealMapCustomAnnotationView *) [self.dealMapMapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];

    if (!customAnnotationView) {
        customAnnotationView = [[DealMapCustomAnnotationView alloc]initWithAnnotationwithImage:annotation reuseIdentifier:annotationIdentifier annotationViewImage:[UIImage imageNamed:@"pin.png"]];

        customAnnotationView.canShowCallout = YES;
        customAnnotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    }

    return customAnnotationView;

}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    [self performSegueWithIdentifier:@"mapPlaceSegue" sender:view];
}
Was it helpful?

Solution

Many solutions to associate annotation whit Model Array.

The below solution is what i uesd:

1.add int variable "tag" in your PlaceDealAnnotation

@property (nonatomic, assign) int tag;

2.change the "for cycle" in setMapAnnotations: method

//After region has been set you need to populate the annotations
for (int tag = 0; tag < [self.dealMapModelArray count]; tag++) {
    //Get the place from the model objects stored in the model array
    DealListModel *dealLstObj = [self.dealMapModelArray objectAtIndex:tag];
    PlaceDealAnnotation *placeAnnontation = [[PlaceDealAnnotation alloc]init];
    double tempPlaceLatitude = [dealLstObj.placeLatitude doubleValue];
    double tempPlaceLongitude = [dealLstObj.placeLongitude doubleValue];
    //set tag here
    placeAnnontation.tag = tag;
    location.latitude = tempPlaceLatitude;
    location.longitude = tempPlaceLongitude;
    placeAnnontation.coordinate = location;
    placeAnnontation.title = dealLstObj.placeDescription;
    //adding array of locations map
    [placeLocations addObject:placeAnnontation];
}

3.implement the following mapView's delegate method:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
     PlaceDealAnnotation * annotation = [view annotation];
     int tag = annotation.tag;
     //get the model your want here
     DealListModel *targetModel = [self.dealMapModelArray objectAtIndex:tag];
     //go to the next view controller here
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top