ios - 将注释链接到模型数组

标签 ios annotations mapkit mkannotation

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

我可以在这些地方放置注释。但是,当用户单击右侧的调用披露按钮时,我希望他们转到另一个 View Controller 。现在我知道该怎么做了。

我遇到的严重问题是我无法将单击的注释与我的模型数组链接起来。在我的下一个 View Controller 中,我将显示所有地点信息,但我无法发现单击了哪个注释。

考虑一个具有索引 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.在 PlaceDealAnnotation 中添加 int 变量“tag”

@property (nonatomic, assign) int tag;

2.更改setMapAnnotations:方法中的“for循环”

//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的委托(delegate)方法:

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

关于ios - 将注释链接到模型数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20226054/

相关文章:

swift - 自定义 MKAnnotation 字典输入 (CLLocationCooperative2d)

ios - 如何通过 Objective-C 添加自定义图像以替换 iOS MKMapView 中的默认图钉?

ios - 不要删除我 map 上的所有注释

ios - 使用调试器查找对对象的所有强引用

iOS MapBox map 滞后

ios - 如何在 Swift 中从同一站点重新加载新的 JSON 数据?

Spring @Transactional v Spring Security @Secured 不一致的行为

ios - 搜索对象数组以返回与属性值匹配的对象

symfony - Symfony2 插件在 PHPStorm 中的注释

scala - 从 Scala 宏注释中获取参数