0

I am inserting some annotations that are coming from a json server, but I wanted to check if the annotation is already on the map, if so, does not add it again. For they are being added on each other , have someone help me solve this problem?

my code:

  // adiciona produtos ao mapa
    - (void)adicionaAnnotationsNoMapa:(id)objetos{
        NSMutableArray *annotationsPins = [[NSMutableArray alloc] init];

    for (NSDictionary *annotationDeProdutos in objetos) {
        CLLocationCoordinate2D location;
        AnnotationMap *myAnn;
        myAnn = [[AnnotationMap alloc] init];
        location.latitude = [[annotationDeProdutos objectForKey:@"latitude"] floatValue];
        location.longitude = [[annotationDeProdutos objectForKey:@"longitude"] floatValue];
        myAnn.coordinate = location;
        myAnn.title = [annotationDeProdutos objectForKey:@"name"];
        myAnn.subtitle = [NSString stringWithFormat:@"R$ %@",[annotationDeProdutos objectForKey:@"price"]];
        myAnn.categoria = [NSString stringWithFormat:@"%@", [annotationDeProdutos objectForKey:@"id_categoria"]];
        myAnn.idProduto = [NSString stringWithFormat:@"%@", [annotationDeProdutos objectForKey:@"id"]];
        [annotationsPins addObject:myAnn];
    }

    [self.mapView addAnnotations:annotationsPins];
}

3 Answers 3

0

You can iterate through annotations already on MkMapView and see if they are already there:

NSArray * annotations = [self.mapView.annotations copy];
for (NSDictionary *annotationDeProdutos in objetos)
{
    // Check if annotation in annotations are duplicate of annotationDeProdutos.
    // You can match using name.
}

Or the other way if you only want to show annotations from single server call:

[self.mapView removeAnnotations: self.mapView.annotations];
// Now add from JSON response.
1
  • How do I check if the annotations are duplicate on the NSArray * annotations ? Commented Apr 8, 2015 at 11:28
0

You can do this for each of your annotations:

if(![self.mapView.annotations containsObject: myAnn]) {
    [self.mapView addAnnotations: myAnn];
}
2
  • This way they work, they are still being placed one over the other . Commented Apr 8, 2015 at 11:30
  • That's because they're not the same annotation, just annotations in the same location, but with different objects. You can do either remove the map annotations and then add the news, or compare the annotations' locations before adding the new one.
    – Pablo A.
    Commented Apr 8, 2015 at 12:36
0

I solved the problem with this code :

// adiciona produtos ao mapa
- (void)adicionaAnnotationsNoMapa:(id)objetos{
    NSMutableArray *annotationsPins = [[NSMutableArray alloc] init];

for (NSDictionary *annotationDeProdutos in objetos) {

    CLLocationCoordinate2D location;
    AnnotationMap *myAnn;
    myAnn = [[AnnotationMap alloc] init];
    location.latitude = [[annotationDeProdutos objectForKey:@"latitude"] floatValue];
    location.longitude = [[annotationDeProdutos objectForKey:@"longitude"] floatValue];
    myAnn.coordinate = location;
    myAnn.title = [annotationDeProdutos objectForKey:@"name"];
    myAnn.subtitle = [NSString stringWithFormat:@"R$ %@",[annotationDeProdutos objectForKey:@"price"]];
    myAnn.categoria = [NSString stringWithFormat:@"%@", [annotationDeProdutos objectForKey:@"id_categoria"]];
    myAnn.idProduto = [NSString stringWithFormat:@"%@", [annotationDeProdutos objectForKey:@"id"]];

    if (self.mapView.annotations.count <1) {
        [annotationsPins addObject:myAnn];
    } else {

        __block NSInteger foundIndex = NSNotFound;

        [annotationsPins enumerateObjectsUsingBlock:^(AnnotationMap *annotation, NSUInteger idx, BOOL *stop) {
            CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:location.latitude longitude:location.longitude];
            CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];

            if ([loc1 distanceFromLocation:loc2] <= 1.0f) {
                foundIndex = idx;
                *stop = YES;
            }
        }];

        if (foundIndex != NSNotFound) {
            [annotationsPins addObject:myAnn];
        }
    }

}

[self.mapView addAnnotations:annotationsPins];

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.