将注释链接到模型数组

时间:2013-11-26 19:19:06

标签: ios annotations mapkit mkannotation

基本上我使用包含一个场所对象数组的(模型)数组。对于每个地方对象,我有各种属性,如纬度,经度,地名,地点描述等。

我可以在地方放置注释。但是,当用户点击右侧调用公开按钮时,我希望它们转到另一个视图控制器。现在我知道该怎么做了。

我遇到的严重问题是我无法将点击的注释与我的模型数组相关联。在我的下一个视图控制器中,我将显示所有的地方信息,但我无法找到已点击的注释。

想想一个具有索引path.row的tableview,我需要类似的东西来发现正确的注释。

感谢。

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

1 个答案:

答案 0 :(得分:1)

许多解决方案将关联注释与模型阵列相关联。

以下解决方案就是我所说的:

1.在PlaceDealAnnotation中添加变量“tag”

@property (nonatomic, assign) int tag;

2.在setMapAnnotations:method

中更改“for cycle”
//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.实现以下mapView的委托方法:

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