Do not insert repeated Annotations into the map

Asked

Viewed 32 times

2

I’m inserting some Annotations q are coming from a server JSON, but I wanted to check if the Annotation is already on the map, if yes, does not add it again. For they are being added over each other. There’s someone who can help me solve this problem?

My code by adding the pins:

  // 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];
    }

1 answer

1


Within this iteration for that you do, right after assigning the values of latitude and longitude, you can use something like block for this, thus making the comparison based on the distance between the coordinate to be inserted and those that already exist in the array.

Something like that:

__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] <= 5.0f) {
        foundIndex = idx;
        *stop = YES;
    }
}];

if (foundIndex != NSNotFound) {
    // Achou coordenada, não faz as linhas abaixo
    continue;
}

// Prossegue iteração

What happens here is the following: you use the type block enumerate to scan your annotationsPins array and search for some coordinate that is less than 5 meters of the current coordinate (loc1). If so, you interrupt the block and proceed.

If foundIndex is different from NSNotFound, means you’ve found a similar coordinate, so you don’t have to continue what you have on for down.

  • Thank you very much, it worked perfect. I just added a if to check if you have any pin on the map, because he was not able to check the first time because n had nothing on the map. Is there any way I can put the code resolution here? 'Cause I’m new to the stack.

  • Great! Consider marking this answer as accepted if you have answered your question. It will help others with the same question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.