【问题标题】:MapKit Annotations Appear in One View Controller, and not in anotherMapKit 注释出现在一个视图控制器中,而不是另一个
【发布时间】:2018-04-30 06:02:53
【问题描述】:

在我的浏览视图控制器中,地图工具包注释出现了。

_mapVC = [[CollectionMapViewController alloc] init];
mapVC.collectedLeafArray = [collectionFetchedResultsController fetchedObjects];
    mapVC.canShowCallout = YES;
    mapVC.delegate = self;
    [mapVC layoutMapView];
    [self addChildViewController:mapVC];
    [self.view insertSubview:mapVC.view belowSubview:_userCollectionSortMenu.view];

    [mapVC didMoveToParentViewController:self];

我在笔记视图控制器中设置子视图控制器的方式完全相同。但是,它们的注释图钉不显示。

 _mapVC = [[CollectionMapViewController alloc] init];
    _mapVC.collectedLeafArray = [NSArray arrayWithObject:_collectedLeaf];
    _mapVC.delegate = self;
    _mapVC.canShowCallout = NO;
    [_mapVC layoutMapView];

    [self addChildViewController:_mapVC];
    [_scrollView addSubview:_mapVC.view]; 
    [_mapVC didMoveToParentViewController:self];

我知道收集到的叶子不是零,因为我在设置注释的CollectionMapViewController 方法中检查了纬度:

- (void)layoutMapView
{
    NSMutableArray* leavesWithValidGeotag = [[NSMutableArray alloc] initWithCapacity:[collectedLeafArray count]];

    for (CollectedLeaf* collectedLeaf in collectedLeafArray)
    {
        NSLog(@"latitude: %@", collectedLeaf.latitude);
        if ( ![collectedLeaf.latitude isEqualToString:kGeoLocationNotAvailable] )
        {
            [leavesWithValidGeotag addObject:collectedLeaf];
            LeafAnnotation* annotation = [[LeafAnnotation alloc] initWithLeaf:collectedLeaf];
            [mapView addAnnotation:annotation];
            [annotation release];
        }
    }

    //// Adjust the region

    if([leavesWithValidGeotag count] > 1)
    {
        NSArray* westEastSort = [leavesWithValidGeotag sortedArrayUsingSelector:@selector(longitudeCompare:)];
        CollectedLeaf* eastMostLeaf = [westEastSort objectAtIndex:0];
        CollectedLeaf* westMostLeaf = [westEastSort lastObject];

        NSArray* southNorthSort = [leavesWithValidGeotag sortedArrayUsingSelector:@selector(latitudeCompare:)];
        CollectedLeaf* southMostLeaf = [southNorthSort objectAtIndex:0];
        CollectedLeaf* northMostLeaf = [southNorthSort lastObject];

        CLLocationCoordinate2D center;
        center.longitude = ([westMostLeaf.longitude doubleValue] + [eastMostLeaf.longitude doubleValue]) / 2.0f;
        center.latitude = ([southMostLeaf.latitude doubleValue] + [northMostLeaf.latitude doubleValue]) / 2.0f;

        MKCoordinateSpan span = MKCoordinateSpanMake(1.5 * fabs([northMostLeaf.latitude doubleValue] - [southMostLeaf.latitude doubleValue]),
                                                     1.5 * fabs([westMostLeaf.longitude doubleValue] - [eastMostLeaf.longitude doubleValue]));

        if ( span.latitudeDelta < kMinimumSpan )
        {
            span.latitudeDelta = kMinimumSpan;
        }
        if ( span.longitudeDelta < kMinimumSpan )
        {
            span.longitudeDelta = kMinimumSpan;
        }

        MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
        [mapView setRegion:region];
    }

    else if([leavesWithValidGeotag count] == 1)
    {
        CollectedLeaf* theLeaf = [leavesWithValidGeotag objectAtIndex:0];
        CLLocationCoordinate2D center;
        center.longitude = [theLeaf.longitude doubleValue];
        center.latitude = [theLeaf.latitude doubleValue];
        MKCoordinateSpan span = MKCoordinateSpanMake(kMinimumSpan, kMinimumSpan);
        MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
        [mapView setRegion:region];
    }

    [leavesWithValidGeotag release];
}

【问题讨论】:

    标签: ios objective-c mapkit mapkitannotation


    【解决方案1】:

    也许,你需要复制 NSArray 对象。

    NSArray *test1 = [NSArray arrayWithObject:_collectedLeaf];
    _mapVC.collectedLeafArray = [test1 copy];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 2013-07-25
      • 1970-01-01
      相关资源
      最近更新 更多