【问题标题】:How to add new Annotation with existed annotations?如何使用现有注释添加新注释?
【发布时间】:2017-06-27 13:36:11
【问题描述】:

在我的应用程序中,我从特定位置发布了录制的视频。我可以从另一个服务响应中获取已发布位置详细信息的列表。所以最初我得到了发布的位置详细信息并将其显示在 MapView 上。在 MapView 中,引脚显示为 Cluster 效果,所以我使用了kingpin

下面我已经在地图上加载了注释。

- (void)loadLocations:(NSArray *)arrayValues 
{
    _annotationArray = arrayValues;

    [self.clusteringController setAnnotations:[self reSetannotations]];

    [self.mapView setZoomEnabled:YES];

    [self.mapView setCenterCoordinate:self.mapView.userLocation.coordinate];

    [self.mapView setUserTrackingMode:MKUserTrackingModeFollow];


    KPGridClusteringAlgorithm *algorithm = [KPGridClusteringAlgorithm new];

    algorithm.annotationSize = CGSizeMake(25, 50);

    algorithm.clusteringStrategy = KPGridClusteringAlgorithmStrategyTwoPhase;

    self.clusteringController = [[KPClusteringController alloc] initWithMapView:self.mapView
                                                            clusteringAlgorithm:algorithm];
    self.clusteringController.delegate = self;

    self.clusteringController.animationOptions = UIViewAnimationOptionCurveEaseOut;

    [self.clusteringController setAnnotations:[self annotations]];

    NSString * lastobjlat;

    double miles;

    CLLocation * location = [COMMON currentLocation];

    lastobjlat = [NSString stringWithFormat:@"%f",location.coordinate.latitude];

    miles = 1.;

    double scalingFactor = ABS( (cos(2 * M_PI * [lastobjlat floatValue] / 360.0) ));

    MKCoordinateSpan span;

    span.latitudeDelta = miles/69.0;

    span.longitudeDelta = miles/(scalingFactor * 69.0);

    MKCoordinateRegion region;

    region.span = span;

    region.center = location.coordinate;

    [self.mapView setRegion:region animated:YES];

    self.mapView.showsUserLocation = YES;


    //Call in the below selectAnnotationAction method when I came to this, after I published new one on existed or new locations
    if (COMMON.isRecentPublication == YES) {

        COMMON.isRecentPublication = NO;

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            [self selectAnnotationAction];
        });
    }
}

//重置注解

- (NSArray *)reSetannotations 
{
    NSMutableArray *annotations = [NSMutableArray array];

    return annotations;
}

//这里我在自定义标记类 MapAnnotation 上管理位置详细信息。

- (NSArray *)annotations {

    CLLocationCoordinate2D locationPort;

    NSMutableArray *annotations = [NSMutableArray array];

    NSString *latitude, *longitude;

    if ([_annotationArray count] > 0) {

        for (int i = 0; i <[_annotationArray count]; i++){

            latitude = [NSString stringWithFormat:@"%@",[[_annotationArray objectAtIndex:i] valueForKey:@"latitude"]];

            latitude = [latitude stringByReplacingOccurrencesOfString:@"\n" withString:@""];

            latitude = [latitude stringByReplacingOccurrencesOfString:@"\t" withString:@""];

            longitude = [NSString stringWithFormat:@"%@",[[_annotationArray objectAtIndex:i] valueForKey:@"longitude"]];

            longitude = [longitude stringByReplacingOccurrencesOfString:@"\n" withString:@""];

            longitude = [longitude stringByReplacingOccurrencesOfString:@"\t" withString:@""];

            latitude = [NSString replaceEmptyStringInsteadOfNull:latitude];

            longitude = [NSString replaceEmptyStringInsteadOfNull:longitude];

            int publicationRatio   = [[[_annotationArray objectAtIndex:i] valueForKey:@"publicationRatio"] intValue];

            int publicationCount   = [[[_annotationArray objectAtIndex:i] valueForKey:@"publicationsCount"] intValue];

            int teazLocationId     = [[[_annotationArray objectAtIndex:i] valueForKey:@"objectId"] intValue];

            BOOL isUpgrade         = [[[_annotationArray objectAtIndex:i] valueForKey:@"isUpgraded"] boolValue];

            locationPort = CLLocationCoordinate2DMake([latitude doubleValue] ,
                                                      [longitude doubleValue]);

            //TODO : This is my custom annotation method

            MapAnnotation *a1 = [[MapAnnotation alloc] initWithCoordinate:locationPort
                                                                            tag:i
                                                               publicationRatio:publicationRatio
                                                               publicationCount:publicationCount
                                                                 teazLocationId:teazLocationId isUpgraded:isUpgrade];

            a1.itemindex = i + 1;

            a1.publicationRatio = publicationRatio;

            a1.publicationCount = publicationCount;

            a1.teazLocationId = teazLocationId;

            a1.isUpgraded = isUpgrade;

            a1.coordinate = CLLocationCoordinate2DMake([latitude doubleValue] ,
                                                       [longitude doubleValue] );
            [annotations addObject:a1];

            if (COMMON.isRecentPublication == YES) {

                if ([COMMON.recentPublicationLocationID  isEqual: @(publishedLocationId)]) {

                    _recentAnnotationView = [[MKPinAnnotationView alloc] initWithAnnotation:a1 reuseIdentifier:@"cluster"];

                }
            }
        }
    }

    return annotations;
}

在初始时加载具有集群效应的 Annotations 是没有问题的。但是,当我从相同或不同(新)位置发布一些内容时,我需要重定向地图屏幕(发布部分是其他屏幕),并且我需要使用 活动图钉图像 显示该发布详细信息。

按照我在地图上显示已发布位置详细信息的步骤

  1. 我将 recentPublicationLocationID 创建为全局变量,并在发布服务后存储响应中的 recentPublishedLocationID。

  2. 然后这个返回类型方法-(NSArray *)annotations(重定向到mapView后我从另一个webservice获取位置详细信息,之后它会调用),

    李>

我已经比较了recentPublishedId 是否存在。然后如果存在,我已将我的自定义注释(包含最近发布的位置详细信息)分配给全局 MKPinAnnotationView 实例 - _recentAnnotationView

  1. 然后我直接从这个方法-(void)loadLocations:(NSArray *)arrayValues调用了didSelectPinAnnotation委托方法,如下所示,

//传递最近发布的位置详细信息

if (COMMON.isRecentPublication == YES)
{
    COMMON.isRecentPublication = NO;

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [self selectAnnotationAction];
    });
}

//selectAnnotationAction

- (void)selectAnnotationAction {

    COMMON.isRecentPublication = NO;

    COMMON.recentPublicationLocationID = nil;

    [self mapView:self.mapView didSelectAnnotationView:_recentAnnotationView];
}

如果我直接将 recentPublishedLocation 详细信息传递给 didSelectAnnotationView 委托,我只能显示 In Active pin 而不是 Active pin。

然后我用断点调试为什么我只能看到 In active pin

因为在这种情况下调用了 didselect 委托,我可以看到 活动图钉图像。但这只是在几秒钟之内。

因为 viewForAnnotation 委托被快速调用以获取其他引脚注释,所以选中的那个进入未选中状态

这是真正的问题。如何使用集群克服这项工作?

因为当我在地图上正确显示发布的位置详细信息时,即使它应该具有聚类效果。是的,我会放大查看具有集群效果的图钉。

【问题讨论】:

  • 您真的希望陌生人阅读所有这些文本行吗?
  • @EI Tomato 我需要建议,所以我只是解释了我做了什么。所以这就是为什么我用文字解释了很多。所以我不能忽视它。

标签: ios objective-c annotations mkmapview markerclusterer


【解决方案1】:

最后我得到了解决方案并使用ClusterKit library而不是Kingpin实现了我的要求。

这个工具真的帮助我实现了附加注释并根据我的要求定制每一个东西。

所以它对我更有帮助。当然是你们。

而且这个工具支持 Apple 和 Goole 地图与 Objective c 以及 Swift。

我希望这篇文章也对其他开发者有所帮助。

谢谢:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多