如何使用现有注释添加新注释?

时间:2017-06-27 13:36:12

标签: ios objective-c annotations mkmapview markerclusterer

在我的应用程序中,我发布了来自特定位置的录制视频。我可以从其他服务响应中获取已发布位置详细信息的列表。所以最初我得到了发布的位置详细信息并在MapView上显示。在MapView中,引脚显示为群集效果,因此我使用了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;
}

在初始时加载具有聚类效果的注释并不是一个问题。但是当我从相同或不同(新的)位置发布一个时,我需要重定向地图屏幕(发布部分是其他屏幕),我需要使用有效图像显示该出版物详细信息。

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

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

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

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

    1. 然后我直接从这个方法中调用了 didSelectPinAnnotation 委托方法 - (void)loadLocations:(NSArray *)arrayValues ,如下所示,
    2. //传递最近发布的位置详情

      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

      因为在这种情况下调用了doselect委托,我可以看到 Active pin image 。但它只在几秒内完成。

      因为快速调用了viewForAnnotation委托以进行其他引脚注释,因此选定的注释将进入未选择状态

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

      因为当我在地图上正确显示发布的位置详细信息时,它甚至可以用于聚类效果。是的我将缩放以查看具有聚类效果的引脚。

1 个答案:

答案 0 :(得分:0)

最后,我得到了解决方案,并使用ClusterKit library代替Kingpin来实现我的要求。

这个工具真的很有帮助我可以实现附加注释并根据我的要求定制所有的东西。

所以这对我更有帮助。当然你们所有人。

此工具支持Apple&amp; Goole使用Objective c和Swift进行映射。

我希望这对其他开发者也更有帮助。

谢谢:)