2013-11-26 44 views
0

基本上我使用包含地方对象数组的模型数组。对于每个地点对象,我都有各种属性,例如经度,纬度,地点名称,地点说明等。将链接注释到模型数组

我可以将注释放在地点所在的位置。但是,当用户点击正确的呼叫泄露按钮,我希望他们继续到另一个视图控制器。现在我知道如何做到这一点。

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

想想一个索引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

许多解决方案关联注释惠特模型阵列。

下面的解决办法是什么我uesd:

1.新增int变量 “标签”,在你的PlaceDealAnnotation

@property (nonatomic, assign) int tag; 

2.change了 “循环”,在setMapAnnotations:方法

//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 
} 
+0

令人惊叹!完美工作。这非常有帮助。我没有在MKAnnotation上寻找标签属性,但没有提供任何内容。我没有想到创造我自己的。毫无疑问,这个解决方案将对未来的这个和其他事物有用!谢谢 – Anthony